Skip to content

Instantly share code, notes, and snippets.

@unionx
Created January 16, 2015 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unionx/6ac55c2b10a91f6f1766 to your computer and use it in GitHub Desktop.
Save unionx/6ac55c2b10a91f6f1766 to your computer and use it in GitHub Desktop.
Leancloud Python REST script
#!/usr/bin/env python3
import time
import json
import hashlib
import requests
appId = 'xxx'
appKey = 'xxx'
masterKey = 'xxx'
url = 'https://leancloud.cn/1.1/classes/'
def createSignHeader():
timestamp = str(int(time.time()))[0:13]
signString = timestamp + appKey
return hashlib.md5(signString.encode()).hexdigest() + ',' + timestamp
headers = {
'Content-Type': 'application/json',
'X-AVOSCloud-Application-Id': appId,
'X-AVOSCloud-Request-Sign': createSignHeader(),
}
class LeanObject(object):
# url = url + getObjectName()
def __init__(self, data, leanObjectName=None):
self.data = data
if leanObjectName is None:
self.LeanObjectName = self.getObjectName()
self.url = url + self.LeanObjectName
@classmethod
def getObjectName(cls):
return cls.__name__
@classmethod
def get(cls, where=None):
cls.url = url + cls.getObjectName()
if where is None:
r = requests.get(cls.url, headers=headers)
return r.json()
def save(self):
r = requests.post(self.url, data=json.dumps(self.data), headers=headers)
return r.json()
class Person(LeanObject):
pass
personInfo = {
'name': 'jiawen',
'age': 19,
'bf': 'guigui'
# 'gf': 'jiawen'
}
# person = Person(personInfo)
# print(person.save())
print(Person.get())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment