Skip to content

Commit 3dc5325

Browse files
authored
Merge pull request #129 from lohanidamodar/feat-python-exception
Feat python exception
2 parents aeb3009 + e5f59a8 commit 3dc5325

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

src/SDK/Language/Python.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ public function getFiles()
146146
'template' => '/python/package/client.py.twig',
147147
'minify' => false,
148148
],
149+
[
150+
'scope' => 'default',
151+
'destination' => '{{ spec.title | caseSnake}}/exception.py',
152+
'template' => '/python/package/exception.py.twig',
153+
'minify' => false,
154+
],
149155
[
150156
'scope' => 'default',
151157
'destination' => '{{ spec.title | caseSnake}}/service.py',

templates/python/package/client.py.twig

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io
22
import requests
3+
from .exception import {{spec.title | caseUcfirst}}Exception
34

45
class Client:
56
def __init__(self):
@@ -63,26 +64,32 @@ class Client:
6364
if isinstance(data[key], io.BufferedIOBase):
6465
files[key] = data[key]
6566
del data[key]
66-
67-
response = requests.request( # call method dynamically https://stackoverflow.com/a/4246075/2299554
68-
method=method,
69-
url=self._endpoint + path,
70-
params=self.flatten(params),
71-
data=self.flatten(data),
72-
json=json,
73-
files=files,
74-
headers=headers,
75-
verify=self._self_signed,
76-
)
77-
78-
response.raise_for_status()
79-
80-
content_type = response.headers['Content-Type']
81-
82-
if content_type.startswith('application/json'):
83-
return response.json()
84-
85-
return response._content
67+
response = None
68+
try:
69+
response = requests.request( # call method dynamically https://stackoverflow.com/a/4246075/2299554
70+
method=method,
71+
url=self._endpoint + path,
72+
params=self.flatten(params),
73+
data=self.flatten(data),
74+
json=json,
75+
files=files,
76+
headers=headers,
77+
verify=self._self_signed,
78+
)
79+
80+
response.raise_for_status()
81+
82+
content_type = response.headers['Content-Type']
83+
84+
if content_type.startswith('application/json'):
85+
return response.json()
86+
87+
return response._content
88+
except Exception as e:
89+
if response.json():
90+
raise {{spec.title | caseUcfirst}}Exception(response.json()['message'], response.status_code, response.json())
91+
else:
92+
raise {{spec.title | caseUcfirst}}Exception(e)
8693

8794
def flatten(self, data, prefix=''):
8895
output = {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class {{spec.title | caseUcfirst}}Exception(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)

tests/SDKTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class SDKTest extends TestCase
158158
// 'python-3.2' => 'docker run --rm -v $(pwd):/app -w /app --env PIP_TARGET=tests/sdks/python/vendor --env PYTHONPATH=tests/sdks/python/vendor python:3.2 python tests/sdks/python/test.py',
159159
// 'python-3.1' => 'docker run --rm -v $(pwd):/app -w /app --env PIP_TARGET=tests/sdks/python/vendor --env PYTHONPATH=tests/sdks/python/vendor python:3.1 python tests/sdks/python/test.py',
160160
],
161-
'supportException' => false,
161+
'supportException' => true,
162162
],
163163
];
164164

tests/languages/python/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from appwrite.services.foo import Foo
33
from appwrite.services.bar import Bar
44
from appwrite.services.general import General
5+
from appwrite.exception import AppwriteException
56
import os.path
67

78

@@ -52,3 +53,13 @@
5253

5354
response = general.upload('string', 123, ['string in array'], open('./tests/resources/file.png', 'rb'))
5455
print(response['result'])
56+
57+
try:
58+
response = general.error400()
59+
except AppwriteException as e:
60+
print(e.message)
61+
62+
try:
63+
response = general.error500()
64+
except AppwriteException as e:
65+
print(e.message)

0 commit comments

Comments
 (0)