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

Feature/auto code-formatting #540

Closed
wants to merge 3 commits into from
Closed
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
57 changes: 32 additions & 25 deletions ably/http/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def reauth_if_expired(func):
@functools.wraps(func)
async def wrapper(rest, *args, **kwargs):
if kwargs.get("skip_auth"):
if kwargs.get('skip_auth'):
return await func(rest, *args, **kwargs)

# RSA4b1 Detect expired token to avoid round-trip request
Expand All @@ -44,8 +44,9 @@ async def wrapper(rest, *args, **kwargs):


class Request:
def __init__(self, method='GET', url='/', version=None, headers=None, body=None,
skip_auth=False, raise_on_error=True):
def __init__(
self, method='GET', url='/', version=None, headers=None, body=None, skip_auth=False, raise_on_error=True
):
self.__method = method
self.__headers = headers or {}
self.__body = body
Expand All @@ -56,8 +57,9 @@ def __init__(self, method='GET', url='/', version=None, headers=None, body=None,

def with_relative_url(self, relative_url):
url = urljoin(self.url, relative_url)
return Request(self.method, url, self.version, self.headers, self.body,
self.skip_auth, self.raise_on_error)
return Request(
self.method, url, self.version, self.headers, self.body, self.skip_auth, self.raise_on_error
)

@property
def method(self):
Expand Down Expand Up @@ -104,7 +106,7 @@ def to_native(self):
elif content_type.startswith('application/json'):
return self.__response.json()

raise ValueError("Unsupported content type")
raise ValueError('Unsupported content type')

@property
def response(self):
Expand Down Expand Up @@ -157,8 +159,17 @@ def get_rest_hosts(self):
return hosts

@reauth_if_expired
async def make_request(self, method, path, version=None, headers=None, body=None,
skip_auth=False, timeout=None, raise_on_error=True):
async def make_request(
self,
method,
path,
version=None,
headers=None,
body=None,
skip_auth=False,
timeout=None,
raise_on_error=True,
):

if body is not None and type(body) not in (bytes, str):
body = self.dump_body(body)
Expand All @@ -172,10 +183,7 @@ async def make_request(self, method, path, version=None, headers=None, body=None

if not skip_auth:
if self.auth.auth_mechanism == Auth.Method.BASIC and self.preferred_scheme.lower() == 'http':
raise AblyException(
"Cannot use Basic Auth over non-TLS connections",
401,
40103)
raise AblyException('Cannot use Basic Auth over non-TLS connections', 401, 40103)
auth_headers = await self.auth._get_auth_headers()
all_headers.update(auth_headers)
if headers:
Expand All @@ -187,9 +195,7 @@ async def make_request(self, method, path, version=None, headers=None, body=None

hosts = self.get_rest_hosts()
for retry_count, host in enumerate(hosts):
base_url = "%s://%s:%d" % (self.preferred_scheme,
host,
self.preferred_port)
base_url = '%s://%s:%d' % (self.preferred_scheme, host, self.preferred_port)
url = urljoin(base_url, path)

request = self.__client.build_request(
Expand Down Expand Up @@ -228,28 +234,29 @@ async def make_request(self, method, path, version=None, headers=None, body=None
raise e

async def delete(self, url, headers=None, skip_auth=False, timeout=None):
result = await self.make_request('DELETE', url, headers=headers,
skip_auth=skip_auth, timeout=timeout)
result = await self.make_request('DELETE', url, headers=headers, skip_auth=skip_auth, timeout=timeout)
return result

async def get(self, url, headers=None, skip_auth=False, timeout=None):
result = await self.make_request('GET', url, headers=headers,
skip_auth=skip_auth, timeout=timeout)
result = await self.make_request('GET', url, headers=headers, skip_auth=skip_auth, timeout=timeout)
return result

async def patch(self, url, headers=None, body=None, skip_auth=False, timeout=None):
result = await self.make_request('PATCH', url, headers=headers, body=body,
skip_auth=skip_auth, timeout=timeout)
result = await self.make_request(
'PATCH', url, headers=headers, body=body, skip_auth=skip_auth, timeout=timeout
)
return result

async def post(self, url, headers=None, body=None, skip_auth=False, timeout=None):
result = await self.make_request('POST', url, headers=headers, body=body,
skip_auth=skip_auth, timeout=timeout)
result = await self.make_request(
'POST', url, headers=headers, body=body, skip_auth=skip_auth, timeout=timeout
)
return result

async def put(self, url, headers=None, body=None, skip_auth=False, timeout=None):
result = await self.make_request('PUT', url, headers=headers, body=body,
skip_auth=skip_auth, timeout=timeout)
result = await self.make_request(
'PUT', url, headers=headers, body=body, skip_auth=skip_auth, timeout=timeout
)
return result

@property
Expand Down
20 changes: 10 additions & 10 deletions ably/http/httputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@


class HttpUtils:
default_format = "json"
default_format = 'json'

mime_types = {
"json": "application/json",
"xml": "application/xml",
"html": "text/html",
"binary": "application/x-msgpack",
'json': 'application/json',
'xml': 'application/xml',
'html': 'text/html',
'binary': 'application/x-msgpack',
}

@staticmethod
def default_get_headers(binary=False, version=None):
headers = HttpUtils.default_headers(version=version)
if binary:
headers["Accept"] = HttpUtils.mime_types['binary']
headers['Accept'] = HttpUtils.mime_types['binary']
else:
headers["Accept"] = HttpUtils.mime_types['json']
headers['Accept'] = HttpUtils.mime_types['json']
return headers

@staticmethod
def default_post_headers(binary=False, version=None):
headers = HttpUtils.default_get_headers(binary=binary, version=version)
headers["Content-Type"] = headers["Accept"]
headers['Content-Type'] = headers['Accept']
return headers

@staticmethod
Expand All @@ -41,8 +41,8 @@ def default_headers(version=None):
if version is None:
version = ably.api_version
return {
"X-Ably-Version": version,
"Ably-Agent": 'ably-python/%s python/%s' % (ably.lib_version, platform.python_version())
'X-Ably-Version': version,
'Ably-Agent': 'ably-python/%s python/%s' % (ably.lib_version, platform.python_version()),
}

@staticmethod
Expand Down
47 changes: 32 additions & 15 deletions ably/http/paginatedresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def format_params(params=None, direction=None, start=None, end=None, limit=None,
params['end'] = format_time_param(end)
if limit:
if limit > 1000:
raise ValueError("The maximum allowed limit is 1000")
raise ValueError('The maximum allowed limit is 1000')
params['limit'] = '%d' % limit

if 'start' in params and 'end' in params and params['start'] > params['end']:
Expand All @@ -42,8 +42,7 @@ def format_params(params=None, direction=None, start=None, end=None, limit=None,


class PaginatedResult:
def __init__(self, http, items, content_type, rel_first, rel_next,
response_processor, response):
def __init__(self, http, items, content_type, rel_first, rel_next, response_processor, response):
self.__http = http
self.__items = items
self.__content_type = content_type
Expand Down Expand Up @@ -77,21 +76,40 @@ async def __get_rel(self, rel_req):
return await self.paginated_query_with_request(self.__http, rel_req, self.__response_processor)

@classmethod
async def paginated_query(cls, http, method='GET', url='/', version=None, body=None,
headers=None, response_processor=None,
raise_on_error=True):
async def paginated_query(
cls,
http,
method='GET',
url='/',
version=None,
body=None,
headers=None,
response_processor=None,
raise_on_error=True,
):
headers = headers or {}
req = Request(method, url, version=version, body=body, headers=headers, skip_auth=False,
raise_on_error=raise_on_error)
req = Request(
method,
url,
version=version,
body=body,
headers=headers,
skip_auth=False,
raise_on_error=raise_on_error,
)
return await cls.paginated_query_with_request(http, req, response_processor)

@classmethod
async def paginated_query_with_request(cls, http, request, response_processor,
raise_on_error=True):
async def paginated_query_with_request(cls, http, request, response_processor, raise_on_error=True):
response = await http.make_request(
request.method, request.url, version=request.version,
headers=request.headers, body=request.body,
skip_auth=request.skip_auth, raise_on_error=request.raise_on_error)
request.method,
request.url,
version=request.version,
headers=request.headers,
body=request.body,
skip_auth=request.skip_auth,
raise_on_error=request.raise_on_error,
)

items = response_processor(response)

Expand All @@ -107,8 +125,7 @@ async def paginated_query_with_request(cls, http, request, response_processor,
else:
next_rel_request = None

return cls(http, items, content_type, first_rel_request,
next_rel_request, response_processor, response)
return cls(http, items, content_type, first_rel_request, next_rel_request, response_processor, response)


class HttpPaginatedResponse(PaginatedResult):
Expand Down
Loading
Loading