-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.py
41 lines (30 loc) · 1.03 KB
/
request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import aiohttp
import requests
BASEURL = 'https://learning.esiea.fr/'
SERVICE = 'webservice/'
API = 'rest/'
CONNECTION = 'server.php?'
FORMAT = 'moodlewsrestformat=json'
TOKEN = '&wstoken='
REQUEST = f'{BASEURL}{SERVICE}{API}{CONNECTION}{FORMAT}{TOKEN}'
class Request:
def __init__(self, token):
self.token = token
self.request = requests
async def get(self, function, **kwargs):
url = REQUEST + self.token
url += f'&wsfunction={function}'
for key, value in kwargs.items():
url += f'&{key}={value}'
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as r:
if r.status == 200:
js = await r.json()
return js
except requests.exceptions.HTTPError as httperror:
return f'HTTP error occured: {httperror}'
except Exception as err:
return f'Other error occured: {err}'
def post(self, *args, **kwargs):
pass