Source code for todays_paper_web.widgets.core.request

from typing import Optional

import requests
from pydantic import Field

from todays_paper_web.widgets import registerDataSource, DataSource
from todays_paper_web.widgets.datasource import DataSourceParams


[docs] class RequestParams(DataSourceParams): url: str = Field(..., description="The URL to GET.") username: Optional[str] = Field( None, description="If set, the request is sent with HTTP Basic auth." ) password: Optional[str] = Field(None, description="Password for `username`.")
@registerDataSource("request") class RequestSource(DataSource): Params = RequestParams def fetch(self): auth = None if self.params.username: auth = requests.auth.HTTPBasicAuth(self.params.username, self.params.password) r = requests.get(self.params.url, auth=auth) contentType = r.headers["Content-Type"].split(";") if "application/json" in contentType: return r.json() return r.text