Hi Sven,
It worked perfectly now, it is really nice to have a library like Zinc.
Thank you!
Hernán
El 22/07/2013 5:37, Sven Van Caekenberghe escribió:
Hi Hernán,
It is great that you are using ZnClient and are providing feedback.
On 21 Jul 2013, at 08:58, Hernán Morales Durand <hernan.mora...@gmail.com>
wrote:
I'm testing the OpenCalais web service. This is a web service which
needs an API key to be used (registering takes only 1 minute in
http://www.opencalais.com/APIkey). To check the service first I've used
a Python script, taken from [1]
import httplib2
import json
LOCAL_API_KEY = '' # Aquire this by registering at the Calais site
CALAIS_TAG_API = 'http://api.opencalais.com/tag/rs/enrich'
# Some sample text from a news story to pass to Calais for analysis
test_body = """
Some huge announcements were made at Apple's Worldwide Developer's
Conference Monday, including the new mobile operating system iOS 5, PC
software OS X Lion, and the unveiling of the iCloud.
"""
# header information need by Calais. For more info see
http://www.opencalais.com/documentation/calais-web-service-api/api-invocation/rest
headers = {
'x-calais-licenseID': LOCAL_API_KEY,
'content-type': 'text/raw',
'accept': 'application/json',
}
# Create your http object
http = httplib2.Http()
# Make the http post request, passing the body and headers as needed.
response, content = http.request(CALAIS_TAG_API, 'POST',
headers=headers, body=test_body)
jcontent = json.loads(content) # Parse the json return into a python dict
print json.dumps(jcontent, indent=4) # Pretty print the resulting
dictionary returned.
The Python script works fine for me. I have tried to translate the code
above, but my version in Pharo 2.0 using Zinc returns "I Read a
ZnResponse(415 Unsupported Media Type text/plain 365B)"
| httpClient localApiKey |
localApiKey := ''. " my API key "
httpClient := ZnClient new
logToTranscript;
url: 'http://api.opencalais.com/tag/rs/enrich';
method: #POST;
headerAt: 'x-calais-licenseID' put: localApiKey;
headerAt: 'accept' put: 'application/json';
headerAt: 'content-Type' put: 'text/raw';
contents: 'Some huge announcements were made at Apple''s
Worldwide
Developer''s Conference Monday, including the new mobile operating
system iOS 5, PC software OS X Lion, and the unveiling of the iCloud.'.
httpClient execute.
Additionally the request returns the following message:
'Document conversion error. Please make sure that the content-type
(passed through the paramsXML) matches this document contents.
[ErrorMessage:
com.clearforest.platform.system.CLFRuntimeException: There is no
converter for this type of URI
"file:/C:/CLFSW/v71/ct/bin/../../../v71/ct/var/dptemp/prvt/228295-app9/dp/papi/data/1374378595643-DF317E60-6634505.asc"
]'
From what I could see, a difference is the 'text/raw'
content-type is passed in Python's httplib2, but in Zinc is overriding
by 'text/plain' it in #acceptEntityDescription: ? Any hints?
You were _very_ close. Indeed #contents: by default (without an explicit
#contentReader set) will do something very generic. Setting the content-type as
a header won't change the fact that the content-type of the entity wins, as you
correctly said, in #acceptEntityDescription:
The solution is to explicitly make your own entity, like this:
| httpClient localApiKey text |
localApiKey := 'xxx'. " my API key "
text := 'Some huge announcements were made at Apple''s Worldwide
Developer''s Conference Monday, including the new mobile operating
system iOS 5, PC software OS X Lion, and the unveiling of the
iCloud.'.httpClient := ZnClient new
systemPolicy;
url: 'http://api.opencalais.com/tag/rs/enrich';
headerAt: 'x-calais-licenseID' put: localApiKey;
entity: (ZnEntity with: text type: 'text/raw');
accept: 'application/json';
contentReader: [ :entity | NeoJSONReader fromString: entity contents ];
post.
Since content comes back as JSON, I parsed it already using NeoJSON (which you
can easily load using the configuration browser). Using #systemPolicy will
ensure that you get an error when the result is not 200 OK and application/json.
It worked for me: I made an account and got a result back.
HTH,
Sven
Hernán
[1]
http://www.flagonwiththedragon.com/2011/06/08/dead-simple-python-calls-to-open-calais-api/
--
Sven Van Caekenberghe
Proudly supporting Pharo
http://pharo.org
http://association.pharo.org
http://consortium.pharo.org