Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Prepare Wordnik for 2.0 release to Package Index #10

Merged
merged 4 commits into from

2 participants

@jaraco

These changesets restore the setup.py and create a package structure that will be suitable for use in a production environment. It includes minor changes to the readme for basic usage.

This pull request also includes a changeset that eliminates the monkey patching of the urllib2.Request by instead using a subclass, restoring the contrib in a219cec.

The library is now in a state where it can be released. If you would like assistance releasing this to PyPI, I'd be happy to help do so, but hobbeswalsh will have to authorize me as a maintainer.

The basic steps are these:
Optionally tag the changeset in the repo.
Run "setup.py sdist register upload"
Update the version indicated in setup.py (to 2.1 or so).
Commit and push.

Regards,
Jason

@fehguy fehguy merged commit 4088fcf into wordnik:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
18 README.md
@@ -9,18 +9,18 @@ Place the `wordnik` folder that you downloaded somewhere where it can be accesse
```
import sys
-# Put the full path to the wordnik/api directory here
-sys.path.append('/path/to/wordnik/api')
+# Put the full path to the directory containing the wordnik directory here
+sys.path.append('/parent/path')
-from APIClient import APIClient
-import model
+from wordnik.api.APIClient import APIClient
+import wordnik.model
api_key = 'YOUR API KEY HERE'
my_client = APIClient(api_key, 'http://api.wordnik.com/v4')
```
-You'll want to edit those lines to reflect the full path to the `wordnik/api` folder you downloaded, and to use your own personal API key.
+You'll want to edit those lines to reflect the full path to where you extracted the `wordnik` folder you downloaded, and to use your own personal API key.
Calling a Method
-----
@@ -28,7 +28,7 @@ Calling a Method
Once you have a client set up, you need to instantiate an API object for whichever category or categories of items you are interested in working with. For example, to work with the `word` API and apply the method `getTopExample` method, you can do the following:
```
-from WordAPI import WordAPI
+from wordnik.api.WordAPI import WordAPI
wordAPI = WordAPI(my_client)
example = wordAPI.getTopExample('irony')
@@ -48,7 +48,7 @@ Let's see an example using the `getDefinitions` method. Examining its definition
we see that it takes `wordDefinitionsInput` as its input, so we'll first instantiate an object of class `WordDefinitionsInput`.
```
-input = model.WordDefinitionsInput.WordDefinitionsInput()
+input = wordnik.model.WordDefinitionsInput.WordDefinitionsInput()
```
Here `word` is a mandatory argument to the `getDefinitions` method, so we make sure to set that property on the input object after instantiating it. We'll also set a limit of 1, to get back a single definition, and let's also specify that we want a definition for our word when used as a verb.
@@ -57,7 +57,7 @@ Here `word` is a mandatory argument to the `getDefinitions` method, so we make s
input.word = 'tree'
input.limit = 1
input.partOfSpeech = 'verb'
-definition = wordAPI.getDefinitions(input)
+definition = wordAPI.getDefinitions(input)
```
-The variable `$definition` is now an instance of the `Definition` class defined in `wordnik/model/Definition.py`, as indicated in the documentation for `getDefinition`. It has all the properties that you'll see in the response body for that method call if you invoke it from the online documentation.
+The variable `$definition` is now an instance of the `Definition` class defined in `wordnik/model/Definition.py`, as indicated in the documentation for `getDefinition`. It has all the properties that you'll see in the response body for that method call if you invoke it from the online documentation.
View
16 setup.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+
+import setuptools
+
+setup_params = dict(
+ name='wordnik',
+ version="2.0",
+ description='Wordnik API for Python',
+ author='Robin Walsh',
+ author_email='robin@wordnik.com',
+ url='http://developer.wordnik.com',
+ packages = setuptools.find_packages(),
+)
+
+if __name__ == '__main__':
+ setuptools.setup(**setup_params)
View
0  wordnik/__init__.py
No changes.
View
30 wordnik/api/APIClient.py
@@ -4,17 +4,12 @@
the methods and models for each application are generated from the Swagger
templates."""
-import sys
-import os
import re
import urllib
import urllib2
-import httplib
import json
-sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../')
-import model
-
+import wordnik.model as model
class APIClient:
"""Generic API client for Swagger client library builds"""
@@ -47,7 +42,6 @@ def callAPI(self, resourcePath, method, queryParams, postData,
if value != None:
sentQueryParams[param] = value
url = url + '?' + urllib.urlencode(sentQueryParams)
- request = urllib2.Request(url=url, headers=headers)
elif method in ['POST', 'PUT', 'DELETE']:
data = postData
if data:
@@ -55,14 +49,11 @@ def callAPI(self, resourcePath, method, queryParams, postData,
data = json.dumps(postData)
elif type(postData) not in [str, int, float, bool]:
data = json.dumps(postData.__dict__)
- request = urllib2.Request(url=url, headers=headers, data=data)
- if method in ['PUT', 'DELETE']:
- # Monkey patch alert! Urllib2 doesn't really do PUT / DELETE
- request.get_method = lambda: method
-
else:
raise Exception('Method ' + method + ' is not recognized.')
+ request = MethodRequest(method=method, url=url, headers=headers, data=data)
+
# Make the request
response = urllib2.urlopen(request).read()
@@ -131,3 +122,18 @@ def deserialize(self, obj, objClass):
objClass))
return instance
+
+class MethodRequest(urllib2.Request):
+ def __init__(self, *args, **kwargs):
+ """
+ Construct a MethodRequest. Usage is the same as for
+ `urllib2.Request` except it also takes an optional `method`
+ keyword argument. If supplied, `method` will be used instead of
+ the default.
+ """
+ if 'method' in kwargs:
+ self.method = kwargs.pop('method')
+ return urllib2.Request.__init__(self, *args, **kwargs)
+
+ def get_method(self):
+ return getattr(self, 'method', urllib2.Request.get_method(self))
View
0  wordnik/api/__init__.py
No changes.
Something went wrong with that request. Please try again.