Skip to content

Commit 38e762e

Browse files
authored
Merge pull request #80 from appwrite/dev
Dev
2 parents 1473969 + 99aa333 commit 38e762e

File tree

290 files changed

+4864
-1434
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

290 files changed

+4864
-1434
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2023 Appwrite (https://appwrite.io) and individual contributors.
1+
Copyright (c) 2024 Appwrite (https://appwrite.io) and individual contributors.
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

README.md

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

33
![License](https://img.shields.io/github/license/appwrite/sdk-for-python.svg?style=flat-square)
4-
![Version](https://img.shields.io/badge/api%20version-1.4.12-blue.svg?style=flat-square)
4+
![Version](https://img.shields.io/badge/api%20version-1.5.0-blue.svg?style=flat-square)
55
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
66
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
77
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
88

9-
**This SDK is compatible with Appwrite server version 1.4.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-python/releases).**
9+
**This SDK is compatible with Appwrite server version 1.5.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-python/releases).**
1010

1111
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. Use the Python SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)
1212

13-
![Appwrite](https://appwrite.io/images/github.png)
13+
![Appwrite](https://github.com/appwrite/appwrite/raw/main/public/images/github.png)
1414

1515
## Installation
1616

@@ -46,7 +46,7 @@ Once your SDK object is set, create any of the Appwrite service objects and choo
4646
```python
4747
users = Users(client)
4848

49-
result = users.create('[USER_ID]', 'email@example.com', 'password')
49+
result = users.create(ID.unique(), email = "email@example.com", phone = "+123456789", password = "password", name = "Walter O'Brien")
5050
```
5151

5252
### Full Example
@@ -66,7 +66,7 @@ client = Client()
6666

6767
users = Users(client)
6868

69-
result = users.create(ID.unique(), 'email@example.com', 'password')
69+
result = users.create(ID.unique(), email = "email@example.com", phone = "+123456789", password = "password", name = "Walter O'Brien")
7070
```
7171

7272
### Error Handling
@@ -75,7 +75,7 @@ The Appwrite Python SDK raises `AppwriteException` object with `message`, `code`
7575
```python
7676
users = Users(client)
7777
try:
78-
result = users.create(ID.unique(), 'email@example.com', 'password')
78+
result = users.create(ID.unique(), email = "email@example.com", phone = "+123456789", password = "password", name = "Walter O'Brien")
7979
except AppwriteException as e:
8080
print(e.message)
8181
```

appwrite/client.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import io
2-
import requests
2+
import json
33
import os
4+
import requests
45
from .input_file import InputFile
56
from .exception import AppwriteException
7+
from .encoders.value_class_encoder import ValueClassEncoder
68

79
class Client:
810
def __init__(self):
911
self._chunk_size = 5*1024*1024
1012
self._self_signed = False
11-
self._endpoint = 'https://HOSTNAME/v1'
13+
self._endpoint = 'https://cloud.appwrite.io/v1'
1214
self._global_headers = {
1315
'content-type': '',
14-
'user-agent' : 'AppwritePythonSDK/4.1.0 (${os.uname().sysname}; ${os.uname().version}; ${os.uname().machine})',
16+
'user-agent' : 'AppwritePythonSDK/5.0.0 (${os.uname().sysname}; ${os.uname().version}; ${os.uname().machine})',
1517
'x-sdk-name': 'Python',
1618
'x-sdk-platform': 'server',
1719
'x-sdk-language': 'python',
18-
'x-sdk-version': '4.1.0',
19-
'X-Appwrite-Response-Format' : '1.4.0',
20+
'x-sdk-version': '5.0.0',
21+
'X-Appwrite-Response-Format' : '1.5.0',
2022
}
2123

2224
def set_self_signed(self, status=True):
@@ -53,7 +55,19 @@ def set_locale(self, value):
5355
self._global_headers['x-appwrite-locale'] = value
5456
return self
5557

56-
def call(self, method, path='', headers=None, params=None):
58+
def set_session(self, value):
59+
"""The user session to authenticate with"""
60+
61+
self._global_headers['x-appwrite-session'] = value
62+
return self
63+
64+
def set_forwarded_user_agent(self, value):
65+
"""The user agent string of the client that made the request"""
66+
67+
self._global_headers['x-forwarded-user-agent'] = value
68+
return self
69+
70+
def call(self, method, path='', headers=None, params=None, response_type='json'):
5771
if headers is None:
5872
headers = {}
5973

@@ -63,7 +77,6 @@ def call(self, method, path='', headers=None, params=None):
6377
params = {k: v for k, v in params.items() if v is not None} # Remove None values from params dictionary
6478

6579
data = {}
66-
json = {}
6780
files = {}
6881
stringify = False
6982

@@ -74,8 +87,7 @@ def call(self, method, path='', headers=None, params=None):
7487
params = {}
7588

7689
if headers['content-type'].startswith('application/json'):
77-
json = data
78-
data = {}
90+
data = json.dumps(data, cls=ValueClassEncoder)
7991

8092
if headers['content-type'].startswith('multipart/form-data'):
8193
del headers['content-type']
@@ -84,23 +96,28 @@ def call(self, method, path='', headers=None, params=None):
8496
if isinstance(data[key], InputFile):
8597
files[key] = (data[key].filename, data[key].data)
8698
del data[key]
99+
data = self.flatten(data, stringify=stringify)
100+
87101
response = None
88102
try:
89103
response = requests.request( # call method dynamically https://stackoverflow.com/a/4246075/2299554
90104
method=method,
91105
url=self._endpoint + path,
92106
params=self.flatten(params, stringify=stringify),
93-
data=self.flatten(data),
94-
json=json,
107+
data=data,
95108
files=files,
96109
headers=headers,
97110
verify=(not self._self_signed),
111+
allow_redirects=False if response_type == 'location' else True
98112
)
99113

100114
response.raise_for_status()
101115

102116
content_type = response.headers['Content-Type']
103117

118+
if response_type == 'location':
119+
return response.headers.get('Location')
120+
104121
if content_type.startswith('application/json'):
105122
return response.json()
106123

appwrite/encoders/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import json
2+
from ..enums.authenticator_type import AuthenticatorType
3+
from ..enums.authentication_factor import AuthenticationFactor
4+
from ..enums.o_auth_provider import OAuthProvider
5+
from ..enums.browser import Browser
6+
from ..enums.credit_card import CreditCard
7+
from ..enums.flag import Flag
8+
from ..enums.relationship_type import RelationshipType
9+
from ..enums.relation_mutate import RelationMutate
10+
from ..enums.index_type import IndexType
11+
from ..enums.runtime import Runtime
12+
from ..enums.execution_method import ExecutionMethod
13+
from ..enums.name import Name
14+
from ..enums.smtp_encryption import SmtpEncryption
15+
from ..enums.compression import Compression
16+
from ..enums.image_gravity import ImageGravity
17+
from ..enums.image_format import ImageFormat
18+
from ..enums.password_hash import PasswordHash
19+
from ..enums.messaging_provider_type import MessagingProviderType
20+
21+
class ValueClassEncoder(json.JSONEncoder):
22+
def default(self, o):
23+
if isinstance(o, AuthenticatorType):
24+
return o.value
25+
26+
if isinstance(o, AuthenticationFactor):
27+
return o.value
28+
29+
if isinstance(o, OAuthProvider):
30+
return o.value
31+
32+
if isinstance(o, Browser):
33+
return o.value
34+
35+
if isinstance(o, CreditCard):
36+
return o.value
37+
38+
if isinstance(o, Flag):
39+
return o.value
40+
41+
if isinstance(o, RelationshipType):
42+
return o.value
43+
44+
if isinstance(o, RelationMutate):
45+
return o.value
46+
47+
if isinstance(o, IndexType):
48+
return o.value
49+
50+
if isinstance(o, Runtime):
51+
return o.value
52+
53+
if isinstance(o, ExecutionMethod):
54+
return o.value
55+
56+
if isinstance(o, Name):
57+
return o.value
58+
59+
if isinstance(o, SmtpEncryption):
60+
return o.value
61+
62+
if isinstance(o, Compression):
63+
return o.value
64+
65+
if isinstance(o, ImageGravity):
66+
return o.value
67+
68+
if isinstance(o, ImageFormat):
69+
return o.value
70+
71+
if isinstance(o, PasswordHash):
72+
return o.value
73+
74+
if isinstance(o, MessagingProviderType):
75+
return o.value
76+
77+
return super().default(o)

appwrite/enums/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from enum import Enum
2+
3+
class AuthenticationFactor(Enum):
4+
EMAIL = "email"
5+
PHONE = "phone"
6+
TOTP = "totp"
7+
RECOVERYCODE = "recoverycode"

appwrite/enums/authenticator_type.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from enum import Enum
2+
3+
class AuthenticatorType(Enum):
4+
TOTP = "totp"

appwrite/enums/browser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from enum import Enum
2+
3+
class Browser(Enum):
4+
AVANT_BROWSER = "aa"
5+
ANDROID_WEBVIEW_BETA = "an"
6+
GOOGLE_CHROME = "ch"
7+
GOOGLE_CHROME_IOS = "ci"
8+
GOOGLE_CHROME_MOBILE = "cm"
9+
CHROMIUM = "cr"
10+
MOZILLA_FIREFOX = "ff"
11+
SAFARI = "sf"
12+
MOBILE_SAFARI = "mf"
13+
MICROSOFT_EDGE = "ps"
14+
MICROSOFT_EDGE_IOS = "oi"
15+
OPERA_MINI = "om"
16+
OPERA = "op"
17+
OPERA_NEXT = "on"

appwrite/enums/compression.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from enum import Enum
2+
3+
class Compression(Enum):
4+
NONE = "none"
5+
GZIP = "gzip"
6+
ZSTD = "zstd"

0 commit comments

Comments
 (0)