mirror of
https://github.com/RGBCube/GitHubWrapper
synced 2025-05-17 22:45:08 +00:00
Adding objects
This commit is contained in:
parent
02ff6bbeed
commit
3e49dba6a5
2 changed files with 24 additions and 18 deletions
|
@ -1,6 +1,9 @@
|
||||||
#== http.py ==#
|
#== http.py ==#
|
||||||
|
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from collections import namedtuple
|
||||||
|
from datetime import datetime
|
||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
@ -10,6 +13,7 @@ from .objects import APIOBJECT
|
||||||
from .urls import *
|
from .urls import *
|
||||||
|
|
||||||
LINK_PARSING_RE = re.compile(r"<(\S+(\S))>; rel=\"(\S+)\"")
|
LINK_PARSING_RE = re.compile(r"<(\S+(\S))>; rel=\"(\S+)\"")
|
||||||
|
Rates = namedtuple('Rates', ('remaining', 'used', 'total', 'reset_when', 'last_request'))
|
||||||
|
|
||||||
# aiohttp request tracking / checking bits
|
# aiohttp request tracking / checking bits
|
||||||
async def on_req_start(
|
async def on_req_start(
|
||||||
|
@ -18,7 +22,8 @@ async def on_req_start(
|
||||||
params: aiohttp.TraceRequestStartParams
|
params: aiohttp.TraceRequestStartParams
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Before-request hook to make sure we don't overrun the ratelimit."""
|
"""Before-request hook to make sure we don't overrun the ratelimit."""
|
||||||
print(repr(session), repr(ctx), repr(params))
|
#print(repr(session), repr(ctx), repr(params))
|
||||||
|
pass
|
||||||
|
|
||||||
async def on_req_end(
|
async def on_req_end(
|
||||||
session: aiohttp.ClientSession,
|
session: aiohttp.ClientSession,
|
||||||
|
@ -26,7 +31,15 @@ async def on_req_end(
|
||||||
params: aiohttp.TraceRequestEndParams
|
params: aiohttp.TraceRequestEndParams
|
||||||
) -> None:
|
) -> None:
|
||||||
"""After-request hook to adjust remaining requests on this time frame."""
|
"""After-request hook to adjust remaining requests on this time frame."""
|
||||||
print(repr(session), repr(ctx), repr(params))
|
headers = params.response.headers
|
||||||
|
|
||||||
|
remaining = headers['X-RateLimit-Remaining']
|
||||||
|
used = headers['X-RateLimit-Used']
|
||||||
|
total = headers['X-RateLimit-Limit']
|
||||||
|
reset_when = datetime.fromtimestamp(int(headers['X-RateLimit-Reset']))
|
||||||
|
last_req = datetime.utcnow()
|
||||||
|
|
||||||
|
session._rates = Rates(remaining, used, total, reset_when, last_req)
|
||||||
|
|
||||||
trace_config = aiohttp.TraceConfig()
|
trace_config = aiohttp.TraceConfig()
|
||||||
trace_config.on_request_start.append(on_req_start)
|
trace_config.on_request_start.append(on_req_start)
|
||||||
|
@ -42,6 +55,7 @@ async def make_session(*, headers: dict[str, str], authorization: aiohttp.BasicA
|
||||||
headers=headers,
|
headers=headers,
|
||||||
trace_configs=[trace_config]
|
trace_configs=[trace_config]
|
||||||
)
|
)
|
||||||
|
session._rates = Rates('', '' , '', '', '')
|
||||||
return session
|
return session
|
||||||
|
|
||||||
# pagination
|
# pagination
|
||||||
|
@ -51,7 +65,7 @@ class Paginator:
|
||||||
self.session = session
|
self.session = session
|
||||||
self.response = response
|
self.response = response
|
||||||
self.should_paginate = bool(self.response.headers.get('Link', False))
|
self.should_paginate = bool(self.response.headers.get('Link', False))
|
||||||
types: dict[str, User | ...] = {
|
types: dict[str, APIOBJECT] = {
|
||||||
'user': User,
|
'user': User,
|
||||||
}
|
}
|
||||||
self.target_type = types[target_type]
|
self.target_type = types[target_type]
|
||||||
|
@ -97,12 +111,4 @@ async def get_user(session: aiohttp.ClientSession, username: str) -> GitHubUserD
|
||||||
result = await session.get(USERS_URL.format(username))
|
result = await session.get(USERS_URL.format(username))
|
||||||
if result.status == 200:
|
if result.status == 200:
|
||||||
return await result.json()
|
return await result.json()
|
||||||
raise UserNotFound
|
raise UserNotFound
|
||||||
|
|
||||||
async def get_user_repos(session: aiohttp.ClientSession, username: str) -> list:
|
|
||||||
"""Returns a user's public repos in JSON format."""
|
|
||||||
result = await session.get(USER_REPOS_URL.format(username))
|
|
||||||
if result.status == 200:
|
|
||||||
return await Paginator(session, result, 'repo').exhaust()
|
|
||||||
raise UserNotFound
|
|
||||||
|
|
|
@ -61,11 +61,11 @@ class Github:
|
||||||
"""Fetch a Github user from their username."""
|
"""Fetch a Github user from their username."""
|
||||||
return User(await http.get_user(self.session, username), self.session)
|
return User(await http.get_user(self.session, username), self.session)
|
||||||
|
|
||||||
#async def get_repo(self, repo_name: str) -> 'Repository':
|
async def get_repo(self, repo_name: str) -> 'Repo':
|
||||||
# """Fetch a Github repository from it's name."""
|
"""Fetch a Github repository from it's name."""
|
||||||
# pass
|
pass
|
||||||
|
|
||||||
#async def get_org(self, org_name: str) -> 'Org':
|
async def get_org(self, org_name: str) -> 'Org':
|
||||||
# """Fetch a Github organization from it's name"""
|
"""Fetch a Github organization from it's name"""
|
||||||
# pass
|
pass
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue