prance.util.url¶
This submodule contains code for fetching/parsing URLs.
Exceptions¶
Functions¶
- prance.util.url.absurl(url, relative_to=None)[source]¶
Turn relative file URLs into absolute file URLs.
This is necessary, because while JSON pointers do not allow relative file URLs, Swagger/OpenAPI explicitly does. We need to make relative paths absolute before passing them off to jsonschema for verification.
Non-file URLs are left untouched. URLs without scheme are assumed to be file URLs.
- Parameters
url (str/tuple) – The input URL.
relative_to (str/tuple) – [optional] The URL to which the input URL is relative.
- Returns
The output URL, parsed into components.
- Return type
tuple
- prance.util.url.fetch_url(url, cache={}, encoding=None, strict=True)[source]¶
Fetch the URL and parse the contents.
Same as fetch_url_text(), but also parses the content and only returns the parse results.
- Parameters
url (tuple) – The url, parsed as returned by absurl above.
cache (Mapping) – An optional cache. If the URL can be found in the cache, return the cache contents.
encoding (str) – Provide an encoding for local URLs to override encoding detection, if desired. Defaults to None.
- Returns
The parsed file.
- Return type
dict
- prance.util.url.fetch_url_text(url, cache={}, encoding=None)[source]¶
Fetch the URL.
If the URL is a file URL, the format used for parsing depends on the file extension. Otherwise, YAML is assumed.
The URL may also use the python scheme. In this scheme, the netloc part refers to an importable python package, and the path part to a path relative to the package path, e.g. python://some_package/path/to/file.yaml.
- Parameters
url (tuple) – The url, parsed as returned by absurl above.
cache (Mapping) – An optional cache. If the URL can be found in the cache, return the cache contents.
encoding (str) – Provide an encoding for local URLs to override encoding detection, if desired. Defaults to None.
- Returns
The resource text of the URL, and the content type.
- Return type
tuple
- prance.util.url.split_url_reference(base_url, reference)[source]¶
Return a normalized, parsed URL and object path.
The reference string is a JSON reference, i.e. a URL with a fragment that contains an object path into the referenced resource.
The base URL is used as a reference point for relative references.
- Parameters
base_url (mixed) – A parsed URL.
reference (str) – A JSON reference string.
- Returns
The parsed absolute URL of the reference and the object path.