xml - WorkEtc API Python Script -
i'm trying create universal script in python can used import/export sorts of info from/to work etc crm platform. has documentation here: http://admin.worketc.com/xml.
however, bit stuck. authentication works, can phone call different api methods, ones without parameters. new python , that's why can't figure out how pass parameters onto specific method in api. need export time sheets. i'm trying phone call method specifically: http://admin.worketc.com/xml?op=getdrafttimesheets. obvious reasons cannot disclose login info might bit hard test you.
the code itself:
import xml.etree.elementtree et import urllib2 import sys email = 'email@domain.co.uk' password = 'pass' #service = 'getemployee?entityid=1658' #service = 'getentryid?entryid=23354' #service = ['getallcurrencieswebsafe'] #service = ['getentryid', 'entryid=23354'] service = ['getdrafttimesheets','2005-08-15t15:52:01+00:00','2014-08-15t15:52:01+00:00' ] class worketcuniversal(): sessionkey = none def __init__(self,url): if not "http://" in url , not "https://" in url: url = "http://%s" % url self.base_url = url else: self.base_url = url def authenticate(self, user, password): try: loginurl = self.base_url + email + '&pass=' + password req = urllib2.request(loginurl) response = urllib2.urlopen(req) the_page = response.read() root = et.fromstring(the_page) sessionkey = root[1].text print 'authentication successful!' try: f = self.service(sessionkey, service) except runtimeerror: print 'did not perform function!' except runtimeerror: print 'error logging in or calling service method!' def service(self, sessionkey, service): try: if len(service)<2: retrieveurl = 'https://domain.worketc.com/xml/' + service[0] + '?veetrosession=' + sessionkey else: retrieveurl = 'https://domain.worketc.com/xml/' + service[0,1,2] + '?veetrosession=' + sessionkey except typeerror err: print 'type error, means arguments wrong (or wrong implementation)' print 'quitting..' sys.exit() try: responsefile = urllib2.urlopen(retrieveurl) except urllib2.httperror err: if err.code == 500: print 'internal server error: permission denied or object (service) not exist' print 'quitting..' sys.exit() elif err.code == 404: print 'wrong url!' print 'quitting..' sys.exit() else: raise try: f = open("exportfolder/worketcdata.xml",'wb') line in responsefile: f.write(line) f.close() print 'file has been saved into: exportfolder' except (runtimeerror,unboundlocalerror): print 'could not write file' client = worketcuniversal('https://domain.worketc.com/xml/authenticatewebsafe?email=') client.authenticate(email, password)
writing code consuming api requires resolving few questions:
what methods on api available (get list names) how request such method looks (find out url, http method use, requirements body if used, headers expected) how build parts create request what methods availablehttp://admin.worketc.com/xml lists many of them
how request looks likegetdrafttimesheet described here http://admin.worketc.com/xml?op=getdrafttimesheets
and expects create next http request:
post /xml http/1.1 host: admin.worketc.com content-type: text/xml; charset=utf-8 content-length: length soapaction: "http://schema.veetro.com/getdrafttimesheets" <?xml version="1.0" encoding="utf-8"?> <soap:envelope xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:body> <getdrafttimesheets xmlns="http://schema.veetro.com"> <arg> <fromutc>datetime</fromutc> <toutc>datetime</toutc> </arg> </getdrafttimesheets> </soap:body> </soap:envelope>
building request the biggest task build shaped xml document shown above , having elements fromutc
, toutc
filled proper values. guess, values shall in format of iso datetime, shall find yourself.
you shall able building such xml python library, utilize lxml
.
note, xml document using namespaces, have handle them properly.
making post request headers shall easy. library utilize create http requests shall fill in content-length
value, done automatically.
e.g. "http://admin.worketc.com/xml?op=findarticleswebsafe" there set of different methods same service:
soap 1.1 soap 1.2 http get http postdepending on preferences, pick 1 fits needs.
the simplest http get.
for http requests, recommend using requests
, easy use, if through tutorial, understand mean.
python xml json api
No comments:
Post a Comment