External Requests
A class based alternative to making HTTP requests in Python.
Installation
Installation can be done with pip:
pip install external-requests
Basic Usage
In order to use external requests, you must implement one of the abstract classes provided by the package, create an instance of your new class, and call the request function.
from typing import List
from external_requests.base_requests.external_request import ExternalRequest
from external_requests.request_methods import RequestMethods
class ExampleRequest(ExternalRequest):
@property
def _base_url(self) -> str:
return 'http://some-url.com'
@property
def _route(self) -> str:
return '/my-route'
@property
def _request_method(self) -> RequestMethods:
return RequestMethods.GET
@property
def _expected_status_codes(self) -> List[int]:
return [200]
ExampleRequest().request()
Want to avoid duplication between similar requests? You can replace the ExternalRequest base class with one of the alternative, request-specific classes which implement both the request method and expected status code properties. There is a request-specific class for all seven primary HTTP request methods: GET, POST, PUT, PATCH, DELETE, HEAD and OPTIONS.
Working With Parameters
Parameters can be injected into the request when creating an instance of it. This is done using kwargs which are saved on the object in the init function by default. It is recommended that you also state the parameters on the class for readability, although this is not required.
from external_requests.method_requests.get_request import GetRequest
class ExampleRequest(GetRequest):
route_param: str
@property
def _base_url(self) -> str:
return 'http://some-url.com'
@property
def _route(self) -> str:
return f'/my-route/{self.route_param}'
ExampleRequest(route_param='value').request()
Requests With Bodies
In order to both simplify and create standardization in requests with bodies, the SerializationSchema class (based on marshmallow) is available. In order to use it, simply create an instance of your needed schema, inherit from SerializationSchema, and return an instance of your schema in your request's _body_schema property.
from marshmallow import fields
from external_requests.method_requests.post_request import PostRequest
from external_requests.serialization_schema import SerializationSchema
class ExampleSchema(SerializationSchema):
name = fields.Str(data_key='name')
other_attribute = fields.Str(data_key='otherAttribute')
class ExampleRequest(PostRequest):
name: str
other_attribute: str
@property
def _body_schema(self) -> SerializationSchema:
return ExampleSchema(name=self.name, other_attribute=self.other_attribute)
@property
def _base_url(self) -> str:
return 'http://some-url.com'
@property
def _route(self) -> str:
return '/my-route'
ExampleRequest(name='name', other_attribute='other').request()
The project's code can be found here https://gitlab.com/VenfiOranai/external-requests
Metadata
Release files for external-requests 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| external_requests-1.0.2.tar.gz | 5.2 kB | Without provenance | Details |
Release files / external_requests-1.0.2.tar.gz
| Download URL | external_requests-1.0.2.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8b67891d65de9a512eb6ea4852901fb602ac220b5d4d3baa15b5c0cc41d62800
|
|
BLAKE2b-256 checksum How to use checksums |
c1f03f3cf838facb81a4ebc3004c026adabe0cb81293f6a3c5e9743fc0a0aad2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.11
|
Security for external-requests 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.