-
Notifications
You must be signed in to change notification settings - Fork 95
Add async networking libraries support #210
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
543d175
feat: add support for async http library aiohttp
mnunzio 00ad1dc
Merge pull request #1 from mnunzio/async_python_no_duplicate_code
Albo90 e5b6ec1
Merge pull request #2 from mnunzio/async_python_no_duplicate_code
Albo90 5f88377
Added: prefix async getattr, atom response
Albo90 a4cc84d
added tests
Albo90 30470be
updated dev-requirements.txt
Albo90 c2d662a
feat: update async_client tests
mnunzio 7892057
Merge pull request #3 from mnunzio/no_duplication_v1
Albo90 398dd24
feat: update dev-requirements.txt
mnunzio bad2e41
Merge pull request #4 from mnunzio/no_duplication_v1
Albo90 9ceb3dd
feat: clean useless code
mnunzio cad0c92
Merge branch 'Albo90:no_duplication_v1' into no_duplication_v1
mnunzio 3bab585
Merge pull request #5 from mnunzio/no_duplication_v1
Albo90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Utility class to standardize response | ||
|
||
Author: Alberto Moio <email Alberto>, Nunzio Mauro <mnunzio90@gmail.com> | ||
Date: 2017-08-21 | ||
""" | ||
import json | ||
|
||
|
||
class Response: | ||
"""Representation of http response in a standard form already used by handlers""" | ||
|
||
__attrs__ = [ | ||
'content', 'status_code', 'headers', 'url' | ||
] | ||
|
||
def __init__(self): | ||
self.status_code = None | ||
self.headers = None | ||
self.url = None | ||
self.content = None | ||
|
||
@property | ||
def text(self): | ||
"""Textual representation of response content""" | ||
|
||
return self.content.decode('utf-8') | ||
|
||
def json(self): | ||
"""JSON representation of response content""" | ||
|
||
return json.loads(self.text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
from pyodata.exceptions import HttpError, PyODataException, ExpressionError, ProgramError | ||
from . import model | ||
from .response import Response | ||
|
||
LOGGER_NAME = 'pyodata.service' | ||
|
||
|
@@ -292,14 +293,7 @@ def add_headers(self, value): | |
|
||
self._headers.update(value) | ||
|
||
def execute(self): | ||
"""Fetches HTTP response and returns processed result | ||
|
||
Sends the query-request to the OData service, returning a client-side Enumerable for | ||
subsequent in-memory operations. | ||
|
||
Fetches HTTP response and returns processed result""" | ||
|
||
def _build_request(self): | ||
if self._next_url: | ||
url = self._next_url | ||
else: | ||
|
@@ -315,10 +309,47 @@ def execute(self): | |
if body: | ||
self._logger.debug(' body: %s', body) | ||
|
||
params = urlencode(self.get_query_params()) | ||
params = self.get_query_params() | ||
|
||
return url, body, headers, params | ||
|
||
async def async_execute(self): | ||
"""Fetches HTTP response and returns processed result | ||
|
||
Sends the query-request to the OData service, returning a client-side Enumerable for | ||
subsequent in-memory operations. | ||
|
||
Fetches HTTP response and returns processed result""" | ||
|
||
url, body, headers, params = self._build_request() | ||
async with self._connection.request(self.get_method(), | ||
url, | ||
headers=headers, | ||
params=params, | ||
data=body) as async_response: | ||
response = Response() | ||
response.url = async_response.url | ||
response.headers = async_response.headers | ||
response.status_code = async_response.status | ||
response.content = await async_response.read() | ||
return self._call_handler(response) | ||
|
||
def execute(self): | ||
"""Fetches HTTP response and returns processed result | ||
|
||
Sends the query-request to the OData service, returning a client-side Enumerable for | ||
subsequent in-memory operations. | ||
|
||
Fetches HTTP response and returns processed result""" | ||
|
||
url, body, headers, params = self._build_request() | ||
|
||
response = self._connection.request( | ||
self.get_method(), url, headers=headers, params=params, data=body) | ||
self.get_method(), url, headers=headers, params=urlencode(params), data=body) | ||
|
||
return self._call_handler(response) | ||
|
||
def _call_handler(self, response): | ||
self._logger.debug('Received response') | ||
self._logger.debug(' url: %s', response.url) | ||
self._logger.debug(' headers: %s', response.headers) | ||
|
@@ -858,6 +889,19 @@ def __getattr__(self, attr): | |
raise AttributeError('EntityType {0} does not have Property {1}: {2}' | ||
.format(self._entity_type.name, attr, str(ex))) | ||
|
||
async def getattr(self, attr): | ||
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. I guess this method should have async_ prefix as well, similar to other new ones. Will throw RuntimeError when called in synchronous script, where it looks like correct usage until seen the pyodata source code I guess.
|
||
"""Get cached value of attribute or do async call to service to recover attribute value""" | ||
try: | ||
return self._cache[attr] | ||
except KeyError: | ||
try: | ||
value = await self.get_proprty(attr).async_execute() | ||
self._cache[attr] = value | ||
return value | ||
except KeyError as ex: | ||
raise AttributeError('EntityType {0} does not have Property {1}: {2}' | ||
.format(self._entity_type.name, attr, str(ex))) | ||
|
||
def nav(self, nav_property): | ||
"""Navigates to given navigation property and returns the EntitySetProxy""" | ||
|
||
|
@@ -1686,6 +1730,16 @@ def http_get(self, path, connection=None): | |
|
||
return conn.get(urljoin(self._url, path)) | ||
|
||
async def async_http_get(self, path, connection=None): | ||
"""HTTP GET response for the passed path in the service""" | ||
|
||
conn = connection | ||
if conn is None: | ||
conn = self._connection | ||
|
||
async with conn.get(urljoin(self._url, path)) as resp: | ||
return resp | ||
|
||
def http_get_odata(self, path, handler, connection=None): | ||
"""HTTP GET request proxy for the passed path in the service""" | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Odata v2 has both JSON and XML Atom as standard response...
https://www.odata.org/documentation/odata-version-2-0/atom-format/
This stuff is already handled in ODataHttpResponse class. Am I missing something why this module needs to be created as well?