|
1 | | -import aiohttp |
| 1 | +import httpx |
2 | 2 |
|
3 | 3 | async def get(self, endpoint, *args, **kwargs): |
4 | 4 | """Async version of GET request""" |
5 | 5 | await self.validate_session_async() |
6 | | - auth = aiohttp.BasicAuth(self.email, self.password) if self.auth else None |
7 | | - |
8 | | - async with aiohttp.ClientSession() as session: |
9 | | - async with session.get( |
10 | | - self.domain + endpoint, |
11 | | - headers=self.header, |
| 6 | + auth = (self.email, self.password) if self.auth else None |
| 7 | + |
| 8 | + async with httpx.AsyncClient() as client: |
| 9 | + res = await client.get( |
| 10 | + self.domain + endpoint, |
| 11 | + headers=self.header, |
12 | 12 | auth=auth, |
13 | 13 | **kwargs |
14 | | - ) as res: |
15 | | - if 'raw' in args: |
16 | | - return res |
17 | | - else: |
18 | | - return await res.json() if res.status == 200 else False |
19 | | - |
| 14 | + ) |
| 15 | + if 'raw' in args: |
| 16 | + return res |
| 17 | + else: |
| 18 | + return res.json() if res.status_code == 200 else False |
20 | 19 |
|
21 | 20 | async def post(self, endpoint, *args, **kwargs): |
22 | 21 | """Async version of POST request""" |
23 | 22 | await self.validate_session_async() |
24 | | - auth = aiohttp.BasicAuth(self.email, self.password) if self.auth else None |
25 | | - |
26 | | - async with aiohttp.ClientSession() as session: |
27 | | - async with session.post( |
28 | | - self.domain + endpoint, |
29 | | - headers=self.header, |
| 23 | + auth = (self.email, self.password) if self.auth else None |
| 24 | + |
| 25 | + async with httpx.AsyncClient() as client: |
| 26 | + res = await client.post( |
| 27 | + self.domain + endpoint, |
| 28 | + headers=self.header, |
30 | 29 | auth=auth, |
31 | 30 | **kwargs |
32 | | - ) as res: |
33 | | - if 'raw' in args: |
34 | | - return res |
35 | | - else: |
36 | | - return await res.json() if res.status == 200 else False |
37 | | - |
| 31 | + ) |
| 32 | + if 'raw' in args: |
| 33 | + return res |
| 34 | + else: |
| 35 | + return res.json() if res.status_code == 200 else False |
38 | 36 |
|
39 | 37 | async def put(self, endpoint, *args, **kwargs): |
40 | 38 | """Async version of PUT request for updating objects""" |
41 | 39 | await self.validate_session_async() |
42 | | - auth = aiohttp.BasicAuth(self.email, self.password) if self.auth else None |
43 | | - |
44 | | - async with aiohttp.ClientSession() as session: |
45 | | - async with session.put( |
46 | | - self.domain + endpoint, |
47 | | - headers=self.header, |
| 40 | + auth = (self.email, self.password) if self.auth else None |
| 41 | + |
| 42 | + async with httpx.AsyncClient() as client: |
| 43 | + res = await client.put( |
| 44 | + self.domain + endpoint, |
| 45 | + headers=self.header, |
48 | 46 | auth=auth, |
49 | 47 | **kwargs |
50 | | - ) as res: |
51 | | - if 'raw' in args: |
52 | | - return res |
53 | | - else: |
54 | | - return res.status |
55 | | - |
| 48 | + ) |
| 49 | + if 'raw' in args: |
| 50 | + return res |
| 51 | + else: |
| 52 | + return res.status_code |
56 | 53 |
|
57 | 54 | async def delete(self, endpoint, *args, **kwargs): |
58 | 55 | """Async version of DELETE request""" |
59 | 56 | await self.validate_session_async() |
60 | | - auth = aiohttp.BasicAuth(self.email, self.password) if self.auth else None |
61 | | - |
62 | | - async with aiohttp.ClientSession() as session: |
63 | | - async with session.delete( |
64 | | - self.domain + endpoint, |
65 | | - headers=self.header, |
| 57 | + auth = (self.email, self.password) if self.auth else None |
| 58 | + |
| 59 | + async with httpx.AsyncClient() as client: |
| 60 | + res = await client.delete( |
| 61 | + self.domain + endpoint, |
| 62 | + headers=self.header, |
66 | 63 | auth=auth, |
67 | 64 | **kwargs |
68 | | - ) as res: |
69 | | - if 'raw' in args: |
70 | | - return res |
71 | | - else: |
72 | | - return res.status |
| 65 | + ) |
| 66 | + if 'raw' in args: |
| 67 | + return res |
| 68 | + else: |
| 69 | + return res.status_code |
0 commit comments