23
23
24
24
from __future__ import annotations
25
25
26
- __all__ = ("OAuth2Response" , "PlugSocketBuilder" , "OAuthURL" , "Image" )
26
+ __all__ = ("OAuth2Response" , "PlugSocketBuilder" , "OAuthURL" , "Image" , "Settings" )
27
27
28
28
import asyncio
29
29
import datetime
35
35
import aiohttp
36
36
import attrs
37
37
38
- from . import error
39
- from . import url
40
- from .internal import enums
41
- from .internal import helpers
38
+ from . import error , url
39
+ from .internal import enums , helpers
42
40
43
41
if typing .TYPE_CHECKING :
44
42
import collections .abc as collections
45
43
import concurrent .futures
46
44
import os
45
+ import ssl
47
46
import types
48
47
49
- from typing_extensions import Required
50
- from typing_extensions import Self
48
+ from typing_extensions import Required , Self
51
49
52
- from aiobungie import traits
53
- from aiobungie import typedefs
50
+ from aiobungie import traits , typedefs
54
51
55
52
class _FinderListingValue (typing .TypedDict ):
56
53
valueType : Required [int ]
@@ -62,6 +59,49 @@ class _ListingFilter(typing.TypedDict):
62
59
matchType : int
63
60
64
61
62
+ @typing .final
63
+ @attrs .define (kw_only = True )
64
+ class Settings :
65
+ """Basic settings used within aiobungie HTTP clients."""
66
+
67
+ http_timeout : aiohttp .ClientTimeout = attrs .field (
68
+ default = aiohttp .ClientTimeout (30.0 )
69
+ )
70
+ """Setting to control HTTP request timeouts, This includes
71
+ the time it takes to acquire the client,
72
+ timeout for connecting and reading the socket, and more.
73
+
74
+ Defaults to total of `30.0` seconds.
75
+ """
76
+
77
+ trust_env : bool = attrs .field (default = False )
78
+ """Trust environment settings for proxy configuration.
79
+
80
+ Gets proxy credentials from `~/.netrc` file if present or
81
+ Gets HTTP Basic Auth credentials from `~/.netrc` file if present.
82
+
83
+ If `NETRC` environment variable is set, read from file specified
84
+ there rather than from `~/.netrc`.
85
+ """
86
+
87
+ auth : aiohttp .BasicAuth | None = attrs .field (default = None )
88
+ """an object that represents HTTP Basic Authorization, Defaults to `None`."""
89
+
90
+ headers : collections .Mapping [str , typing .Any ] | None = attrs .field (default = None )
91
+ """Default HTTP headers to send the request with, Defaults to `None`."""
92
+
93
+ use_dns_cache : bool = attrs .field (default = True )
94
+ """References [use_dns_cache](https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.TCPConnector)"""
95
+ ttl_dns_cache : int = attrs .field (default = 10 )
96
+ """References [ttl_dns_cache](https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.TCPConnector)"""
97
+ verify_ssl : bool = attrs .field (default = True )
98
+ """References [verify_ssl](https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.TCPConnector)"""
99
+ ssl_context : ssl .SSLContext | None = attrs .field (default = None )
100
+ """References [ssl_context](https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.TCPConnector)"""
101
+ ssl : bool | aiohttp .Fingerprint | ssl .SSLContext = attrs .field (default = True )
102
+ """References [ssl](https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.TCPConnector)"""
103
+
104
+
65
105
@typing .final
66
106
class MimeType (str , enums .Enum ):
67
107
"""Image mime types enum."""
@@ -82,7 +122,9 @@ def _open_write_path(path: pathlib.Path) -> typing.BinaryIO:
82
122
83
123
@typing .final
84
124
class Image :
85
- """A stream-able Bungie resource.
125
+ """A streamable Bungie resource.
126
+
127
+ Images are _lazy_, which mean they do nothing unless you `await` or poll them.
86
128
87
129
Example
88
130
-------
@@ -171,7 +213,7 @@ def create_url(self) -> str:
171
213
"""
172
214
return url .BASE + "/" + self .path
173
215
174
- async def static_request (self ) -> aiohttp .ClientResponse :
216
+ async def _static_request (self ) -> aiohttp .ClientResponse :
175
217
client_session = aiohttp .ClientSession ()
176
218
request = client_session .request (
177
219
"GET" , self .create_url (), raise_for_status = False
@@ -200,7 +242,7 @@ async def save(
200
242
file_name : str ,
201
243
path : str | os .PathLike [str ],
202
244
* ,
203
- mime_type : MimeType | str = MimeType .JPEG ,
245
+ mime_type : MimeType | str = MimeType .PNG ,
204
246
executor : concurrent .futures .Executor | None = None ,
205
247
) -> None :
206
248
"""Saves this image to a file.
@@ -239,7 +281,7 @@ async def save(
239
281
loop = helpers .get_or_make_loop ()
240
282
file = await loop .run_in_executor (executor , _open_write_path , path )
241
283
242
- reader = await self .static_request ()
284
+ reader = await self ._static_request ()
243
285
try :
244
286
async for chunk in reader .content :
245
287
await loop .run_in_executor (executor , file .write , chunk )
@@ -268,7 +310,7 @@ async def read(self) -> bytes:
268
310
`bytes`:
269
311
The bytes of this image.
270
312
"""
271
- return await (await self .static_request ()).read ()
313
+ return await (await self ._static_request ()).read ()
272
314
273
315
async def stream (self ) -> aiohttp .streams .AsyncStreamIterator [bytes ]:
274
316
"""Stream this image's data.
@@ -287,17 +329,16 @@ async def stream(self) -> aiohttp.streams.AsyncStreamIterator[bytes]:
287
329
Returns
288
330
-------
289
331
`AsyncStreamIterator[bytes]`:
290
- A lazy async iterator that is ready in memory .
332
+ A streaming iterator of this image bytes, yielding the entire data as soon as its received .
291
333
"""
292
- return (await self .static_request ()).content .iter_any ()
334
+ return (await self ._static_request ()).content .iter_any ()
293
335
294
- async def chunks (self , size : int ) -> aiohttp .streams .AsyncStreamIterator [bytes ]:
295
- """Stream this image's data in chunks.
336
+ async def chunks (self , n : int ) -> aiohttp .streams .AsyncStreamIterator [bytes ]:
337
+ """Stream the bytes of this image in `n` chunks.
296
338
297
339
Example
298
340
-------
299
341
```py
300
- # it must be awaited to fetch the image first.
301
342
buffer_size = 1024
302
343
image = Image.default()
303
344
@@ -313,9 +354,9 @@ async def chunks(self, size: int) -> aiohttp.streams.AsyncStreamIterator[bytes]:
313
354
Returns
314
355
-------
315
356
`AsyncStreamIterator[bytes]`:
316
- lazy async iterator that is ready in memory .
357
+ A chunking stream of bytes .
317
358
"""
318
- return (await self .static_request ()).content .iter_chunked (size )
359
+ return (await self ._static_request ()).content .iter_chunked (n )
319
360
320
361
async def iter (self ) -> collections .AsyncGenerator [bytes , None ]:
321
362
"""Yield each byte in this image from start to end.
@@ -332,7 +373,7 @@ async def iter(self) -> collections.AsyncGenerator[bytes, None]:
332
373
Returns
333
374
-------
334
375
`collections.AsyncGenerator[bytes, None]`
335
- An async generator that yields this image's bytes.
376
+ An async generator that yields this image's bytes from start to end .
336
377
"""
337
378
338
379
reader = await self .chunks (1024 )
0 commit comments