harborapi.utils
harborapi.utils
defines utility functions for use with harborapi
.
is_json(response)
Determines if a response body has a json content type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Response
|
The HTTPX response to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in harborapi/utils.py
handle_optional_json_response(resp)
Takes in a response and attempts to parse the body as JSON.
If the response cannot be parsed, an exception is raised.
If the response has no body, None
is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resp |
Response
|
The HTTPX response to parse. |
required |
Returns:
Type | Description |
---|---|
Optional[JSONType]
|
The parsed JSON, or |
Raises:
Type | Description |
---|---|
HarborAPIException
|
Raised if the response body cannot be parsed as JSON.
The |
Source code in harborapi/utils.py
urlencode_repo(repository_name)
URL-encode a repository name. The Harbor API requires names to be double URL-encoded for some reason. So we have to manually encode it here, and then let HTTPX encode it again when we make the request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repository_name |
str
|
The repository name to encode. |
required |
Returns:
Type | Description |
---|---|
str
|
The encoded repository name. |
Source code in harborapi/utils.py
urldecode_header(response, key)
URL decode a value of a specific key from a response's headers.
Returns the decoded value, or an empty string if the header key is not present.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Response
|
The HTTPX response to parse. |
required |
key |
str
|
The header key to decode. |
required |
Returns:
Type | Description |
---|---|
str
|
The decoded header value, or an empty string if the header key is not present. |
Source code in harborapi/utils.py
get_repo_path(project_name, repository_name)
Get a Harbor repository path given a project name and a repository name.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_name |
str
|
The project name |
required |
repository_name |
str
|
The repository name |
required |
Returns:
Type | Description |
---|---|
str
|
The repository path |
Source code in harborapi/utils.py
get_artifact_path(project_name, repository_name, reference)
Get artifact path given a project name, repo name and a reference (tag or digest)
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_name |
str
|
The project name |
required |
repository_name |
str
|
The repository name |
required |
reference |
str
|
The tag or digest of the artifact |
required |
Returns:
Type | Description |
---|---|
str
|
The artifact path |
Source code in harborapi/utils.py
get_basicauth(username, secret)
Get HTTP basic access authentication credentials given a username and a secret.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username |
str
|
The username to use for authentication. |
required |
secret |
str
|
The secret (password) for the user. |
required |
Returns:
Type | Description |
---|---|
SecretStr
|
The credentials string used for HTTP basic access authentication,
encoded in base64 as a Pydantic SecretStr, which prevents the
credentials from leaking when printing locals.
The string is a base64 encoded string of the form |
Source code in harborapi/utils.py
parse_pagination_url(url, strip=True)
Parse pagination URL and return the next URL
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The pagination URL to parse |
required |
strip |
bool
|
Whether to strip the /api/v2.x/ path from the URL |
True
|
Returns:
Type | Description |
---|---|
Optional[str]
|
The next URL, or |
Source code in harborapi/utils.py
get_project_headers(project_name_or_id)
Get HTTP header for identifying whether a Project Name or Project ID is used in an API call.
If the value is an integer, it is assumed to be the Project ID.
Otherwise, it is assumed to be the Project Name.
This determines the value of the X-Is-Resource-Name
header.
X-Is-Resource-Name: true
means the value is a project name,
X-Is-Resource-Name: false
means the value is a project ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_name_or_id |
Union[str, int]
|
The project name or ID. |
required |
Returns:
Type | Description |
---|---|
Dict[str, str]
|
The headers to use for the request. |
Source code in harborapi/utils.py
get_params(**kwargs)
Get parameters for an API call as a dict, where None
values are ignored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
QueryParamValue
|
The parameters to use for the request. Each keyword argument type must be a primitive, JSON-serializable type. |
{}
|
Returns:
Type | Description |
---|---|
QueryParamMapping
|
The dict representation of the parameters with |