Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now using Python API Template for API
- Loading branch information
Showing
9 changed files
with
297 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env python | ||
|
||
from api import API | ||
|
||
__all__ = [API] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python | ||
|
||
"""Python wrapper for an API.""" | ||
|
||
try: | ||
import json | ||
except ImportError: # pragma: no cover | ||
# For older versions of Python. | ||
import simplejson as json | ||
|
||
try: | ||
from urllib import urlencode | ||
except ImportError: # pragma: no cover | ||
# For Python 3. | ||
from urllib.parse import urlencode | ||
|
||
try: | ||
from urllib2 import urlopen | ||
except ImportError: # pragma: no cover | ||
# For Python 3. | ||
from urllib.request import urlopen | ||
|
||
# Maybe modules should import their own API key. | ||
from api_key import API_KEY | ||
from xml2dict import xml2dict | ||
|
||
|
||
class API(object): | ||
"""An example class for a Python API wrapper.""" | ||
|
||
def __init__(self, api_key=''): | ||
if not api_key: | ||
self.api_key = API_KEY | ||
else: | ||
self.api_key = api_key | ||
self.base_url = '' | ||
self.output_format = None | ||
self.required_params = {'api_key': self.api_key} | ||
|
||
def call_api(self, directory, **kwargs): | ||
""" | ||
A generic example api wrapping method. Other methods can use this | ||
method to interact with the API. | ||
""" | ||
url_list = [self.base_url, '/%s' % directory] | ||
if self.required_params: | ||
kwargs.update(self.required_params) | ||
try: | ||
output_format = kwargs.pop('output_format') | ||
except KeyError: | ||
output_format = self.output_format | ||
params = urlencode(kwargs) | ||
url_list.extend(['?', params]) | ||
url = ''.join(url_list) | ||
data = urlopen(url).read() | ||
return self._format_data(output_format, data) | ||
|
||
def _format_data(self, output_format, data): | ||
"""Internal method to return formatted data to developer.""" | ||
if output_format == 'json': | ||
# Turn JSON into a dictionary. | ||
return json.loads(data) | ||
elif output_format == 'xml': | ||
return self._xml_to_dict(data) | ||
return data | ||
|
||
def _xml_to_dict(self, xml): | ||
""" | ||
Internal method to turn XML to dictionary output. Developers can | ||
overwrite this method to use their favorite XML parser of choice. | ||
""" | ||
return xml2dict(xml) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env python | ||
|
||
"""Store your API Key in this file.""" | ||
|
||
API_KEY = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
xml2dict | ||
======== | ||
|
||
Thunder Chen<nkchenz@gmail.com> 2007.9.1 | ||
|
||
http://code.google.com/p/xml2dict/ | ||
|
||
|
||
Note | ||
---- | ||
|
||
This is an updated, fully tested version of Thunder Chen's original `xml2dict` | ||
module. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
xml2dict | ||
======== | ||
|
||
Thunder Chen<nkchenz@gmail.com> 2007.9.1 | ||
|
||
http://code.google.com/p/xml2dict/ | ||
|
||
|
||
Note | ||
---- | ||
|
||
This is an updated, fully tested version of Thunder Chen's original `xml2dict` | ||
module. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
xml2dict | ||
Thunder Chen<nkchenz@gmail.com> 2007.9.1 | ||
Convert an XML string or file with XML data to a dict object. | ||
http://code.google.com/p/xml2dict/ | ||
""" | ||
|
||
from .xml2dict import XML2Dict, Dict2XML | ||
from object_dict import object_dict | ||
|
||
|
||
def xml2dict(data): | ||
"""Turn XML into a dictionary.""" | ||
converter = XML2Dict() | ||
if hasattr(data, 'read'): | ||
# Then it's a file. | ||
data = data.read() | ||
return converter.fromstring(data) | ||
|
||
|
||
def dict2xml(data): | ||
"""Turn a dictionary into XML.""" | ||
converter = Dict2XML() | ||
return converter.tostring(data) | ||
|
||
|
||
__all__ = [XML2Dict, Dict2XML, xml2dict, dict2xml, object_dict] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
object_dict | ||
nkchenz@gmail.com 2007 | ||
Provided as-is; use at your own risk; no warranty; no promises; enjoy! | ||
""" | ||
|
||
class object_dict(dict): | ||
"""Object view of dict, you can. | ||
>>> a = object_dict() | ||
>>> a.fish = 'fish' | ||
>>> a['fish'] | ||
'fish' | ||
>>> a['water'] = 'water' | ||
>>> a.water | ||
'water' | ||
>>> a.test = {'value': 1} | ||
>>> a.test2 = object_dict({'name': 'test2', 'value': 2}) | ||
>>> a.test, a.test2.name, a.test2.value | ||
(1, 'test2', 2) | ||
""" | ||
|
||
def __init__(self, initd=None): | ||
if initd is None: | ||
initd = {} | ||
dict.__init__(self, initd) | ||
|
||
def __getattr__(self, item): | ||
d = self.__getitem__(item) | ||
# if value is the only key in object, you can omit it | ||
if isinstance(d, dict) and 'value' in d and len(d) == 1: | ||
return d['value'] | ||
else: | ||
return d | ||
|
||
def __setattr__(self, item, value): | ||
self.__setitem__(item, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#!/usr/bin/env python | ||
|
||
"""Thunder Chen<nkchenz@gmail.com> 2007.9.1""" | ||
|
||
from __future__ import with_statement | ||
import re | ||
|
||
try: | ||
import xml.etree.ElementTree as ET | ||
except: # pragma: no cover | ||
# For Python 2.4 | ||
import cElementTree as ET | ||
|
||
from object_dict import object_dict | ||
|
||
|
||
class XML2Dict(object): | ||
|
||
def _parse_node(self, node): | ||
node_tree = object_dict() | ||
if node.text and node.attrib: | ||
if node.tag in node.attrib: | ||
raise ValueError("Name conflict: Attribute name conflicts with " | ||
"tag name. Check the documentation.") | ||
node.attrib.update({node.tag: node.text}) | ||
node.text = '' | ||
# Save attrs and text. Fair warning, if there's a child node with the same name | ||
# as an attribute, values will become a list. | ||
if node.text and node.text.strip(): | ||
node_tree = node.text | ||
else: | ||
for k, v in node.attrib.items(): | ||
k, v = self._namespace_split(k, v) | ||
node_tree[k] = v | ||
# Save children. | ||
for child in node.getchildren(): | ||
tag, tree = self._namespace_split(child.tag, self._parse_node(child)) | ||
if tag not in node_tree: # First encounter, store it in dict. | ||
node_tree[tag] = tree | ||
continue | ||
old = node_tree[tag] | ||
if not isinstance(old, list): | ||
# Multiple encounters, change dict to a list | ||
node_tree.pop(tag) | ||
node_tree[tag] = [old] | ||
node_tree[tag].append(tree) # Add the new one. | ||
if not node_tree: | ||
node_tree = None | ||
return node_tree | ||
|
||
def _namespace_split(self, tag, value): | ||
""" | ||
Split the tag '{http://cs.sfsu.edu/csc867/myscheduler}patients' | ||
ns = http://cs.sfsu.edu/csc867/myscheduler | ||
name = patients | ||
""" | ||
result = re.compile("\{(.*)\}(.*)").search(tag) | ||
if result: | ||
tag = result.groups(1) | ||
# value.namespace, tag = result.groups() | ||
return (tag, value) | ||
|
||
def parse(self, file): | ||
"""Parse an XML file to a dict.""" | ||
with open(file, 'r') as f: | ||
return self.fromstring(f.read()) | ||
|
||
def fromstring(self, s): | ||
"""Parse a string.""" | ||
t = ET.fromstring(s) | ||
root_tag, root_tree = self._namespace_split(t.tag, self._parse_node(t)) | ||
return object_dict({root_tag: root_tree}) | ||
|
||
|
||
class Dict2XML(object): | ||
"""Turn a dictionary into an XML string.""" | ||
|
||
def tostring(self, d): | ||
"""Convert dictionary to an XML string.""" | ||
if not isinstance(d, dict): | ||
raise TypeError('tostring must receive a dictionary: %r' % d) | ||
if len(d) != 1: | ||
raise ValueError('Dictionary must have exactly one root element') | ||
if isinstance(d.itervalues().next(), list): | ||
raise ValueError('Dictionary must not be a map to list: %r' % d) | ||
|
||
xml_list = ['<?xml version="1.0" encoding="UTF-8" ?>\n'] | ||
xml_list.append(self.__tostring_helper(d)) | ||
return ''.join(xml_list) | ||
|
||
def __tostring_helper(self, d): | ||
if isinstance(d, int): | ||
return str(d) | ||
|
||
elif isinstance(d, basestring): | ||
return '<![CDATA[%s]]>' % d | ||
|
||
elif isinstance(d, dict): | ||
x = [] | ||
for tag, content in d.iteritems(): | ||
if content is None: | ||
x.append('<%s />' % tag) | ||
elif isinstance(content, list): | ||
for c in content: | ||
if c is None: | ||
x.append('<%s />' % tag) | ||
else: | ||
x.append('<%s>%s</%s>' %\ | ||
(tag, self.__tostring_helper(c), tag)) | ||
else: | ||
x.append('<%s>%s</%s>' %\ | ||
(tag, self.__tostring_helper(content), tag)) | ||
xml_string = ''.join(x) | ||
return xml_string | ||
|
||
else: | ||
raise ValueError('Cannot convert %r to an XML string' % d) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d57185e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
best of luck