def test_header_adder(): req = snug.GET('my/url', headers={'Accept': 'application/json'}) adder = snug.header_adder({'Authorization': 'my-auth'}) assert adder(req) == snug.GET('my/url', headers={ 'Accept': 'application/json', 'Authorization': 'my-auth' })
async def quiz_executor_and_schema( args: argparse.Namespace, session: aiohttp.ClientSession ) -> Tuple[quiz.execution.async_executor, quiz.Schema]: async_executor = quiz.async_executor( url="https://api.github.com/graphql", auth=snug.header_adder({"Authorization": f"Bearer {args.github_auth_token}"}), client=session, ) result = await async_executor(quiz.INTROSPECTION_QUERY) schema: quiz.Schema = quiz.Schema.from_raw( result["__schema"], scalars=(), module=None ) log.debug("fetched github graphql schema") return async_executor, schema
async def quiz_executor_and_schema( config: GithubClientConfig, session: aiohttp.ClientSession ) -> Tuple[quiz.execution.async_executor, quiz.Schema]: async_executor = quiz.async_executor( url=config["base_url"], auth=snug.header_adder( {"Authorization": f"Bearer {config['github_auth_token']}"}), client=session, ) result = await async_executor(quiz.INTROSPECTION_QUERY) schema: quiz.Schema = quiz.Schema.from_raw(result["__schema"], scalars=(), module=None) log.debug("fetched github graphql schema") return async_executor, schema
def auth_factory(auth): if isinstance(auth, str): return snug.header_adder({'Authorization': f'bearer {auth}'}) else: assert isinstance(auth, tuple) return auth
def token_auth(token): return snug.header_adder({'Authorization': 'token {}'.format(token)})
def token_auth(token): return snug.header_adder({'Authorization': f'Bearer {token}'})
import json import snug from gentools import reusable, map_yield, map_send add_prefix = snug.prefix_adder('https://api.github.com') add_headers = snug.header_adder({ 'Accept': 'application/vnd.github.v3+json', 'User-Agent': 'my awesome app', }) class ApiException(Exception): """an error from the github API""" def handle_errors(resp): """raise a descriptive exception on a "bad request" response""" if resp.status_code == 400: raise ApiException(json.loads(resp.content).get('message')) return resp def load_json_content(resp): """get the response body as JSON""" return json.loads(resp.content) @reusable @map_send(load_json_content, handle_errors) @map_yield(add_headers, add_prefix, snug.GET) def repo(name: str, owner: str) -> snug.Query[dict]: """a repository lookup by owner and name""" return (yield f'/repos/{owner}/{name}') @reusable
def token_auth(token): return snug.header_adder({"Authorization": "token {}".format(token)})