Last active
August 20, 2024 10:25
-
-
Save Chimezirim-Bassey/46e6a907374d5aa85056503eb61f6b52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.client | |
import asyncio | |
import json | |
class API: | |
URL = 'hacker-news.firebaseio.com' | |
async def get(self, *, path: str) -> list | int | dict: | |
url = f'{self.URL}' | |
path = f'/v0/{path}' | |
conn = http.client.HTTPSConnection(url) | |
await asyncio.to_thread(conn.request, 'GET', path) | |
res = await asyncio.to_thread(conn.getresponse) | |
res = json.loads(res.read().decode('utf-8')) | |
conn.close() | |
return res | |
async def get_item(self, *, item_id) -> dict: | |
path = f'item/{item_id}.json' | |
res = await self.get(path=path) | |
return res | |
async def get_user(self, *, user_id) -> dict: | |
path = f'user/{user_id}.json' | |
res = await self.get(path=path) | |
return res | |
async def max_item(self) -> int: | |
path = 'maxitem.json' | |
res = await self.get(path=path) | |
return res | |
async def new_stories(self) -> list[int]: | |
path = 'newstories.json' | |
res = await self.get(path=path) | |
return res | |
async def best_stories(self) -> list[int]: | |
path = 'beststories.json' | |
res = await self.get(path=path) | |
return res | |
async def top_stories(self) -> list[int]: | |
path = 'topstories.json' | |
res = await self.get(path=path) | |
return res | |
async def ask_stories(self) -> list[int]: | |
path = 'askstories.json' | |
res = await self.get(path=path) | |
return res | |
async def job_stories(self) -> list[int]: | |
path = 'jobstories.json' | |
res = await self.get(path=path) | |
return res | |
async def show_stories(self) -> list[int]: | |
path = 'showstories.json' | |
res = await self.get(path=path) | |
return res | |
async def updates(self) -> dict: | |
path = 'updates.json' | |
res = await self.get(path=path) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment