Skip to content
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

refactor: added type hints for Python SDK #665

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
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
24 changes: 12 additions & 12 deletions templates/python/package/client.py.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import io
import requests
import os
from .input_file import InputFile
from .exception import {{spec.title | caseUcfirst}}Exception
from typing import Optional

class Client:
def __init__(self):
Expand All @@ -21,20 +21,20 @@ class Client:
{% endfor %}
}

def set_self_signed(self, status=True):
def set_self_signed(self, status: bool = True):
self._self_signed = status
return self

def set_endpoint(self, endpoint):
def set_endpoint(self, endpoint: str):
self._endpoint = endpoint
return self

def add_header(self, key, value):
def add_header(self, key: str, value: str):
self._global_headers[key.lower()] = value
return self
{% for header in spec.global.headers %}

def set_{{header.key | caseSnake}}(self, value):
def set_{{header.key | caseSnake}}(self, value: str):
{% if header.description %}
"""{{header.description}}"""

Expand All @@ -43,7 +43,7 @@ class Client:
return self
{% endfor %}

def call(self, method, path='', headers=None, params=None):
def call(self, method: str, path: str = '', headers: Optional[dict] = None, params: Optional[dict] = None):
if headers is None:
headers = {}

Expand Down Expand Up @@ -105,12 +105,12 @@ class Client:

def chunked_upload(
self,
path,
headers = None,
params = None,
param_name = '',
path: str,
headers: Optional[dict] = None,
params: Optional[dict] = None,
param_name: str = '',
on_progress = None,
upload_id = ''
upload_id: str = ''
):
input_file = params[param_name]

Expand Down Expand Up @@ -186,7 +186,7 @@ class Client:

return result

def flatten(self, data, prefix='', stringify=False):
def flatten(self, data: dict, prefix: str = '', stringify: bool = False):
output = {}
i = 0

Expand Down
6 changes: 4 additions & 2 deletions templates/python/package/exception.py.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Optional

class {{spec.title | caseUcfirst}}Exception(Exception):
def __init__(self, message, code = 0, type = None, response = None):
def __init__(self, message: str, code: int = 0, type: Optional[str] = None, response: Optional[dict] = None):
self.message = message
self.code = code
self.type = type
self.response = response
super().__init__(self.message)
super().__init__(self.message)
2 changes: 1 addition & 1 deletion templates/python/package/id.py.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ class ID:

@staticmethod
def unique():
return 'unique()'
return 'unique()'
7 changes: 4 additions & 3 deletions templates/python/package/input_file.py.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import mimetypes
from typing import Optional

class InputFile:
@classmethod
def from_path(cls, path):
def from_path(cls, path: str) -> 'InputFile':
instance = cls()
instance.path = path
instance.filename = os.path.basename(path)
Expand All @@ -12,10 +13,10 @@ class InputFile:
return instance

@classmethod
def from_bytes(cls, bytes, filename = None, mime_type = None):
def from_bytes(cls, bytes, filename: Optional[str] = None, mime_type: Optional[str] = None) -> 'InputFile':
instance = cls()
instance.data = bytes
instance.filename = filename
instance.mime_type = mime_type
instance.source_type = 'bytes'
return instance
return instance
10 changes: 5 additions & 5 deletions templates/python/package/permission.py.twig
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
class Permission:

@staticmethod
def read(role):
def read(role) -> str:
return f'read("{role}")'

@staticmethod
def write(role):
def write(role) -> str:
return f'write("{role}")'

@staticmethod
def create(role):
def create(role) -> str:
return f'create("{role}")'

@staticmethod
def update(role):
def update(role) -> str:
return f'update("{role}")'

@staticmethod
def delete(role):
def delete(role) -> str:
return f'delete("{role}")'
44 changes: 22 additions & 22 deletions templates/python/package/query.py.twig
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
class Query:
@staticmethod
def equal(attribute, value):
def equal(attribute, value) -> str:
return Query.add_query(attribute, "equal", value)

@staticmethod
def not_equal(attribute, value):
def not_equal(attribute, value) -> str:
return Query.add_query(attribute, "notEqual", value)

@staticmethod
def less_than(attribute, value):
def less_than(attribute, value) -> str:
return Query.add_query(attribute, "lessThan", value)

@staticmethod
def less_than_equal(attribute, value):
def less_than_equal(attribute, value) -> str:
return Query.add_query(attribute, "lessThanEqual", value)

@staticmethod
def greater_than(attribute, value):
def greater_than(attribute, value) -> str:
return Query.add_query(attribute, "greaterThan", value)

@staticmethod
def greater_than_equal(attribute, value):
def greater_than_equal(attribute, value) -> str:
return Query.add_query(attribute, "greaterThanEqual", value)

@staticmethod
def is_null(attribute):
def is_null(attribute) -> str:
return f'isNull("{attribute}")'

@staticmethod
def is_not_null(attribute):
def is_not_null(attribute) -> str:
return f'isNotNull("{attribute}")'

@staticmethod
def between(attribute, start, end):
def between(attribute, start, end) -> str:
return Query.add_query(attribute, "between", [start, end])

@staticmethod
def starts_with(attribute, value):
def starts_with(attribute, value) -> str:
return Query.add_query(attribute, "startsWith", value)

@staticmethod
def ends_with(attribute, value):
def ends_with(attribute, value) -> str:
return Query.add_query(attribute, "endsWith", value)

@staticmethod
def select(attributes):
def select(attributes) -> str:
return f'select([{",".join(map(Query.parseValues, attributes))}])'

@staticmethod
def search(attribute, value):
def search(attribute, value) -> str:
return Query.add_query(attribute, "search", value)

@staticmethod
def order_asc(attribute):
def order_asc(attribute) -> str:
return f'orderAsc("{attribute}")'

@staticmethod
def order_desc(attribute):
def order_desc(attribute) -> str:
return f'orderDesc("{attribute}")'

@staticmethod
def cursor_before(id):
def cursor_before(id) -> str:
return f'cursorBefore("{id}")'

@staticmethod
def cursor_after(id):
def cursor_after(id) -> str:
return f'cursorAfter("{id}")'

@staticmethod
def limit(limit):
def limit(limit) -> str:
return f'limit({limit})'

@staticmethod
def offset(offset):
def offset(offset) -> str:
return f'offset({offset})'

@staticmethod
def add_query(attribute, method, value):
def add_query(attribute, method, value) -> str:
if type(value) == list:
return f'{method}("{attribute}", [{",".join(map(Query.parseValues, value))}])'
else:
return f'{method}("{attribute}", [{Query.parseValues(value)}])'

@staticmethod
def parseValues(value):
def parseValues(value) -> str:
if type(value) == str:
return f'"{value}"'
elif type(value) == bool:
return str(value).lower()
else:
return str(value)
return str(value)
14 changes: 7 additions & 7 deletions templates/python/package/role.py.twig
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
class Role:
@staticmethod
def any():
def any() -> str:
return 'any'

@staticmethod
def user(id, status = ""):
def user(id, status: str = "") -> str:
if status:
return f'user:{id}/{status}'
return f'user:{id}'

@staticmethod
def users(status = ""):
def users(status: str = "") -> str:
if status:
return f'users/{status}'
return 'users'

@staticmethod
def guests():
def guests() -> str:
return 'guests'

@staticmethod
def team(id, role = ""):
def team(id, role: str = "") -> str:
if role:
return f'team:{id}/{role}'
return f'team:{id}'

@staticmethod
def member(id):
return f'member:{id}'
def member(id) -> str:
return f'member:{id}'