我發現this發佈在Python REST客戶端的Work Etc.論壇上,並且所使用的論壇沒有包含代碼縮進,作者也沒有包含它們,所以我所做的就是輸入我相信是正確的缺口,並已經得到了以下內容:WorkEtc的Python實現。 API
import sys, json, urllib
from httplib2 import Http
class WORKetcRESTClient():
session_key = None
connector_hash = None
def __init__(self,url):
if not "http://" in url and not "https://" in url:
url = "http://%s" % url
self.base_url = url
else:
self.base_url = url
def authenticate(self,user,password):
args = { "email" : user,"pass" : password,}
res = self.request("AuthenticateWebSafe", args)
if res["Code"] == 1:
self.session_key = res["SessionKey"]
self.user = res["User"]
return True
else:
return False
def request(self,service,args):
url = "%s/%s" % (self.base_url, service)
if not self.session_key is None:
url = "%s?VeetroSession=%s" %(url,self.session_key)
p = {}
p['data'] = json.dumps(args)
h = Http()
r,c = h.request(url, body=json.dumps(args), method="POST", headers = {'contentType' : 'application/json; charset=utf-8'})
if r.status == 200:
jsondata = json.loads(c)
return jsondata
else:
print r,c
return {}
client = WORKetcRESTClient('http://company.worketc.com')
client.authenticate('[email protected]', 'pAsSwOrD')
result = client.request('FindCompanies',{'keywords':'customer'})
print result
100%誠實的,如果這種情況沒有任何錯誤運行,我不知道我會得到打印到控制檯,但是我得到的錯誤讓我無法找到:
Traceback (most recent call last):
File "worketc.py", line 42, in <module>
File "worketc.py", line 17, in authenticate
res = self.request("AuthenticateWebSafe", args)
File "worketc.py", line 34, in request
if r.status == 200:
UnboundLocalError: local variable 'r' referenced before assignment
它告訴我,變量'r'正在獲得cal在它被分配/創建之前被引導,但我不確定它需要被創建或移動到哪裏,考慮到它在當前腳本中的位置。任何人都可以對此有所瞭解嗎?
*從[serg553]評論(http://stackoverflow.com/users/3442593/serg553):*感謝這個非常好的片段。我想知道你有沒有辦法修復它? –