Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_entity_get_value_without_proxy(service):
"""Check getting $value without proxy"""
# pylint: disable=redefined-outer-name
responses.add(
responses.GET,
"{0}/CarIDPics('Hadraplan')/$value/".format(service.url),
headers={'Content-type': 'application/jpeg'},
body='DEADBEAF',
status=200)
request = service.entity_sets.CarIDPics.get_entity('Hadraplan').get_value()
assert isinstance(request, pyodata.v2.service.ODataHttpRequest)
stream = request.execute()
assert stream.content == b'DEADBEAF'
self.get_method(), url, headers=headers, params=self.get_query_params(), data=body)
self._logger.debug('Received response')
self._logger.debug(' url: %s', response.url)
self._logger.debug(' headers: %s', response.headers)
self._logger.debug(' status code: %d', response.status_code)
try:
self._logger.debug(' body: %s', response.content.decode('utf-8'))
except UnicodeDecodeError:
self._logger.debug(' body: ')
return self._handler(response)
class EntityGetRequest(ODataHttpRequest):
"""Used for GET operations of a single entity"""
def __init__(self, handler, entity_key, entity_set_proxy):
super(EntityGetRequest, self).__init__(entity_set_proxy.service.url, entity_set_proxy.service.connection,
handler)
self._logger = logging.getLogger(LOGGER_NAME)
self._entity_key = entity_key
self._entity_set_proxy = entity_set_proxy
self._select = None
self._expand = None
self._logger.debug('New instance of EntityGetRequest for last segment: %s', self._entity_set_proxy.last_segment)
def nav(self, nav_property):
"""Navigates to given navigation property and returns the EntitySetProxy"""
return self._entity_set_proxy.nav(nav_property, self._entity_key)
stream_handler)
class NavEntityGetRequest(EntityGetRequest):
"""Used for GET operations of a single entity accessed via a Navigation property"""
def __init__(self, handler, master_key, entity_set_proxy, nav_property):
super(NavEntityGetRequest, self).__init__(handler, master_key, entity_set_proxy)
self._nav_property = nav_property
def get_path(self):
return "{}/{}".format(super(NavEntityGetRequest, self).get_path(), self._nav_property)
class EntityCreateRequest(ODataHttpRequest):
"""Used for creating entities (POST operations of a single entity)
Call execute() to send the create-request to the OData service
and get the newly created entity."""
def __init__(self, url, connection, handler, entity_set, last_segment=None):
super(EntityCreateRequest, self).__init__(url, connection, handler)
self._logger = logging.getLogger(LOGGER_NAME)
self._entity_set = entity_set
self._entity_type = entity_set.entity_type
if last_segment is None:
self._last_segment = self._entity_set.name
else:
self._last_segment = last_segment
self._logger.info(kwargs)
for key, val in kwargs.items():
try:
val = self._entity_type.proprty(key).typ.traits.to_json(val)
except KeyError:
raise PyODataException(
'Property {} is not declared in {} entity type'.format(key, self._entity_type.name))
self._values[key] = val
return self
class QueryRequest(ODataHttpRequest):
"""INTERFACE A consumer-side query-request builder. Call execute() to issue the request."""
# pylint: disable=too-many-instance-attributes
def __init__(self, url, connection, handler, last_segment):
super(QueryRequest, self).__init__(url, connection, handler)
self._logger = logging.getLogger(LOGGER_NAME)
self._count = None
self._top = None
self._skip = None
self._order_by = None
self._filter = None
self._select = None
self._expand = None
self._last_segment = last_segment
if isinstance(req, MultipartRequest):
raise PyODataException('Changeset cannot contain nested multipart content')
# part represents single request, we have to parse
# content (without checking Content type for binary/http)
response = ODataHttpResponse.from_string(part[0])
result.append(req.handler(response))
return result
return Changeset(self._url, self._connection, changeset_handler, changeset_id)
class MultipartRequest(ODataHttpRequest):
"""HTTP Batch request"""
def __init__(self, url, connection, handler, request_id=None):
super(MultipartRequest, self).__init__(url, connection, partial(MultipartRequest.http_response_handler, self))
self.requests = []
self._handler_decoded = handler
# generate random id of form dddd-dddd-dddd
# pylint: disable=invalid-name
self.id = request_id if request_id is not None else '{}_{}_{}'.format(
random.randint(1000, 9999), random.randint(1000, 9999), random.randint(1000, 9999))
self._logger.debug('New multipart %s request initialized, id=%s', self.__class__.__name__, self.id)
@property
def http_get_odata(self, path, handler, connection=None):
"""HTTP GET request proxy for the passed path in the service"""
conn = connection
if conn is None:
conn = self._connection
return ODataHttpRequest(
urljoin(self._url, path),
conn,
handler,
headers={'Accept': 'application/json'})
super(EntityDeleteRequest, self).__init__(url, connection, handler)
self._logger = logging.getLogger(LOGGER_NAME)
self._entity_set = entity_set
self._entity_key = entity_key
self._logger.debug('New instance of EntityDeleteRequest for entity type: %s', entity_set.entity_type.name)
def get_path(self):
return self._entity_set.name + self._entity_key.to_key_string()
def get_method(self):
# pylint: disable=no-self-use
return 'DELETE'
class EntityModifyRequest(ODataHttpRequest):
"""Used for modyfing entities (UPDATE/MERGE operations on a single entity)
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):
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
self._values = {}
# get all properties declared by entity type
self._type_props = self._entity_type.proprties()
values[key] = val
return values
def set(self, **kwargs):
"""Set properties on the new entity."""
self._logger.info(kwargs)
# TODO: consider use of attset for setting properties
self._values = EntityCreateRequest._build_values(self._entity_type, kwargs)
return self
class EntityDeleteRequest(ODataHttpRequest):
"""Used for deleting entity (DELETE operations on a single entity)"""
def __init__(self, url, connection, handler, entity_set, entity_key):
super(EntityDeleteRequest, self).__init__(url, connection, handler)
self._logger = logging.getLogger(LOGGER_NAME)
self._entity_set = entity_set
self._entity_key = entity_key
self._logger.debug('New instance of EntityDeleteRequest for entity type: %s', entity_set.entity_type.name)
def get_path(self):
return self._entity_set.name + self._entity_key.to_key_string()
def get_method(self):
# pylint: disable=no-self-use
return 'DELETE'
def get_value(self, connection=None):
"""Returns Value of Media EntityTypes also known as the $value URL suffix."""
if connection is None:
connection = self._connection
def stream_handler(response):
"""Returns $value from HTTP Response"""
if response.status_code != requests.codes.ok:
raise HttpError('HTTP GET for $value failed with status code {}'
.format(response.status_code), response)
return response
return ODataHttpRequest(
urljoin(self._url, self.get_path(), '/$value'),
connection,
stream_handler)