Skip to content

Commit a339dc5

Browse files
committed
Upgraded to support Appwrite 0.8
1 parent 4193473 commit a339dc5

37 files changed

+583
-46
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ jobs:
1313
username: "__token__"
1414
password: $PYPI_TOKEN
1515
on:
16-
tags: true
16+
tags: true

README.md

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Appwrite Python SDK
22

3-
![License](https://img.shields.io/github/license/appwrite/sdk-for-python.svg?v=1)
4-
![Version](https://img.shields.io/badge/api%20version-0.7.0-blue.svg?v=1)
3+
![License](https://img.shields.io/github/license/appwrite/sdk-for-python.svg?style=flat-square)
4+
![Version](https://img.shields.io/badge/api%20version-0.8.0-blue.svg?style=flat-square)
5+
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite_io?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite_io)
6+
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
57

6-
**This SDK is compatible with Appwrite server version 0.7.0. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-python/releases).**
8+
**This SDK is compatible with Appwrite server version 0.8.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-python/releases).**
79

810
Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way.
911
Use the Python SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools.
@@ -19,6 +21,73 @@ To install via [PyPI](https://pypi.org/):
1921
pip install appwrite
2022
```
2123

24+
25+
## Getting Started
26+
27+
### Init your SDK
28+
Initialize your SDK code with your project ID which can be found in your project settings page and your new API secret Key from project's API keys section.
29+
30+
```python
31+
from appwrite.client import Client
32+
from appwrite.services.users import Users
33+
34+
client = Client()
35+
36+
(client
37+
.set_endpoint('https://[HOSTNAME_OR_IP]/v1') # Your API Endpoint
38+
.set_project('5df5acd0d48c2') # Your project ID
39+
.set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
40+
.set_self_signed() # Use only on dev mode with a self-signed SSL cert
41+
)
42+
```
43+
44+
### Make Your First Request
45+
Once your SDK object is set, create any of the Appwrite service objects and choose any request to send. Full documentation for any service method you would like to use can be found in your SDK documentation or in the API References section.
46+
47+
```python
48+
users = Users(client)
49+
50+
result = users.create('[email protected]', 'password')
51+
```
52+
53+
### Full Example
54+
```python
55+
from appwrite.client import Client
56+
from appwrite.services.users import Users
57+
58+
client = Client()
59+
60+
(client
61+
.set_endpoint('https://[HOSTNAME_OR_IP]/v1') # Your API Endpoint
62+
.set_project('5df5acd0d48c2') # Your project ID
63+
.set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
64+
.set_self_signed() # Use only on dev mode with a self-signed SSL cert
65+
)
66+
67+
users = Users(client)
68+
69+
result = users.create('[email protected]', 'password')
70+
```
71+
72+
### Error Handling
73+
The Appwrite Python SDK raises `AppwriteException` object with `message`, `code` and `response` properties. You can handle any errors by catching `AppwriteException` and present the `message` to the user or handle it yourself based on the provided error information. Below is an example.
74+
75+
```python
76+
users = Users(client)
77+
try:
78+
result = users.create('[email protected]', 'password')
79+
except AppwriteException as e:
80+
print(e.message)
81+
```
82+
83+
### Learn more
84+
You can use followng resources to learn more and get help
85+
- 🚀 [Getting Started Tutorial](https://appwrite.io/docs/getting-started-for-server)
86+
- 📜 [Appwrite Docs](https://appwrite.io/docs)
87+
- 💬 [Discord Community](https://appwrite.io/discord)
88+
- 🚂 [Appwrite Python Playground](https://github.com/appwrite/playground-for-python)
89+
90+
2291
## Contribution
2392

2493
This library is auto-generated by Appwrite custom [SDK Generator](https://github.com/appwrite/sdk-generator). To learn more about how you can help us improve this SDK, please check the [contribution guide](https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md) before sending a pull-request.

appwrite/client.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import io
22
import requests
3+
from .exception import AppwriteException
34

45
class Client:
56
def __init__(self):
67
self._self_signed = False
78
self._endpoint = 'https://appwrite.io/v1'
89
self._global_headers = {
910
'content-type': '',
10-
'x-sdk-version': 'appwrite:python:0.1.0',
11+
'x-sdk-version': 'appwrite:python:0.2.0',
12+
'X-Appwrite-Response-Format' : '0.8.0',
1113
}
1214

1315
def set_self_signed(self, status=True):
@@ -34,6 +36,12 @@ def set_key(self, value):
3436
self._global_headers['x-appwrite-key'] = value.lower()
3537
return self
3638

39+
def set_j_w_t(self, value):
40+
"""Your secret JSON Web Token"""
41+
42+
self._global_headers['x-appwrite-jwt'] = value.lower()
43+
return self
44+
3745
def set_locale(self, value):
3846
self._global_headers['x-appwrite-locale'] = value.lower()
3947
return self
@@ -66,26 +74,36 @@ def call(self, method, path='', headers=None, params=None):
6674
if isinstance(data[key], io.BufferedIOBase):
6775
files[key] = data[key]
6876
del data[key]
69-
70-
response = requests.request( # call method dynamically https://stackoverflow.com/a/4246075/2299554
71-
method=method,
72-
url=self._endpoint + path,
73-
params=self.flatten(params),
74-
data=self.flatten(data),
75-
json=json,
76-
files=files,
77-
headers=headers,
78-
verify=self._self_signed,
79-
)
80-
81-
response.raise_for_status()
82-
83-
content_type = response.headers['Content-Type']
84-
85-
if content_type.startswith('application/json'):
86-
return response.json()
87-
88-
return response._content
77+
response = None
78+
try:
79+
response = requests.request( # call method dynamically https://stackoverflow.com/a/4246075/2299554
80+
method=method,
81+
url=self._endpoint + path,
82+
params=self.flatten(params),
83+
data=self.flatten(data),
84+
json=json,
85+
files=files,
86+
headers=headers,
87+
verify=self._self_signed,
88+
)
89+
90+
response.raise_for_status()
91+
92+
content_type = response.headers['Content-Type']
93+
94+
if content_type.startswith('application/json'):
95+
return response.json()
96+
97+
return response._content
98+
except Exception as e:
99+
if response != None:
100+
content_type = response.headers['Content-Type']
101+
if content_type.startswith('application/json'):
102+
raise AppwriteException(response.json()['message'], response.status_code, response.json())
103+
else:
104+
raise AppwriteException(response.text, response.status_code)
105+
else:
106+
raise AppwriteException(e)
89107

90108
def flatten(self, data, prefix=''):
91109
output = {}

appwrite/exception.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AppwriteException(Exception):
2+
def __init__(self, message, code = 0, response = None):
3+
self.message = message
4+
self.code = code
5+
self.response = response
6+
super().__init__(self.message)

appwrite/services/account.py

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
from ..service import Service
2+
3+
4+
class Account(Service):
5+
6+
def __init__(self, client):
7+
super(Account, self).__init__(client)
8+
9+
def get(self):
10+
"""Get Account"""
11+
12+
params = {}
13+
path = '/account'
14+
15+
return self.client.call('get', path, {
16+
'content-type': 'application/json',
17+
}, params)
18+
19+
def delete(self):
20+
"""Delete Account"""
21+
22+
params = {}
23+
path = '/account'
24+
25+
return self.client.call('delete', path, {
26+
'content-type': 'application/json',
27+
}, params)
28+
29+
def update_email(self, email, password):
30+
"""Update Account Email"""
31+
32+
params = {}
33+
path = '/account/email'
34+
params['email'] = email
35+
params['password'] = password
36+
37+
return self.client.call('patch', path, {
38+
'content-type': 'application/json',
39+
}, params)
40+
41+
def get_logs(self):
42+
"""Get Account Logs"""
43+
44+
params = {}
45+
path = '/account/logs'
46+
47+
return self.client.call('get', path, {
48+
'content-type': 'application/json',
49+
}, params)
50+
51+
def update_name(self, name):
52+
"""Update Account Name"""
53+
54+
params = {}
55+
path = '/account/name'
56+
params['name'] = name
57+
58+
return self.client.call('patch', path, {
59+
'content-type': 'application/json',
60+
}, params)
61+
62+
def update_password(self, password, old_password=''):
63+
"""Update Account Password"""
64+
65+
params = {}
66+
path = '/account/password'
67+
params['password'] = password
68+
params['oldPassword'] = old_password
69+
70+
return self.client.call('patch', path, {
71+
'content-type': 'application/json',
72+
}, params)
73+
74+
def get_prefs(self):
75+
"""Get Account Preferences"""
76+
77+
params = {}
78+
path = '/account/prefs'
79+
80+
return self.client.call('get', path, {
81+
'content-type': 'application/json',
82+
}, params)
83+
84+
def update_prefs(self, prefs):
85+
"""Update Account Preferences"""
86+
87+
params = {}
88+
path = '/account/prefs'
89+
params['prefs'] = prefs
90+
91+
return self.client.call('patch', path, {
92+
'content-type': 'application/json',
93+
}, params)
94+
95+
def create_recovery(self, email, url):
96+
"""Create Password Recovery"""
97+
98+
params = {}
99+
path = '/account/recovery'
100+
params['email'] = email
101+
params['url'] = url
102+
103+
return self.client.call('post', path, {
104+
'content-type': 'application/json',
105+
}, params)
106+
107+
def update_recovery(self, user_id, secret, password, password_again):
108+
"""Complete Password Recovery"""
109+
110+
params = {}
111+
path = '/account/recovery'
112+
params['userId'] = user_id
113+
params['secret'] = secret
114+
params['password'] = password
115+
params['passwordAgain'] = password_again
116+
117+
return self.client.call('put', path, {
118+
'content-type': 'application/json',
119+
}, params)
120+
121+
def get_sessions(self):
122+
"""Get Account Sessions"""
123+
124+
params = {}
125+
path = '/account/sessions'
126+
127+
return self.client.call('get', path, {
128+
'content-type': 'application/json',
129+
}, params)
130+
131+
def delete_sessions(self):
132+
"""Delete All Account Sessions"""
133+
134+
params = {}
135+
path = '/account/sessions'
136+
137+
return self.client.call('delete', path, {
138+
'content-type': 'application/json',
139+
}, params)
140+
141+
def delete_session(self, session_id):
142+
"""Delete Account Session"""
143+
144+
params = {}
145+
path = '/account/sessions/{sessionId}'
146+
path = path.replace('{sessionId}', session_id)
147+
148+
return self.client.call('delete', path, {
149+
'content-type': 'application/json',
150+
}, params)
151+
152+
def create_verification(self, url):
153+
"""Create Email Verification"""
154+
155+
params = {}
156+
path = '/account/verification'
157+
params['url'] = url
158+
159+
return self.client.call('post', path, {
160+
'content-type': 'application/json',
161+
}, params)
162+
163+
def update_verification(self, user_id, secret):
164+
"""Complete Email Verification"""
165+
166+
params = {}
167+
path = '/account/verification'
168+
params['userId'] = user_id
169+
params['secret'] = secret
170+
171+
return self.client.call('put', path, {
172+
'content-type': 'application/json',
173+
}, params)

appwrite/services/database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def get_collection(self, collection_id):
4545
'content-type': 'application/json',
4646
}, params)
4747

48-
def update_collection(self, collection_id, name, read, write, rules=[]):
48+
def update_collection(self, collection_id, name, read=[], write=[], rules=[]):
4949
"""Update Collection"""
5050

5151
params = {}
@@ -89,7 +89,7 @@ def list_documents(self, collection_id, filters=[], limit=25, offset=0, order_fi
8989
'content-type': 'application/json',
9090
}, params)
9191

92-
def create_document(self, collection_id, data, read, write, parent_document='', parent_property='', parent_property_type='assign'):
92+
def create_document(self, collection_id, data, read=[], write=[], parent_document='', parent_property='', parent_property_type='assign'):
9393
"""Create Document"""
9494

9595
params = {}
@@ -118,7 +118,7 @@ def get_document(self, collection_id, document_id):
118118
'content-type': 'application/json',
119119
}, params)
120120

121-
def update_document(self, collection_id, document_id, data, read, write):
121+
def update_document(self, collection_id, document_id, data, read=[], write=[]):
122122
"""Update Document"""
123123

124124
params = {}

0 commit comments

Comments
 (0)