Reference
Public API¶
generate_http_request_code
¶
from http_request_codegen import generate_http_request_code
Generates a code snippet of an HTTP request for a library of a given programming language or a CLI of a program, based on a valid HTTP method and a specification of parameters.
There are some peculiarities concerning certain methods:
POST
Most POST methods implementations render their code snippets different, depending on Content-Types header, including by default some of the most used Content-Types header related behaviours:
- The default behavior, even if you don't specify it explicitly in the Content-Type header is the generation of an
application/x-www-form-urlencoded
encoded request. - If you want to generate a
multipart/form-data
encoded request, you need to specify the files to sent using thefiles
argument. - If you specifies the Content-Type header
application/json
, the parameters sent will be adjusted according to the JSON encoded POST request. - If you specifies the Content-Type header
text/plain
, you can only send one parameter and it will be adjusted accordingly following the implementation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
language | str | Programming language or plataform of the resulting code snippet. See Support to check the supported platforms and programming languages. | None |
impl | str | Implementation type used for the code snippet. It can be a library, a program, or a language API. See Support to check the supported implementations by language. | None |
method | str | HTTP method of the generated request. | 'GET' |
url | str, iterable, callable | URL endpoint of the generated request.
| 'http://localhost' |
headers | dict | Mapping of request header names and values. | {} |
parameters | list | List of parameters for the request. Each parameter must be a dictionary. This dictionary defines, for each parameter, what is the parameter name and how are the parameters values generated:
| [] |
files | dict | Mapping of files to send to URL. Only has effect for POST methods. If you define this argument, the
| {} |
wrap | int | Maximum anchor of the rendered code snippet. If it exceeds it, the rendered code will be conveniently formatted on multiple lines. | 80 |
indent | str | Indentation string used in the generated code. If not defined, the indentation string commonly used in the implementation will be used. | None |
quote_char | str | Quotation character for strings used in the generated code. | "'" |
setup | bool, str | If | None |
teardown | str | Code snippet to include after the HTTP request code. | None |
oneline | bool | Render the code in a single line. | False |
seed | int | Seed used generating random fake values of parameters. Useful if you want to generate the same set of values between multiples code snippets. | None |
locale | str | Locale used by faker library to localize the faked random values for parameters. | None |
Exceptions:
Type | Description |
---|---|
ValueError | Value is not a valid value in their context. |
TypeError | Values does not complaint with the types supported for it. |
ImportError | Python module-function path specified can not be imported successfully. |
Returns:
Type | Description |
---|---|
str | HTTP request code snippet. |
generate_http_request_md_fenced_code_block
¶
from http_request_codegen import generate_http_request_md_fenced_code_block
Wraps generate_http_request_code
function result in a Markdown fenced code block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fence_string | str | Code block fence string used wrapping the code. It does not perform any check about if the fenced string is a "valid" markdown code block fence string. | '```' |
**kwargs | All other optional arguments are passed to | {} |
Examples:
>>> generate_http_request_md_fenced_code_block(setup=False)
"```python\nreq = requests.get('http://localhost')\n```"
>>> generate_http_request_md_fenced_code_block(fence_string='~~~',
... setup=False)
"~~~python\nreq = requests.get('http://localhost')\n~~~"
Returns:
Type | Description |
---|---|
str | Fenced code block with HTTP request code snippet inside. |
lazy_name_by_parameter
¶
from http_request_codegen import lazy_name_by_parameter
Given a dictionary of parameter options, returns the corresponding parameter name built following the rules listed in parameters
argument of generate_http_request_code
function documentation.
The strategy of name building is to check next attributes in given order:
name
names
You can use this function to build the parameters at lower level. This can be used, for example, to append the parameters to an URL generating GET method code snippets if an implementation does by building the parameters as arguments of a function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameter_data | dict | Parameter specification data. It's defined at name and names sections of | required |
seed | int | Seed using randomizing names. | None |
Exceptions:
Type | Description |
---|---|
ValueError | none of the |
Examples:
>>> lazy_name_by_parameter({'name': 'foo'})
'foo'
>>> result = lazy_name_by_parameter({'names': ['foo', 'bar', 'baz']})
>>> result in ['foo', 'bar', 'baz']
True
Returns:
Type | Description |
---|---|
str | Parameter name. |
lazy_value_by_parameter
¶
from http_request_codegen import lazy_value_by_parameter
Given a dictionary of parameter options, returns the corresponding value built following the rules listed in parameters
argument of generate_http_request_code
function documentation.
For example, giving {'type': int}
as input, the output will be a random number as string.
The strategy of value building is to check next attributes in given order:
'value'
'values'
'faker'
'type'
If none of the previous attributes are passed will be treated as if {'type': str}
has been passed, returning a random word.
You can use this function to build the parameters at lower level. This can be used, for example, to append the parameters to an URL generating GET method code snippets if an implementation does by building the parameters as arguments of a function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameter_data | dict | Parameter specification data. It's defined at type, value, values and faker sections of | required |
seed | int | Seed using randomizing values. | None |
locale | str | Locale used for | None |
Examples:
>>> lazy_value_by_parameter({'value': 'foo'})
'foo'
>>> result = lazy_value_by_parameter({'values': ['foo', 'bar', 'baz']})
>>> result in ['foo', 'bar', 'baz']
True
>>> result = lazy_value_by_parameter({'type': 'int'})
>>> result.replace('.', '', 1).lstrip('-').isnumeric() and \
... isinstance(result, str)
True
Exceptions:
Type | Description |
---|---|
ImportError |
|
TypeError |
|
ImportError |
|
Returns:
Type | Description |
---|---|
str | Parameter value. |