-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example: using aiohttp as client
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import asyncio | ||
|
||
import aiohttp | ||
import audible | ||
|
||
|
||
class ClientRequest(aiohttp.ClientRequest): | ||
def __init__(self, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
self.apply_auth_flow() | ||
|
||
def apply_auth_flow(self) -> None: | ||
body = self.body | ||
if isinstance(body, aiohttp.BytesPayload): | ||
body = body._value | ||
|
||
sign_headers = audible.auth.sign_request( | ||
method=self.method, | ||
path=self.url.path_qs, | ||
body=body, | ||
adp_token=self._session._audible_auth.adp_token, | ||
private_key=self._session._audible_auth.device_private_key | ||
) | ||
for header, value in sign_headers.items(): | ||
self.headers.add(header, value) | ||
|
||
|
||
class ClientSession(aiohttp.ClientSession): | ||
def __init__(self, *args, audible_auth: audible.Authenticator, **kwargs): | ||
if "request_class" in kwargs: | ||
raise Exception("`request_class` keyword is not supported") | ||
|
||
super().__init__(*args, request_class=ClientRequest, **kwargs) | ||
self._audible_auth = audible_auth | ||
|
||
|
||
if __name__ == "__main__": | ||
async def main(asin): | ||
body = { | ||
"supported_drm_types": ["Mpeg", "Adrm"], | ||
"quality": "Extreme", | ||
"consumption_type": "Download", | ||
"response_groups": ( | ||
"last_position_heard, pdf_url, content_reference, chapter_info" | ||
) | ||
} | ||
|
||
fn = "credentials.json" | ||
auth = audible.Authenticator.from_file(fn) | ||
|
||
async with ClientSession(audible_auth=auth) as session: | ||
async with session.post( | ||
f"https://api.audible.de/1.0/content/{asin}/licenserequest", | ||
json=body | ||
) as r: | ||
print(await r.json()) | ||
#print(r.request_info) | ||
print(r.status) | ||
|
||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main("B004V0WRPG")) |