Skip to main content

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

request_api_builder-2.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

request_api_builder-2.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file request_api_builder-2.1.1.tar.gz.

File metadata

  • Download URL: request_api_builder-2.1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.2

File hashes

Hashes for request_api_builder-2.1.1.tar.gz
Algorithm Hash digest
SHA256 5394bb69cb89875683bab740781c64686fe2d56bfda3682f8260a7527949c20c
MD5 8f5bfcd4ebfb8fb1ff73101060c8e2eb
BLAKE2b-256 d31ac939ff50da8b7c0f3161837844bfceabaa5043f4c765a303320070b037f9

See more details on using hashes here.

File details

Details for the file request_api_builder-2.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for request_api_builder-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49a56af275b13b06ab83e2e3995b4077be2d16857c3c08847d2c6b39984e970e
MD5 7d3191f0b04d5fc11bdc45808d0a89ab
BLAKE2b-256 2c2107dced83d069164cc0134b8b201a5c6b40481282af172141036cf41cf9b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page