<reponame>quiktea/nasa.py
import aiohttp
import asyncio
from .errors import *

class Route:
    BASE = "https://api.nasa.gov"
    def __init__(self, endpoint : str, params : dict = {}, method : str = "GET"):
        self.endpoint = endpoint
        self._method = method
        self._params = params

    def __repr__(self):
        return f"<Route method={self._method} path={self.path} params={self.params}>"
    
    @property
    def method(self):
        return self._method
    
    @property
    def path(self) -> str:
        return self.BASE + self.endpoint
    
    @property
    def params(self) -> dict:
        for key in list(self._params.keys()):
            if self._params.get(key) is None or self._params.get(key) == "":
                self._params.pop(key, None)
        return self._params                

    @params.setter
    def params(self, value : dict):
        self._params = value

class HTTPClient:
    def __init__(self, *args, **kwargs):
        self.session = aiohttp.ClientSession(*args, **kwargs)

    async def request(self, route : Route) -> aiohttp.ClientResponse:
        response = await self.session.request(route.method, route.path, params = route.params)
        if response.status in {400}:
            __response__ = await response.json()
            raise HTTPException(response, __response__.get("msg"))
        elif response.status == "429":
            raise HTTPException(response, "You have exceeded your rate limit. Try again later or contact us at https://api.nasa.gov:443/contact/ for assistance")
        return response

    async def read(self, url : str):
        response = await self.session.get(url)
        return await response.read()

    async def __try_key__(self, key : str):
        async with aiohttp.ClientSession(raise_for_status = False) as temp_session:
            response = await temp_session.get("https://api.nasa.gov/planetary/apod?api_key={}".format(key)) #just trying the key on one of the endpoints before using the api key on all the other endpoints
            if response.status == 403:
                raise InvalidAPIKey("Invalid API Key has been passed")


    def login(self, key : str):
        asyncio.get_event_loop().run_until_complete(self.__try_key__(key))
import aiohttp
import asyncio
from .errors import *

class Route:
    BASE = "https://api.nasa.gov"
    def __init__(self, endpoint : str, params : dict = {}, method : str = "GET"):
        self.endpoint = endpoint
        self._method = method
        self._params = params

    def __repr__(self):
        return f"<Route method={self._method} path={self.path} params={self.params}>"
    
    @property
    def method(self):
        return self._method
    
    @property
    def path(self) -> str:
        return self.BASE + self.endpoint
    
    @property
    def params(self) -> dict:
        for key in list(self._params.keys()):
            if self._params.get(key) is None or self._params.get(key) == "":
                self._params.pop(key, None)
        return self._params                

    @params.setter
    def params(self, value : dict):
        self._params = value

class HTTPClient:
    def __init__(self, *args, **kwargs):
        self.session = aiohttp.ClientSession(*args, **kwargs)

    async def request(self, route : Route) -> aiohttp.ClientResponse:
        response = await self.session.request(route.method, route.path, params = route.params)
        if response.status in {400}:
            __response__ = await response.json()
            raise HTTPException(response, __response__.get("msg"))
        elif response.status == "429":
            raise HTTPException(response, "You have exceeded your rate limit. Try again later or contact us at https://api.nasa.gov:443/contact/ for assistance")
        return response

    async def read(self, url : str):
        response = await self.session.get(url)
        return await response.read()

    async def __try_key__(self, key : str):
        async with aiohttp.ClientSession(raise_for_status = False) as temp_session:
            response = await temp_session.get("https://api.nasa.gov/planetary/apod?api_key={}".format(key)) #just trying the key on one of the endpoints before using the api key on all the other endpoints
            if response.status == 403:
                raise InvalidAPIKey("Invalid API Key has been passed")


    def login(self, key : str):
        asyncio.get_event_loop().run_until_complete(self.__try_key__(key))
#just trying the key on one of the endpoints before using the api key on all the other endpoints