Created
January 16, 2015 13:43
-
-
Save unionx/6ac55c2b10a91f6f1766 to your computer and use it in GitHub Desktop.
Leancloud Python REST script
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
#!/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