Skip to content

Fixed Persistent session #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions appwrite/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self):
self._chunk_size = 5*1024*1024
self._self_signed = False
self._endpoint = 'https://cloud.appwrite.io/v1'
self._session = None
self._global_headers = {
'content-type': '',
'user-agent' : 'AppwritePythonSDK/7.0.1 (${os.uname().sysname}; ${os.uname().version}; ${os.uname().machine})',
Expand All @@ -36,31 +37,38 @@ def add_header(self, key, value):
def set_project(self, value):
"""Your project ID"""

self._global_headers['x-appwrite-project'] = value
self._global_headers['x-Appwrite-project'] = value
return self

def set_key(self, value):
"""Your secret API key"""

self._global_headers['x-appwrite-key'] = value
self._global_headers['x-Appwrite-key'] = value
return self

def set_jwt(self, value):
"""Your secret JSON Web Token"""

self._global_headers['x-appwrite-jwt'] = value
self._global_headers['x-Appwrite-jwt'] = value
return self

def set_locale(self, value):
self._global_headers['x-appwrite-locale'] = value
self._global_headers['x-Appwrite-locale'] = value
return self

def set_session(self, value):
"""The user session to authenticate with"""

self._global_headers['x-appwrite-session'] = value
self._global_headers['x-Appwrite-session'] = value
return self

def get_session(self):
""" Get the current session """
if self._session is None:
self._session = requests.Session()
self._session.headers.update(self._global_headers)
return self._session

def set_forwarded_user_agent(self, value):
"""The user agent string of the client that made the request"""

Expand All @@ -79,7 +87,7 @@ def call(self, method, path='', headers=None, params=None, response_type='json')
data = {}
files = {}
stringify = False

headers = {**self._global_headers, **headers}

if method != 'get':
Expand All @@ -100,7 +108,7 @@ def call(self, method, path='', headers=None, params=None, response_type='json')

response = None
try:
response = requests.request( # call method dynamically https://stackoverflow.com/a/4246075/2299554
response = self.get_session().request(
method=method,
url=self._endpoint + path,
params=self.flatten(params, stringify=stringify),
Expand All @@ -113,7 +121,7 @@ def call(self, method, path='', headers=None, params=None, response_type='json')

response.raise_for_status()

warnings = response.headers.get('x-appwrite-warning')
warnings = response.headers.get('x-Appwrite-warning')
if warnings:
for warning in warnings.split(';'):
print(f'Warning: {warning}')
Expand Down Expand Up @@ -200,11 +208,11 @@ def chunked_upload(
headers,
params,
)

offset = offset + self._chunk_size
if "$id" in result:
headers["x-appwrite-id"] = result["$id"]

if "$id" in result:
headers["x-Appwrite-id"] = result["$id"]

if on_progress is not None:
end = min((((counter * self._chunk_size) + self._chunk_size) - 1), size - 1)
Expand All @@ -229,7 +237,7 @@ def flatten(self, data, prefix='', stringify=False):
finalKey = prefix + '[' + key +']' if prefix else key
finalKey = prefix + '[' + str(i) +']' if isinstance(data, list) else finalKey
i += 1

if isinstance(value, list) or isinstance(value, dict):
output = {**output, **self.flatten(value, finalKey, stringify)}
else:
Expand All @@ -239,4 +247,3 @@ def flatten(self, data, prefix='', stringify=False):
output[finalKey] = value

return output

Loading