Utilities¶
utils
¶
Cookie
¶
Represents a single HTTP cookie.
Source code in src/griddy/core/utils/cookies.py
matches_domain
¶
Check if this cookie matches the given domain.
Source code in src/griddy/core/utils/cookies.py
matches_path
¶
to_dict
¶
OpenEnumMeta
¶
Bases: EnumMeta
Enum metaclass that returns the raw value instead of raising ValueError for unknown members.
HarEntryPathManager
¶
Aggregates HAR entries for a single API path, collecting headers and query params.
Source code in src/griddy/core/utils/har.py
as_dict
¶
Return a dict representation, excluding specified attributes.
Source code in src/griddy/core/utils/har.py
add_entry
¶
Merge a HAR entry's headers, query params, and response into this manager.
Source code in src/griddy/core/utils/har.py
FieldMetadata
¶
FieldMetadata(
security=None,
path=None,
query=None,
header=None,
request=None,
form=None,
multipart=None,
)
Aggregates all parameter metadata types for a single Pydantic field.
Source code in src/griddy/core/utils/metadata.py
FormMetadata
dataclass
¶
Metadata for URL-encoded form fields.
HeaderMetadata
dataclass
¶
Bases: ParamMetadata
Metadata for HTTP header parameters.
MultipartFormMetadata
dataclass
¶
Metadata for multipart form fields.
PathParamMetadata
dataclass
¶
Bases: ParamMetadata
Metadata for URL path parameters.
QueryParamMetadata
dataclass
¶
Bases: ParamMetadata
Metadata for URL query parameters.
RequestMetadata
dataclass
¶
Metadata for request body media type.
SecurityMetadata
dataclass
¶
Metadata describing a security field (API key, OAuth2, HTTP auth, etc.).
get_field_name
¶
SerializedRequestBody
dataclass
¶
Container for a serialized HTTP request body with media type and content.
BackoffStrategy
¶
Exponential backoff configuration with initial/max intervals and exponent.
Source code in src/griddy/core/utils/retries.py
Retries
¶
RetryConfig
¶
Retry strategy configuration with backoff settings.
Source code in src/griddy/core/utils/retries.py
YAMLConsolidator
¶
Consolidates multiple OpenAPI YAML spec files into a single spec, tracking diffs.
Source code in src/griddy/core/utils/yaml_consolidator.py
add_diff_entry
¶
Record a diff when a key's value changes between specs.
Source code in src/griddy/core/utils/yaml_consolidator.py
add_spec
¶
Load and register a YAML spec file.
Source code in src/griddy/core/utils/yaml_consolidator.py
combine_all_specs
¶
Integrate all loaded specs into a single combined spec.
Source code in src/griddy/core/utils/yaml_consolidator.py
compute_diff_info
¶
Compute HTML diff and similarity ratio for a diff entry.
Source code in src/griddy/core/utils/yaml_consolidator.py
create_full_html_string
¶
Build an HTML string from a list of diff entries.
Source code in src/griddy/core/utils/yaml_consolidator.py
get_open_api_attr
¶
Get a per-spec attribute map, loading lazily if needed.
get_sorted_spec
¶
Sort a spec's paths, components, and tags alphabetically.
Source code in src/griddy/core/utils/yaml_consolidator.py
handle_component_diffs
¶
Build HTML diff strings for schema and security scheme diffs.
Source code in src/griddy/core/utils/yaml_consolidator.py
integrate_attr
¶
Merge a single top-level spec attribute into the consolidated spec.
Source code in src/griddy/core/utils/yaml_consolidator.py
integrate_components
¶
Merge component schemas and security schemes into the consolidated spec.
Source code in src/griddy/core/utils/yaml_consolidator.py
integrate_spec
¶
Merge all attributes of a single spec into the consolidated spec.
integrate_tags
¶
Merge tags into the consolidated spec, recording diffs.
Source code in src/griddy/core/utils/yaml_consolidator.py
load_specs
¶
Load all specs matching the pattern from the spec directory.
Source code in src/griddy/core/utils/yaml_consolidator.py
output_diff
¶
Generate and write an HTML diff report for all spec differences.
Source code in src/griddy/core/utils/yaml_consolidator.py
set_common_info
¶
Set common OpenAPI attributes (info, servers, security) via keyword arguments.
sort_all_specs
¶
sort_entries_for_attr
¶
Sort entries for a given spec attribute (paths, components, or tags).
Source code in src/griddy/core/utils/yaml_consolidator.py
write_spec_to_disk
¶
Write a single spec dict to a YAML file.
Source code in src/griddy/core/utils/yaml_consolidator.py
write_to_disk
¶
Write all loaded specs to YAML files in the given directory.
Source code in src/griddy/core/utils/yaml_consolidator.py
write_to_html
¶
get_discriminator
¶
Recursively search for the discriminator attribute in a model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Any
|
The model to search within. |
required |
fieldname
|
str
|
The name of the field to search for. |
required |
key
|
str
|
The key to search for in dictionaries. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The name of the discriminator attribute. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the discriminator attribute is not found. |
Source code in src/griddy/core/utils/annotations.py
clean_text
¶
Clean and normalize text data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str | None
|
Text to clean |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Cleaned text or None |
Source code in src/griddy/core/utils/converters.py
safe_float
¶
Safely convert value to float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
Float value or None |
Source code in src/griddy/core/utils/converters.py
safe_int
¶
Safely convert value to integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
Integer value or None |
Source code in src/griddy/core/utils/converters.py
safe_numberify
¶
Attempt to convert a string value to int, then float, falling back to the original string if neither conversion succeeds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
String value to convert |
required |
Returns:
| Type | Description |
|---|---|
int | float | str | None
|
int, float, original string, or None |
Source code in src/griddy/core/utils/converters.py
cookies_to_dict
¶
Convert a list of cookies to a dictionary format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cookies
|
list[Cookie]
|
List of Cookie objects |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary with cookie names as keys and values as values |
Source code in src/griddy/core/utils/cookies.py
cookies_to_header
¶
Convert a list of cookies to a Cookie header string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cookies
|
list[Cookie]
|
List of Cookie objects |
required |
Returns:
| Type | Description |
|---|---|
str
|
Cookie header string (e.g., "name1=value1; name2=value2") |
Source code in src/griddy/core/utils/cookies.py
extract_cookies_as_dict
¶
Extract cookies for a URL and return as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cookies_file
|
str | Path
|
Path to the cookies.txt file |
required |
target_url
|
str
|
URL to match cookies against |
required |
include_expired
|
bool
|
Whether to include expired cookies |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary with cookie names as keys and values as values |
Source code in src/griddy/core/utils/cookies.py
extract_cookies_as_header
¶
Extract cookies for a URL and return as a Cookie header string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cookies_file
|
str | Path
|
Path to the cookies.txt file |
required |
target_url
|
str
|
URL to match cookies against |
required |
include_expired
|
bool
|
Whether to include expired cookies |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Cookie header string suitable for HTTP requests |
Source code in src/griddy/core/utils/cookies.py
extract_cookies_for_url
¶
Extract cookies that match a specific URL from a cookies.txt file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cookies_file
|
str | Path
|
Path to the cookies.txt file |
required |
target_url
|
str
|
URL to match cookies against |
required |
include_expired
|
bool
|
Whether to include expired cookies |
False
|
Returns:
| Type | Description |
|---|---|
list[Cookie]
|
List of matching Cookie objects |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the cookies file doesn't exist |
ValueError
|
If the URL or file format is invalid |
Source code in src/griddy/core/utils/cookies.py
parse_cookies_txt
¶
Parse a cookies.txt file and return a list of Cookie objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the cookies.txt file |
required |
Returns:
| Type | Description |
|---|---|
list[Cookie]
|
List of Cookie objects |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the cookies file doesn't exist |
ValueError
|
If the file format is invalid |
Source code in src/griddy/core/utils/cookies.py
parse_date
¶
Parse date string into datetime object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_str
|
str | None
|
Date string in various formats |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
Parsed datetime object or None |
Source code in src/griddy/core/utils/datetimes.py
parse_datetime
¶
Convert a RFC 3339 / ISO 8601 formatted string into a datetime object. Python versions 3.11 and later support parsing RFC 3339 directly with datetime.fromisoformat(), but for earlier versions, this function encapsulates the necessary extra logic.
Source code in src/griddy/core/utils/datetimes.py
consolidate_minified_entries
¶
Group minified HAR entries by API path into HarEntryPathManager instances.
Source code in src/griddy/core/utils/har.py
extract_minified_har_entry
¶
Extract a minimal request/response pair from a HAR entry.
Source code in src/griddy/core/utils/har.py
minify_har
¶
Read a HAR file and return a list of minified request/response entries.
Source code in src/griddy/core/utils/har.py
write_consolidated_to_files
¶
Write each consolidated path manager to a JSON file.
Source code in src/griddy/core/utils/har.py
get_headers
¶
Extract HTTP headers from request params and globals.
Source code in src/griddy/core/utils/headers.py
get_response_headers
¶
Convert httpx Headers to a dict mapping header names to lists of values.
Source code in src/griddy/core/utils/headers.py
get_body_content
¶
Return the request body content as a string, or a placeholder for streaming.
get_default_logger
¶
Get a default logger, optionally enabled via an environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_var
|
str
|
Environment variable name to check for debug logging. |
'GRIDDY_DEBUG'
|
Source code in src/griddy/core/utils/logger.py
find_metadata
¶
Find the first metadata annotation of the given type on a field.
Source code in src/griddy/core/utils/metadata.py
get_query_params
¶
Extract query parameters from request params and globals.
Source code in src/griddy/core/utils/queryparams.py
serialize_request_body
¶
serialize_request_body(
request_body,
nullable,
optional,
serialization_method,
request_body_type,
)
Serialize a request body based on the given serialization method (json, form, multipart, etc.).
Source code in src/griddy/core/utils/requestbodies.py
retry
¶
Execute func with optional backoff retry logic based on retries config.
Source code in src/griddy/core/utils/retries.py
retry_async
async
¶
Async variant of retry with optional backoff retry logic.
Source code in src/griddy/core/utils/retries.py
retry_on_rate_limit
¶
Decorator to retry function calls on rate limit errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_retries
|
int
|
Maximum number of retry attempts |
3
|
backoff_factor
|
float
|
Factor for exponential backoff |
1.0
|
Source code in src/griddy/core/utils/retries.py
get_security
¶
Extract security headers and query params from a Pydantic security model.
Source code in src/griddy/core/utils/security.py
get_security_from_env
¶
Resolve security from environment variables if not already set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
security
|
Any
|
Existing security instance, or None. |
required |
security_class
|
Any
|
The Pydantic security model class. |
required |
env_mapping
|
Optional[Dict[str, str]]
|
Dict mapping field names to environment variable names. If None, no env vars are checked. |
None
|
Source code in src/griddy/core/utils/security.py
get_pydantic_model
¶
Return data as-is if it already contains Pydantic models, otherwise unmarshal.
marshal_json
¶
Serialize a value to a JSON string using a Pydantic model wrapper.
Source code in src/griddy/core/utils/serializers.py
serialize_decimal
¶
Return a Pydantic serializer for Decimal values.
Source code in src/griddy/core/utils/serializers.py
serialize_float
¶
Return a Pydantic serializer for float values.
Source code in src/griddy/core/utils/serializers.py
serialize_int
¶
Return a Pydantic serializer for int values.
Source code in src/griddy/core/utils/serializers.py
stream_to_bytes
¶
stream_to_bytes_async
async
¶
stream_to_text
¶
stream_to_text_async
async
¶
Async read a streaming HTTP response into a single text string.
unmarshal
¶
Coerce a parsed value into the given Pydantic-compatible type.
Source code in src/griddy/core/utils/serializers.py
unmarshal_json
¶
validate_const
¶
Return a validator that enforces a constant value.
Source code in src/griddy/core/utils/serializers.py
validate_decimal
¶
Validate and coerce a value to Decimal.
Source code in src/griddy/core/utils/serializers.py
validate_float
¶
Validate and coerce a value to float.
Source code in src/griddy/core/utils/serializers.py
validate_int
¶
Validate and coerce a value to int.
Source code in src/griddy/core/utils/serializers.py
validate_open_enum
¶
Return a validator that accepts int or str enum values.
Source code in src/griddy/core/utils/serializers.py
build_url
¶
Build URL from base URL, path, and parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
Base URL |
required |
path
|
str
|
URL path |
required |
params
|
dict[str, any] | None
|
Query parameters |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Complete URL |
Source code in src/griddy/core/utils/url.py
generate_url
¶
Generate a full URL by interpolating path params into the path template.
Source code in src/griddy/core/utils/url.py
remove_suffix
¶
Remove a suffix from a string if present.
template_url
¶
Replace {key} placeholders in a URL with the given param values.
Source code in src/griddy/core/utils/url.py
cast_partial
¶
get_global_from_env
¶
Return value if set, otherwise read from environment and cast.
Source code in src/griddy/core/utils/values.py
match_content_type
¶
Check if a content type matches a pattern (exact, wildcard, or partial).
Source code in src/griddy/core/utils/values.py
match_response
¶
Check if an HTTP response matches the given status codes and content type.
Source code in src/griddy/core/utils/values.py
match_status_codes
¶
Check if a status code matches any of the given patterns (exact or wildcard like 4XX).