This component makes working with requests a little easier in introducing a somewhat async workflow by taking a request on one after another with an extensive API.
It makes life a little easier by autoparsing requests and responses to JSON.
Parameters:
- Server : The base domain all requests will be appended to.
- Header: A TableDAT representing a Header that will be appended to every request where first col is the key, second col is the value.
- Timeout: How long to wait in seconds before closing the connection and executing the next request.
Attributes:
- Processing : Bool. Shows if the webclient is currently fetching data.
Methods:
- QueryRequest(request_dict : Dictionary): Takes a dictionary and appends it to the queue and fires it off if nothing is requesting at the moment. The Dict needs to be formatted the following way:
method: HTTP Request method as a string. ("GET", "POST", ...)
url: The target URL including query-params.
header: The header as a Dictionary.
body: The data you want to send in the requestbody.
callback: A method taking one argument that gets called when the request returns a success. - MultipartFormData( dictionary ): Returns the dictionary as multipart-formdata in bytes. Header needs to be set manually.
- Get( endpoint: string, params: Dict, header: Dict, callback: function) : Query a get request. Except endpoint everything else is optional.
- Post,Put,Delete( endpoint: string, params: Dict, header: Dict, callback: function, data ): Creates a Post/Put/Delete request.
If the data is delivered as a dictionary, it will be converted to JSON and the apllicationtype Json will be applied to the header.
For get one page of catbreeds from catfact.ninja and print it out call:
op("queriedWebClient").Get("breeds",
params = {"limit" : 1},
callback = lambda response: debug(response))
Callbacks:
- onResponse(request, response): Passes the request and the response on a good response.
- onError(request, response, error_exception, exceptions: On error. Passes the request, response, an exception and the module with exceptions.
- onQueryEmpty(): Gets called when the last request in the current query resolved.
- onTimeout( request ): No response from the server during the timeout.
Exceptions:
The onError-callbacks passes an exception-object that can be rissen and excepted to handle specific errors.
try:
raise error_exception( response["body"] )
except exceptions.NotFound:
#What happens when the endpoint cannot be found?
WebClientError : A general-purpose error.
UserError : The issues lies withing the request
ServerError : The issue comes from the server.
400 : BadRequest,
401 : Unauthorized,
403 : Forbidden,
404 : NotFound,
405 : MethodNotAllowed,
410 : Gone,
500 : InternalServerError,
501 : NotImplemented,
502 : BadGateway,
503 : ServiceUnavailable,
504 : GatewayTimeout