Skip to content

Commit 7d394d7

Browse files
committed
first try
1 parent 092c171 commit 7d394d7

File tree

132 files changed

+21082
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+21082
-0
lines changed

Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Reference: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
2+
help: ## Show this help message
3+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
4+
5+
# Variable for the OpenAPI specification path
6+
OPENAPI_SPEC=./openapi_specification/nadag-innmelding.json
7+
OPENAPI_SPEC_URL=https://ngu.github.io/mono/nadag-innmelding/openapi/nadag-innmelding.json
8+
9+
# Install the required library using pipx
10+
install: ## Install openapi-python-client using pipx
11+
pipx install openapi-python-client --include-deps
12+
openapi-python-client --install-completion
13+
14+
# Install jq
15+
install_jq: ## Install jq command-line tool
16+
@which jq > /dev/null && echo "jq is already installed." || { \
17+
echo "Installing jq..."; \
18+
if [ "$$(uname)" = "Linux" ]; then \
19+
sudo apt-get update && sudo apt-get install -y jq; \
20+
elif [ "$$(uname)" = "Darwin" ]; then \
21+
brew install jq; \
22+
else \
23+
echo "Please install jq manually."; \
24+
fi \
25+
}
26+
27+
# Clear the log file
28+
clear_log: ## Delete the log file if it exists
29+
@rm -f logs/log openapi_specification/version
30+
31+
# Download the OpenAPI specification
32+
download_openapi: ## Download the OpenAPI specification
33+
@echo "Downloading OpenAPI specification..."
34+
@curl -s $(OPENAPI_SPEC_URL) > $(OPENAPI_SPEC)
35+
36+
format_openapi: download_openapi ## Format the OpenAPI specification
37+
@echo "Formatting OpenAPI specification..."
38+
@npx prettier --write $(OPENAPI_SPEC)
39+
40+
# Get the version from openapi.json and save to openapi_specification/version
41+
get_version: ## Get the version from openapi.json and save to openapi_specification/version
42+
@echo "Fetching version from $(OPENAPI_SPEC)..."
43+
@VERSION=$$(jq -r '.info.version' $(OPENAPI_SPEC)) && \
44+
echo "$$VERSION" > openapi_specification/version
45+
46+
# Run the openapi-python-client generate command
47+
generate: clear_log get_version ## Generate API client from OpenAPI spec
48+
@echo "Generating API client..."
49+
openapi-python-client --version > logs/log 2>&1
50+
openapi-python-client generate --path $(OPENAPI_SPEC) --config config.yaml >> logs/log 2>&1
51+
52+
# A shortcut to install dependencies and then generate the client
53+
all: install install_jq generate ## Install dependencies and generate client

config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
project_name_override: nadag-innmelding-python-client
2+
package_name_override: nadag_innmelding_python_client
3+
# package_version_override:
4+
post_hooks:
5+
# - "bash -c 'cd field-manager-python-client && poetry install'"

logs/log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
openapi-python-client version: 0.21.6
2+
Generating /home/jiyang/github/ngi/nadag-python-client/nadag-innmelding-python-client
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
__pycache__/
2+
build/
3+
dist/
4+
*.egg-info/
5+
.pytest_cache/
6+
7+
# pyenv
8+
.python-version
9+
10+
# Environments
11+
.env
12+
.venv
13+
14+
# mypy
15+
.mypy_cache/
16+
.dmypy.json
17+
dmypy.json
18+
19+
# JetBrains
20+
.idea/
21+
22+
/coverage.xml
23+
/.coverage
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# nadag-innmelding-python-client
2+
A client library for accessing Generated API
3+
4+
## Usage
5+
First, create a client:
6+
7+
```python
8+
from nadag_innmelding_python_client import Client
9+
10+
client = Client(base_url="https://api.example.com")
11+
```
12+
13+
If the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead:
14+
15+
```python
16+
from nadag_innmelding_python_client import AuthenticatedClient
17+
18+
client = AuthenticatedClient(base_url="https://api.example.com", token="SuperSecretToken")
19+
```
20+
21+
Now call your endpoint and use your models:
22+
23+
```python
24+
from nadag_innmelding_python_client.models import MyDataModel
25+
from nadag_innmelding_python_client.api.my_tag import get_my_data_model
26+
from nadag_innmelding_python_client.types import Response
27+
28+
with client as client:
29+
my_data: MyDataModel = get_my_data_model.sync(client=client)
30+
# or if you need more info (e.g. status_code)
31+
response: Response[MyDataModel] = get_my_data_model.sync_detailed(client=client)
32+
```
33+
34+
Or do the same thing with an async version:
35+
36+
```python
37+
from nadag_innmelding_python_client.models import MyDataModel
38+
from nadag_innmelding_python_client.api.my_tag import get_my_data_model
39+
from nadag_innmelding_python_client.types import Response
40+
41+
async with client as client:
42+
my_data: MyDataModel = await get_my_data_model.asyncio(client=client)
43+
response: Response[MyDataModel] = await get_my_data_model.asyncio_detailed(client=client)
44+
```
45+
46+
By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.
47+
48+
```python
49+
client = AuthenticatedClient(
50+
base_url="https://internal_api.example.com",
51+
token="SuperSecretToken",
52+
verify_ssl="/path/to/certificate_bundle.pem",
53+
)
54+
```
55+
56+
You can also disable certificate validation altogether, but beware that **this is a security risk**.
57+
58+
```python
59+
client = AuthenticatedClient(
60+
base_url="https://internal_api.example.com",
61+
token="SuperSecretToken",
62+
verify_ssl=False
63+
)
64+
```
65+
66+
Things to know:
67+
1. Every path/method combo becomes a Python module with four functions:
68+
1. `sync`: Blocking request that returns parsed data (if successful) or `None`
69+
1. `sync_detailed`: Blocking request that always returns a `Request`, optionally with `parsed` set if the request was successful.
70+
1. `asyncio`: Like `sync` but async instead of blocking
71+
1. `asyncio_detailed`: Like `sync_detailed` but async instead of blocking
72+
73+
1. All path/query params, and bodies become method arguments.
74+
1. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above)
75+
1. Any endpoint which did not have a tag will be in `nadag_innmelding_python_client.api.default`
76+
77+
## Advanced customizations
78+
79+
There are more settings on the generated `Client` class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying `httpx.Client` or `httpx.AsyncClient` (depending on your use-case):
80+
81+
```python
82+
from nadag_innmelding_python_client import Client
83+
84+
def log_request(request):
85+
print(f"Request event hook: {request.method} {request.url} - Waiting for response")
86+
87+
def log_response(response):
88+
request = response.request
89+
print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}")
90+
91+
client = Client(
92+
base_url="https://api.example.com",
93+
httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}},
94+
)
95+
96+
# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()
97+
```
98+
99+
You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):
100+
101+
```python
102+
import httpx
103+
from nadag_innmelding_python_client import Client
104+
105+
client = Client(
106+
base_url="https://api.example.com",
107+
)
108+
# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.
109+
client.set_httpx_client(httpx.Client(base_url="https://api.example.com", proxies="http://localhost:8030"))
110+
```
111+
112+
## Building / publishing this package
113+
This project uses [Poetry](https://python-poetry.org/) to manage dependencies and packaging. Here are the basics:
114+
1. Update the metadata in pyproject.toml (e.g. authors, version)
115+
1. If you're using a private repository, configure it with Poetry
116+
1. `poetry config repositories.<your-repository-name> <url-to-your-repository>`
117+
1. `poetry config http-basic.<your-repository-name> <username> <password>`
118+
1. Publish the client with `poetry publish --build -r <your-repository-name>` or, if for public PyPI, just `poetry publish --build`
119+
120+
If you want to install this client into another project without publishing it (e.g. for development) then:
121+
1. If that project **is using Poetry**, you can simply do `poetry add <path-to-this-client>` from that project
122+
1. If that project is not using Poetry:
123+
1. Build a wheel with `poetry build -f wheel`
124+
1. Install that wheel from the other project `pip install <path-to-wheel>`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""A client library for accessing Generated API"""
2+
3+
from .client import AuthenticatedClient, Client
4+
5+
__all__ = (
6+
"AuthenticatedClient",
7+
"Client",
8+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Contains methods for accessing the API"""

nadag-innmelding-python-client/nadag_innmelding_python_client/api/default/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from http import HTTPStatus
2+
from typing import Any, Dict, Optional, Union
3+
4+
import httpx
5+
6+
from ... import errors
7+
from ...client import AuthenticatedClient, Client
8+
from ...types import Response
9+
10+
11+
def _get_kwargs(
12+
geoteknisk_unders_id: str,
13+
) -> Dict[str, Any]:
14+
_kwargs: Dict[str, Any] = {
15+
"method": "delete",
16+
"url": f"/v1/GeotekniskUnders/{geoteknisk_unders_id}",
17+
}
18+
19+
return _kwargs
20+
21+
22+
def _parse_response(*, client: Union[AuthenticatedClient, Client], response: httpx.Response) -> Optional[Any]:
23+
if response.status_code == 204:
24+
return None
25+
if client.raise_on_unexpected_status:
26+
raise errors.UnexpectedStatus(response.status_code, response.content)
27+
else:
28+
return None
29+
30+
31+
def _build_response(*, client: Union[AuthenticatedClient, Client], response: httpx.Response) -> Response[Any]:
32+
return Response(
33+
status_code=HTTPStatus(response.status_code),
34+
content=response.content,
35+
headers=response.headers,
36+
parsed=_parse_response(client=client, response=response),
37+
)
38+
39+
40+
def sync_detailed(
41+
geoteknisk_unders_id: str,
42+
*,
43+
client: AuthenticatedClient,
44+
) -> Response[Any]:
45+
"""Deletes a GeotekniskUnders.
46+
47+
Deletes a GeotekniskUnders.
48+
49+
Args:
50+
geoteknisk_unders_id (str):
51+
52+
Raises:
53+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
54+
httpx.TimeoutException: If the request takes longer than Client.timeout.
55+
56+
Returns:
57+
Response[Any]
58+
"""
59+
60+
kwargs = _get_kwargs(
61+
geoteknisk_unders_id=geoteknisk_unders_id,
62+
)
63+
64+
response = client.get_httpx_client().request(
65+
**kwargs,
66+
)
67+
68+
return _build_response(client=client, response=response)
69+
70+
71+
async def asyncio_detailed(
72+
geoteknisk_unders_id: str,
73+
*,
74+
client: AuthenticatedClient,
75+
) -> Response[Any]:
76+
"""Deletes a GeotekniskUnders.
77+
78+
Deletes a GeotekniskUnders.
79+
80+
Args:
81+
geoteknisk_unders_id (str):
82+
83+
Raises:
84+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
85+
httpx.TimeoutException: If the request takes longer than Client.timeout.
86+
87+
Returns:
88+
Response[Any]
89+
"""
90+
91+
kwargs = _get_kwargs(
92+
geoteknisk_unders_id=geoteknisk_unders_id,
93+
)
94+
95+
response = await client.get_async_httpx_client().request(**kwargs)
96+
97+
return _build_response(client=client, response=response)

0 commit comments

Comments
 (0)