Use requests.Session()
This commit is contained in:
parent
2f56ce4314
commit
867969b660
@ -4,6 +4,7 @@ import requests
|
|||||||
class SeafileClient:
|
class SeafileClient:
|
||||||
|
|
||||||
def __init__(self,server,username,password=None,token=None):
|
def __init__(self,server,username,password=None,token=None):
|
||||||
|
self.session = requests.Session()
|
||||||
self.server = server
|
self.server = server
|
||||||
self.token = token
|
self.token = token
|
||||||
self.login = (username,password)
|
self.login = (username,password)
|
||||||
@ -13,7 +14,7 @@ class SeafileClient:
|
|||||||
|
|
||||||
def ping(self):
|
def ping(self):
|
||||||
try:
|
try:
|
||||||
return requests.get("%s/ping" % self.api_endpoint()).text == "pong"
|
return self.session.get("%s/ping" % self.api_endpoint()).text == "pong"
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ class SeafileClient:
|
|||||||
def obtain_token(self):
|
def obtain_token(self):
|
||||||
user,passw = self.login
|
user,passw = self.login
|
||||||
try:
|
try:
|
||||||
req=requests.post("%s/auth-token/" % self.api_endpoint(), data = {'username': user, 'password': passw })
|
req=self.session.post("%s/auth-token/" % self.api_endpoint(), data = {'username': user, 'password': passw })
|
||||||
json = req.json()
|
json = req.json()
|
||||||
if "non_field_errors" in json:
|
if "non_field_errors" in json:
|
||||||
print json["non_field_errors"]
|
print json["non_field_errors"]
|
||||||
@ -32,11 +33,12 @@ class SeafileClient:
|
|||||||
|
|
||||||
def authorize(self):
|
def authorize(self):
|
||||||
self.token = self.obtain_token()
|
self.token = self.obtain_token()
|
||||||
|
self.session.headers.update({'Authorization': "Token %s" % self.token.token})
|
||||||
return self.token != False
|
return self.token != False
|
||||||
|
|
||||||
#curl -H 'Authorization: Token 24fd3c026886e3121b2ca630805ed425c272cb96' -H 'Accept: application/json; indent=4' https://cloud.seafile.com/api2/repos/
|
#curl -H 'Authorization: Token 24fd3c026886e3121b2ca630805ed425c272cb96' -H 'Accept: application/json; indent=4' https://cloud.seafile.com/api2/repos/
|
||||||
def libraries(self):
|
def libraries(self):
|
||||||
resp=requests.get("%s/repos/" % self.api_endpoint(), headers = {'Authorization': "Token %s" % self.token.token, 'Accept': 'application/json; indent=4' })
|
resp=self.session.get("%s/repos/" % self.api_endpoint(), headers = {'Authorization': "Token %s" % self.token.token, 'Accept': 'application/json; indent=4' })
|
||||||
if not resp.status_code == 200: return
|
if not resp.status_code == 200: return
|
||||||
libraries=[]
|
libraries=[]
|
||||||
for lib in resp.json():
|
for lib in resp.json():
|
||||||
@ -48,6 +50,7 @@ class SeafileLibrary:
|
|||||||
|
|
||||||
def __init__(self,client,id,name,owner):
|
def __init__(self,client,id,name,owner):
|
||||||
self.client = client
|
self.client = client
|
||||||
|
self.session = self.client.session
|
||||||
self.id = id
|
self.id = id
|
||||||
self.name = name
|
self.name = name
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
@ -59,11 +62,11 @@ class SeafileLibrary:
|
|||||||
def obtain_link():
|
def obtain_link():
|
||||||
#curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" https://cloud.seafile.com/api2/repos/99b758e6-91ab-4265-b705-925367374cf0/upload-link/
|
#curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" https://cloud.seafile.com/api2/repos/99b758e6-91ab-4265-b705-925367374cf0/upload-link/
|
||||||
print "%s/upload-link" % self.api_endpoint()
|
print "%s/upload-link" % self.api_endpoint()
|
||||||
quoted = requests.get("%s/upload-link" % self.api_endpoint(), headers = {'Authorization': "Token %s" % self.client.token.token,}).text
|
quoted = self.session.get("%s/upload-link" % self.api_endpoint(), headers = {'Authorization': "Token %s" % self.client.token.token,}).text
|
||||||
return quoted[1:-1]
|
return quoted[1:-1]
|
||||||
|
|
||||||
#curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" -F file=@test.txt -F filename=test.txt -F parent_dir=/ http://cloud.seafile.com:8082/upload-api/73c5d117-3bcf-48a0-aa2a-3f48d5274ae3
|
#curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" -F file=@test.txt -F filename=test.txt -F parent_dir=/ http://cloud.seafile.com:8082/upload-api/73c5d117-3bcf-48a0-aa2a-3f48d5274ae3
|
||||||
resp = requests.post(link or obtain_link() ,
|
resp = self.session.post(link or obtain_link() ,
|
||||||
files={'file': (file_name,open(file, 'rb')), 'parent_dir': directory, 'target_file': "%s/%s" % (directory,file_name)},
|
files={'file': (file_name,open(file, 'rb')), 'parent_dir': directory, 'target_file': "%s/%s" % (directory,file_name)},
|
||||||
headers = {'Authorization': "Token %s" % self.client.token.token,}
|
headers = {'Authorization': "Token %s" % self.client.token.token,}
|
||||||
)
|
)
|
||||||
@ -73,7 +76,7 @@ class SeafileLibrary:
|
|||||||
|
|
||||||
#curl -H 'Authorization: Token f2210dacd3606d94ff8e61d99b477fd' -H 'Accept: application/json; charset=utf-8; indent=4' https://cloud.seafile.com/api2/repos/dae8cecc-2359-4d33-aa42-01b7846c4b32/file/detail/?p=/foo.c
|
#curl -H 'Authorization: Token f2210dacd3606d94ff8e61d99b477fd' -H 'Accept: application/json; charset=utf-8; indent=4' https://cloud.seafile.com/api2/repos/dae8cecc-2359-4d33-aa42-01b7846c4b32/file/detail/?p=/foo.c
|
||||||
def file_info(self,path):
|
def file_info(self,path):
|
||||||
resp=requests.get("%s/file/detail/?p=%s" % (self.api_endpoint(),path), headers = {'Authorization': "Token %s" % self.token.token, 'Accept': 'application/json; indent=4' })
|
resp=self.session.get("%s/file/detail/?p=%s" % (self.api_endpoint(),path), headers = {'Authorization': "Token %s" % self.token.token, 'Accept': 'application/json; indent=4' })
|
||||||
if resp.status_code == 200:
|
if resp.status_code == 200:
|
||||||
json = resp.json()
|
json = resp.json()
|
||||||
return SeafileFile(self,path,json['id'],json['size'])
|
return SeafileFile(self,path,json['id'],json['size'])
|
||||||
@ -88,11 +91,12 @@ class SeafileFile:
|
|||||||
self.id = id
|
self.id = id
|
||||||
self.path = path
|
self.path = path
|
||||||
self.library = library
|
self.library = library
|
||||||
|
self.session = self.library.session
|
||||||
self.size = size
|
self.size = size
|
||||||
|
|
||||||
#curl -v -X PUT -d "p=/foo.md" -H 'Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd' -H 'Accept: application/json; indent=4' https://cloud.seafile.com/api2/repos/afc3b694-7d4c-4b8a-86a4-89c9f3261b12/file/shared-link/
|
#curl -v -X PUT -d "p=/foo.md" -H 'Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd' -H 'Accept: application/json; indent=4' https://cloud.seafile.com/api2/repos/afc3b694-7d4c-4b8a-86a4-89c9f3261b12/file/shared-link/
|
||||||
def share(self):
|
def share(self):
|
||||||
resp = requests.put("%s/repos/%s/file/shared-link/" % (self.library.client.api_endpoint(),self.library.id),
|
resp = self.session.put("%s/repos/%s/file/shared-link/" % (self.library.client.api_endpoint(),self.library.id),
|
||||||
headers = {'Authorization': "Token %s" % self.library.client.token.token, 'Accept': 'application/json; indent=4'},
|
headers = {'Authorization': "Token %s" % self.library.client.token.token, 'Accept': 'application/json; indent=4'},
|
||||||
data = {'p': self.path}
|
data = {'p': self.path}
|
||||||
)
|
)
|
||||||
@ -101,7 +105,7 @@ class SeafileFile:
|
|||||||
def update(self,file):
|
def update(self,file):
|
||||||
def obtain_link():
|
def obtain_link():
|
||||||
#curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" https://cloud.seafile.com/api2/repos/99b758e6-91ab-4265-b705-925367374cf0/upload-link/
|
#curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" https://cloud.seafile.com/api2/repos/99b758e6-91ab-4265-b705-925367374cf0/upload-link/
|
||||||
quoted = requests.get("%s/update-link" % self.library.api_endpoint(), headers = {'Authorization': "Token %s" % self.library.client.token.token,}).text
|
quoted = self.session.get("%s/update-link" % self.library.api_endpoint(), headers = {'Authorization': "Token %s" % self.library.client.token.token,}).text
|
||||||
return quoted[1:-1]
|
return quoted[1:-1]
|
||||||
directory, name = os.path.split(self.path)
|
directory, name = os.path.split(self.path)
|
||||||
return self.library.upload(file,name,directory,obtain_link())
|
return self.library.upload(file,name,directory,obtain_link())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user