Skip to content

Instantly share code, notes, and snippets.

@unionx

unionx/leancloud

Created Jan 16, 2015
Embed
What would you like to do?
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