-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24feb6c
commit a3e6937
Showing
8 changed files
with
97 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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,19 @@ | ||
:mod:`gidgethub.httpx` --- httpx support | ||
============================================= | ||
|
||
.. module:: gidgethub.httpx | ||
|
||
.. class:: GitHubAPI(client, requester, *, oauth_token=None, cache=None) | ||
|
||
An implementation of :class:`gidgethub.abc.GitHubAPI` using | ||
`httpx <https://www.encode.io/httpx>`_. Typical usage will be:: | ||
|
||
import httpx | ||
import gidgethub.httpx | ||
|
||
|
||
async with httpx.AsyncClient() as client: | ||
gh = gidgethub.httpx.GitHubAPI(client, requester, | ||
oauth_token=oauth_token) | ||
# Make your requests, e.g. ... | ||
data = await gh.getitem("/rate_limit") |
This file contains 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 |
---|---|---|
|
@@ -134,6 +134,7 @@ Contents | |
aiohttp | ||
treq | ||
tornado | ||
httpx | ||
|
||
|
||
About the title | ||
|
This file contains 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,24 @@ | ||
import asyncio | ||
from typing import Mapping, Tuple, Any | ||
|
||
import httpx | ||
|
||
from . import abc as gh_abc | ||
|
||
|
||
class GitHubAPI(gh_abc.GitHubAPI): | ||
def __init__(self, client: httpx.AsyncClient, *args: Any, | ||
**kwargs: Any) -> None: | ||
self._client = client | ||
super().__init__(*args, **kwargs) | ||
|
||
async def _request(self, method: str, url: str, headers: Mapping[str, str], | ||
body: bytes = b'') -> Tuple[int, Mapping[str, str], bytes]: | ||
"""Make an HTTP request.""" | ||
response = await self._client.request( | ||
method, url, headers=dict(headers), data=body) | ||
return response.status_code, response.headers, await response.read() | ||
|
||
async def sleep(self, seconds: float) -> None: | ||
"""Sleep for the specified number of seconds.""" | ||
await asyncio.sleep(seconds) |
This file contains 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,39 @@ | ||
import datetime | ||
|
||
import httpx | ||
import pytest | ||
|
||
from .. import httpx as gh_httpx | ||
from .. import sansio | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_sleep(): | ||
delay = 1 | ||
start = datetime.datetime.now() | ||
async with httpx.AsyncClient() as client: | ||
gh = gh_httpx.GitHubAPI(client, "gidgethub") | ||
await gh.sleep(delay) | ||
stop = datetime.datetime.now() | ||
assert (stop - start) > datetime.timedelta(seconds=delay) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test__request(): | ||
"""Make sure that that abstract method is implemented properly.""" | ||
request_headers = sansio.create_headers("gidgethub") | ||
async with httpx.AsyncClient() as client: | ||
gh = gh_httpx.GitHubAPI(client, "gidgethub") | ||
aio_call = await gh._request("GET", "https://api.github.com/rate_limit", | ||
request_headers) | ||
data, rate_limit, _ = sansio.decipher_response(*aio_call) | ||
assert "rate" in data | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get(): | ||
"""Integration test.""" | ||
async with httpx.AsyncClient() as client: | ||
gh = gh_httpx.GitHubAPI(client, "gidgethub") | ||
data = await gh.getitem("/rate_limit") | ||
assert "rate" in data |
This file contains 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 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