-
Notifications
You must be signed in to change notification settings - Fork 95
A few improvements #111
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
A few improvements #111
Changes from all commits
866db9f
474adfb
87cfac9
06a52b8
95ad17f
3f98d27
b6351d4
517a24d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,7 +239,7 @@ def __init__(self, url, connection, handler, headers=None): | |
self._connection = connection | ||
self._url = url | ||
self._handler = handler | ||
self._headers = headers | ||
self._headers = headers or dict() | ||
self._logger = logging.getLogger(LOGGER_NAME) | ||
|
||
@property | ||
|
@@ -272,6 +272,12 @@ def get_headers(self): | |
# pylint: disable=no-self-use | ||
return None | ||
|
||
def add_headers(self, value): | ||
if not isinstance(value, dict): | ||
raise TypeError("Headers must be of type 'dict' not {}".format(type(value))) | ||
|
||
self._headers.update(value) | ||
|
||
def execute(self): | ||
"""Fetches HTTP response and returns processed result | ||
|
||
|
@@ -284,7 +290,7 @@ def execute(self): | |
# pylint: disable=assignment-from-none | ||
body = self.get_body() | ||
|
||
headers = {} if self._headers is None else self._headers | ||
headers = self._headers | ||
|
||
# pylint: disable=assignment-from-none | ||
extra_headers = self.get_headers() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you keep the get_headers method as it is this patch, then we don't need the line 287 at all. But I would rather keep the line 287 and remove all occurences of slef.headers from the redefined get_headers. I don't think its a good idea to force descendant classes to use the headers class member to return the correct headers - this is up to the parent class. |
||
|
@@ -351,7 +357,7 @@ def get_path(self): | |
return self._entity_set_proxy.last_segment + self._entity_key.to_key_string() | ||
|
||
def get_headers(self): | ||
return {'Accept': 'application/json'} | ||
return {'Accept': 'application/json', **self._headers} | ||
|
||
def get_query_params(self): | ||
qparams = super(EntityGetRequest, self).get_query_params() | ||
|
@@ -448,7 +454,7 @@ def get_body(self): | |
return json.dumps(self._get_body()) | ||
|
||
def get_headers(self): | ||
return {'Accept': 'application/json', 'Content-Type': 'application/json', 'X-Requested-With': 'X'} | ||
return {'Accept': 'application/json', 'Content-Type': 'application/json', 'X-Requested-With': 'X', **self._headers} | ||
|
||
@staticmethod | ||
def _build_values(entity_type, entity): | ||
|
@@ -512,13 +518,18 @@ class EntityModifyRequest(ODataHttpRequest): | |
Call execute() to send the update-request to the OData service | ||
and get the modified entity.""" | ||
|
||
def __init__(self, url, connection, handler, entity_set, entity_key): | ||
def __init__(self, url, connection, handler, entity_set, entity_key, method="PATCH"): | ||
super(EntityModifyRequest, self).__init__(url, connection, handler) | ||
self._logger = logging.getLogger(LOGGER_NAME) | ||
self._entity_set = entity_set | ||
self._entity_type = entity_set.entity_type | ||
self._entity_key = entity_key | ||
|
||
if method.upper() not in ["PATCH", "PUT"]: | ||
raise ValueError("Method must be either PATCH or PUT") | ||
|
||
self._method = method | ||
|
||
self._values = {} | ||
|
||
# get all properties declared by entity type | ||
|
@@ -531,7 +542,7 @@ def get_path(self): | |
|
||
def get_method(self): | ||
# pylint: disable=no-self-use | ||
return 'PATCH' | ||
return self._method | ||
|
||
def get_body(self): | ||
# pylint: disable=no-self-use | ||
|
@@ -541,7 +552,7 @@ def get_body(self): | |
return json.dumps(body) | ||
|
||
def get_headers(self): | ||
return {'Accept': 'application/json', 'Content-Type': 'application/json'} | ||
return {'Accept': 'application/json', 'Content-Type': 'application/json', **self._headers} | ||
|
||
def set(self, **kwargs): | ||
"""Set properties to be changed.""" | ||
|
@@ -639,6 +650,7 @@ def get_headers(self): | |
|
||
return { | ||
'Accept': 'application/json', | ||
**self._headers | ||
} | ||
|
||
def get_query_params(self): | ||
|
@@ -699,6 +711,7 @@ def get_method(self): | |
def get_headers(self): | ||
return { | ||
'Accept': 'application/json', | ||
**self._headers | ||
} | ||
|
||
|
||
|
@@ -956,6 +969,18 @@ def __eq__(self, value): | |
def __ne__(self, value): | ||
return GetEntitySetFilter.format_filter(self._proprty, 'ne', value) | ||
|
||
def __lt__(self, value): | ||
return GetEntitySetFilter.format_filter(self._proprty, 'lt', value) | ||
|
||
def __le__(self, value): | ||
return GetEntitySetFilter.format_filter(self._proprty, 'le', value) | ||
|
||
def __ge__(self, value): | ||
return GetEntitySetFilter.format_filter(self._proprty, 'ge', value) | ||
|
||
def __gt__(self, value): | ||
return GetEntitySetFilter.format_filter(self._proprty, 'gt', value) | ||
|
||
|
||
class GetEntitySetRequest(QueryRequest): | ||
"""GET on EntitySet""" | ||
|
@@ -1140,7 +1165,7 @@ def create_entity_handler(response): | |
return EntityCreateRequest(self._service.url, self._service.connection, create_entity_handler, self._entity_set, | ||
self.last_segment) | ||
|
||
def update_entity(self, key=None, **kwargs): | ||
def update_entity(self, key=None, method="PATCH", **kwargs): | ||
"""Updates an existing entity in the given entity-set.""" | ||
|
||
def update_entity_handler(response): | ||
|
@@ -1158,7 +1183,7 @@ def update_entity_handler(response): | |
self._logger.info('Updating entity %s for key %s and args %s', self._entity_set.entity_type.name, key, kwargs) | ||
|
||
return EntityModifyRequest(self._service.url, self._service.connection, update_entity_handler, self._entity_set, | ||
entity_key) | ||
entity_key, method=method) | ||
|
||
def delete_entity(self, key: EntityKey = None, **kwargs): | ||
"""Delete the entity""" | ||
|
@@ -1433,7 +1458,7 @@ def get_boundary(self): | |
|
||
def get_headers(self): | ||
# pylint: disable=no-self-use | ||
return {'Content-Type': 'multipart/mixed;boundary={}'.format(self.get_boundary())} | ||
return {'Content-Type': 'multipart/mixed;boundary={}'.format(self.get_boundary()), **self._headers} | ||
|
||
def get_body(self): | ||
return encode_multipart(self.get_boundary(), self.requests) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry but I cannot see any code for this last changelog entry.