Request builder for testing API
Project description
request_api_builder
BuildRequests Class
The BuildRequests class provides a convenient interface for making HTTP requests using the requests library in Python. It simplifies the process of sending GET, POST, PATCH, and DELETE requests by storing common parameters like base URL and cookie across all requests.
Usage
1. Import RequestBuilder
from request_api_builder.rab import BuildRequests, build_request
2. Set Cookie and Base URL.
Before making any requests, set the cookie and base URL using the following methods:
BuildRequests.set_cookie(cookie)
BuildRequests.set_base_url(base_url)
3. Create requests
Additionally, there is a utility function build_request that simplifies the process of making requests by automatically selecting the correct HTTP method based on the input:
build_request(method, url_template, data=None, **kwargs)
The class provides methods for each HTTP method:
- GET Request
response = build_request(method='get', url_template, params=None, **url_params)
- POST Request
response = build_request(method='post', url_template, data=None, **url_params)
- PATCH Request
response = build_request(method='patch', url_template, data=None, **url_params)
- DELETE Request
response = build_request(method='delete', url_template, **url_params)
Parameters:
method(str): HTTP method (get, post, patch, delete).url_template(str): The URL template where parameters can be interpolated.params(dict, optional): Query parameters for GET requests.data(dict, optional): Data to be sent as the body of the request (for POST and PATCH requests).**url_params(dict, optional): Keyword arguments to interpolate into the url_template.
Samples
- GET Request
request_get = build_request(
method='get',
url_template='api/endpoint/{id}/another/{another_id}',
id='123',
another_id='456',
query_param_1='value1',
query_param_2='value2',
)
- POST Request
request_post = build_request(
method='post',
url_template='api/endpoint/{id}/another/{another_id}',
data={},
id='123',
another_id='456',
query_param_1='value1',
query_param_2='value2',
)
- PATCH Request
request_post = build_request(
method='post',
url_template='api/endpoint/{id}/another/{another_id}',
data={},
id='123',
another_id='456',
query_param_1='value1',
query_param_2='value2',
)
- DELETE Request
request_post = build_request(
method='post',
url_template='api/endpoint/{id}/another/{another_id}',
id='123',
another_id='456',
query_param_1='value1',
query_param_2='value2',
)
Utility Functions
This block describes the utility functions get_base_url and write_to_html, which provide additional functionalities to complement the BuildRequests class for making HTTP requests.
1. Import
from request_api_builder.utils import get_base_url, write_to_html
2. Usage
get_base_url
The get_base_url() function retrieves the base URL for API requests from the environment variable API_BASE.
# Retrieve base URL from environment variable
base_url = get_base_url()
write_to_html
The write_to_html(response, file_name='test.html') function takes an HTTP response object and writes its content to
an HTML file. It detects the encoding of the response content, decodes it, and saves it as UTF-8 encoded text.
# Write the response content to an HTML file
decoded_text = write_to_html(response, file_name='myfile.html')
Parameters:
response(requests.Response): The HTTP response object to be written to the file.file_name(str, optional): The name of the file to write the HTML content. Defaults to 'test.html'.
Example
from request_api_builder.rab import BuildRequests, build_request
from request_api_builder.utils import get_base_url, write_to_html
from login import get_cookie
from test_data import base_url
cookie = get_cookie()
BuildRequests.set_cookie(cookie)
BuildRequests.set_base_url(base_url)
response = build_request(
method='get',
url_template='api/endpoint/{id}/another/{another_id}',
id='123',
another_id='456',
query_param_1='value1',
query_param_2='value2',
)
# Will create a file named 'myfile.html' with the response content
decoded_text = write_to_html(response, file_name='myfile.html')
# Print the decoded text if needed
print(decoded_text)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file request_api_builder-2.1.0.tar.gz.
File metadata
- Download URL: request_api_builder-2.1.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25df7fddb9e5be4a37dd5f1e01fe29e2601cba479d86b44e18e41345cb0fd515
|
|
| MD5 |
f72f043b39c7d36b15e3d2ffa61b1254
|
|
| BLAKE2b-256 |
b8da97f1bccf431c6b9a1dbe58356f316120c678b674440e686d1725fb4060d7
|
File details
Details for the file request_api_builder-2.1.0-py3-none-any.whl.
File metadata
- Download URL: request_api_builder-2.1.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61b8c6bfc004bcef196e246532f0286d645d9cc4f4c6314a91f02c0ad3a495a8
|
|
| MD5 |
cb686f7eea593bd3502968d907f08b8c
|
|
| BLAKE2b-256 |
e518021aff2c95ea174720c260f822468ed558dc0942faa0e15918ae8c83cb12
|