Created
March 4, 2021 04:23
-
-
Save johanste/dbfade9084372051d34c5d38c8a598f9 to your computer and use it in GitHub Desktop.
Prototype generic llc request builder
This file contains 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 json | |
import typing | |
import purl | |
HeadersType = typing.Union[typing.Sequence[typing.Tuple[str, typing.Any]], typing.Mapping[str, typing.Any], None] | |
JsonBodyType = typing.Union[typing.Mapping[str, "JsonBodyType"], str, int, float, bool, None] | |
ParamType2 = typing.Union[typing.Sequence[typing.Tuple[str, typing.Any]], typing.Mapping[str, typing.Any]] | |
ParamType = typing.Union[typing.Sequence[typing.Tuple[str, typing.Any]], typing.Mapping[str, typing.Any]] | |
LIST_COLLECTION = 'https://www.dn.se/{collection}{?page,top}' | |
COLLECTION_ITEM = 'https://www.dn.se/{collection}/{item}' | |
@typing.overload | |
def HttpRequest(verb: typing.Literal['get'], path_template:typing.Literal['https://www.dn.se/{collection}{?page,top}'], *, collection, page=None, top=None, headers=None): | |
... | |
@typing.overload | |
def HttpRequest(verb:typing.Literal['get'], path_template:typing.Literal['https://www.dn.se/{collection}/{item}'], *, collection, item, headers=None): | |
... | |
@typing.overload | |
def HttpRequest(verb:typing.Literal['post'], path_template:typing.Literal['https://www.dn.se/{collection}/{item}'], *, collection, item, json, headers=None): | |
... | |
def HttpRequest(verb: str, path_template: str, *, json:JsonBodyType=None, headers:HeadersType=None, params:ParamType={}, **kwargs:typing.Any): | |
return _HttpRequest(verb, path_template, json=json, headers=headers, params=params, **kwargs) | |
class _HttpRequest(object): | |
def __init__(self, verb: str, path_template: str, *, json:JsonBodyType=None, headers:HeadersType=None, params:ParamType={}, **kwargs): | |
self.verb = verb | |
self.template = purl.Template(path_template) | |
variables = dict(params) | |
variables.update(kwargs) | |
self.path = self.template.expand(variables) | |
self.headers = headers.items() if isinstance(headers, dict) else (headers or []) | |
self.json = json | |
def __str__(self): | |
request = f"{self.verb.upper()} {self.path} HTTP/1.1" | |
for header, value in self.headers: | |
request = request + '\n' + f'{header}: {value}' | |
request = request + '\n' | |
if self.json is not None: | |
request = request + f'\n{json.dumps(self.json, indent=True)}' | |
return request | |
request1 = HttpRequest('get', COLLECTION_ITEM, collection='yeah', item=14) | |
print(request1) | |
request2 = HttpRequest('get', 'https://www.dn.se/{collection}{?page,top}', collection='smurfs', page=2, top=17, headers=[('Accept-Encoding', 'application/json')]) | |
print(request2) | |
request3 = HttpRequest('post', 'https://www.dn.se/{collection}/{item}', collection='smurfettes', item=12, json=dict(hello='world'), headers={'Accept-Language': 'en-us', 'Content-Encoding': 'application/smurf+json'}) | |
print(request3) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment