From 3e49dba6a54140c2e2826c044276d14834de25f1 Mon Sep 17 00:00:00 2001 From: VarMonke Date: Sun, 27 Mar 2022 17:00:49 +0530 Subject: [PATCH] Adding objects --- Github/http.py | 30 ++++++++++++++++++------------ Github/main.py | 12 ++++++------ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Github/http.py b/Github/http.py index fd777a8..baadd8b 100644 --- a/Github/http.py +++ b/Github/http.py @@ -1,6 +1,9 @@ #== http.py ==# + import aiohttp +from collections import namedtuple +from datetime import datetime from types import SimpleNamespace import re @@ -10,6 +13,7 @@ from .objects import APIOBJECT from .urls import * 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 async def on_req_start( @@ -18,7 +22,8 @@ async def on_req_start( params: aiohttp.TraceRequestStartParams ) -> None: """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( session: aiohttp.ClientSession, @@ -26,7 +31,15 @@ async def on_req_end( params: aiohttp.TraceRequestEndParams ) -> None: """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.on_request_start.append(on_req_start) @@ -42,6 +55,7 @@ async def make_session(*, headers: dict[str, str], authorization: aiohttp.BasicA headers=headers, trace_configs=[trace_config] ) + session._rates = Rates('', '' , '', '', '') return session # pagination @@ -51,7 +65,7 @@ class Paginator: self.session = session self.response = response self.should_paginate = bool(self.response.headers.get('Link', False)) - types: dict[str, User | ...] = { + types: dict[str, APIOBJECT] = { 'user': User, } 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)) if result.status == 200: return await result.json() - 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 - + raise UserNotFound \ No newline at end of file diff --git a/Github/main.py b/Github/main.py index 4d1bc2d..6aa144d 100644 --- a/Github/main.py +++ b/Github/main.py @@ -61,11 +61,11 @@ class Github: """Fetch a Github user from their username.""" return User(await http.get_user(self.session, username), self.session) - #async def get_repo(self, repo_name: str) -> 'Repository': - # """Fetch a Github repository from it's name.""" - # pass + async def get_repo(self, repo_name: str) -> 'Repo': + """Fetch a Github repository from it's name.""" + pass - #async def get_org(self, org_name: str) -> 'Org': - # """Fetch a Github organization from it's name""" - # pass + async def get_org(self, org_name: str) -> 'Org': + """Fetch a Github organization from it's name""" + pass