|
1 | 1 | # field-manager-python-client |
2 | 2 | A client library for accessing Field Manager Data API |
3 | 3 |
|
4 | | -## Usage |
5 | | -First, create a client: |
| 4 | +## 🚀 Quick Start |
6 | 5 |
|
7 | | -```python |
8 | | -from field_manager_python_client import Client |
| 6 | +### Installation |
9 | 7 |
|
10 | | -client = Client(base_url="https://api.example.com") |
| 8 | +```bash |
| 9 | +pip install field_manager_python_client python-keycloak |
11 | 10 | ``` |
12 | 11 |
|
13 | | -If the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead: |
| 12 | +### Authentication |
| 13 | + |
| 14 | +The easiest way to get started is using the built-in authentication functions: |
14 | 15 |
|
15 | 16 | ```python |
16 | | -from field_manager_python_client import AuthenticatedClient |
| 17 | +from field_manager_python_client import get_prod_client |
17 | 18 |
|
18 | | -client = AuthenticatedClient(base_url="https://api.example.com", token="SuperSecretToken") |
| 19 | +# Authenticate with your Field Manager account |
| 20 | +client = get_prod_client( email="[email protected]") |
19 | 21 | ``` |
20 | 22 |
|
21 | | -Now call your endpoint and use your models: |
| 23 | +### Basic Usage |
22 | 24 |
|
23 | 25 | ```python |
24 | | -from field_manager_python_client.models import MyDataModel |
25 | | -from field_manager_python_client.api.my_tag import get_my_data_model |
26 | | -from field_manager_python_client.types import Response |
| 26 | +from field_manager_python_client.api.projects import get_project_projects_project_id_get |
27 | 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) |
| 28 | +# Use the authenticated client |
| 29 | +project_info = get_project_projects_project_id_get.sync( |
| 30 | + client=client, |
| 31 | + project_id="your-project-id" |
| 32 | +) |
| 33 | +print(f"Project: {project_info.name}") |
32 | 34 | ``` |
33 | 35 |
|
34 | | -Or do the same thing with an async version: |
| 36 | +## 🔐 Authentication Options |
35 | 37 |
|
| 38 | +### 1. Integrated Authentication (Recommended) |
36 | 39 | ```python |
37 | | -from field_manager_python_client.models import MyDataModel |
38 | | -from field_manager_python_client.api.my_tag import get_my_data_model |
39 | | -from field_manager_python_client.types import Response |
| 40 | +from field_manager_python_client import authenticate |
40 | 41 |
|
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) |
| 42 | +client = authenticate( environment="prod", email="[email protected]") |
44 | 43 | ``` |
45 | 44 |
|
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 | | - |
| 45 | +### 2. Manual Token Setup |
48 | 46 | ```python |
| 47 | +from field_manager_python_client import AuthenticatedClient |
| 48 | + |
49 | 49 | client = AuthenticatedClient( |
50 | | - base_url="https://internal_api.example.com", |
51 | | - token="SuperSecretToken", |
52 | | - verify_ssl="/path/to/certificate_bundle.pem", |
| 50 | + base_url="https://app.fieldmanager.io/api/location", |
| 51 | + token="your-access-token" |
53 | 52 | ) |
54 | 53 | ``` |
55 | 54 |
|
56 | | -You can also disable certificate validation altogether, but beware that **this is a security risk**. |
| 55 | +### 3. Service Account (for automation) |
| 56 | +See the [main repository](https://github.com/norwegian-geotechnical-institute/field-manager-python-client) for service account setup. |
| 57 | + |
| 58 | +## 📖 API Usage Patterns |
57 | 59 |
|
| 60 | +### Synchronous Operations |
58 | 61 | ```python |
59 | | -client = AuthenticatedClient( |
60 | | - base_url="https://internal_api.example.com", |
61 | | - token="SuperSecretToken", |
62 | | - verify_ssl=False |
63 | | -) |
| 62 | +from field_manager_python_client.api.organizations import get_organizations_organizations_get |
| 63 | + |
| 64 | +# Simple request - returns data or None |
| 65 | +organizations = get_organizations_organizations_get.sync(client=client) |
| 66 | + |
| 67 | +# Detailed request - returns Response object with status code, headers, etc. |
| 68 | +from field_manager_python_client.types import Response |
| 69 | +response: Response = get_organizations_organizations_get.sync_detailed(client=client) |
| 70 | +if response.status_code == 200: |
| 71 | + organizations = response.parsed |
64 | 72 | ``` |
65 | 73 |
|
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 |
| 74 | +### Asynchronous Operations |
| 75 | +```python |
| 76 | +import asyncio |
| 77 | +from field_manager_python_client.api.organizations import get_organizations_organizations_get |
| 78 | + |
| 79 | +async def fetch_organizations(): |
| 80 | + # Simple async request |
| 81 | + organizations = await get_organizations_organizations_get.asyncio(client=client) |
| 82 | + |
| 83 | + # Detailed async request |
| 84 | + response = await get_organizations_organizations_get.asyncio_detailed(client=client) |
| 85 | + return response.parsed |
| 86 | + |
| 87 | +# Run async function |
| 88 | +organizations = asyncio.run(fetch_organizations()) |
| 89 | +``` |
72 | 90 |
|
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 `field_manager_python_client.api.default` |
| 91 | +## 🛠️ Advanced Features |
76 | 92 |
|
77 | | -## Advanced customizations |
| 93 | +### SSL Configuration |
| 94 | +```python |
| 95 | +from field_manager_python_client import AuthenticatedClient |
78 | 96 |
|
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): |
| 97 | +# Custom certificate bundle |
| 98 | +client = AuthenticatedClient( |
| 99 | + base_url="https://internal.example.com/api", |
| 100 | + token="token", |
| 101 | + verify_ssl="/path/to/certificate_bundle.pem" |
| 102 | +) |
80 | 103 |
|
| 104 | +# Disable SSL verification (not recommended for production) |
| 105 | +client = AuthenticatedClient( |
| 106 | + base_url="https://internal.example.com/api", |
| 107 | + token="token", |
| 108 | + verify_ssl=False |
| 109 | +) |
| 110 | +``` |
| 111 | + |
| 112 | +### Custom HTTP Configuration |
81 | 113 | ```python |
82 | | -from field_manager_python_client import Client |
| 114 | +from field_manager_python_client import AuthenticatedClient |
83 | 115 |
|
84 | 116 | def log_request(request): |
85 | | - print(f"Request event hook: {request.method} {request.url} - Waiting for response") |
| 117 | + print(f"Request: {request.method} {request.url}") |
86 | 118 |
|
87 | 119 | def log_response(response): |
88 | | - request = response.request |
89 | | - print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}") |
| 120 | + print(f"Response: {response.status_code}") |
90 | 121 |
|
91 | | -client = Client( |
| 122 | +client = AuthenticatedClient( |
92 | 123 | base_url="https://api.example.com", |
93 | | - httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}}, |
| 124 | + token="token", |
| 125 | + httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}} |
94 | 126 | ) |
95 | | - |
96 | | -# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client() |
97 | 127 | ``` |
98 | 128 |
|
99 | | -You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url): |
| 129 | +## 📚 Learn More |
100 | 130 |
|
101 | | -```python |
102 | | -import httpx |
103 | | -from field_manager_python_client import Client |
| 131 | +For comprehensive documentation, examples, and contribution guidelines, visit the main repository: |
| 132 | +**[https://github.com/norwegian-geotechnical-institute/field-manager-python-client](https://github.com/norwegian-geotechnical-institute/field-manager-python-client)** |
104 | 133 |
|
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 | | -``` |
| 134 | +The repository includes: |
| 135 | +- 📖 Complete authentication guide |
| 136 | +- 🔧 Advanced usage examples |
| 137 | +- 🚀 Real-world code samples |
| 138 | +- 🤝 Contributing guidelines |
| 139 | +- 📋 Issue tracking and support |
| 140 | + |
| 141 | +## 🔍 API Reference |
| 142 | + |
| 143 | +Every endpoint becomes a Python function with four variants: |
| 144 | +- `sync`: Blocking request returning parsed data or `None` |
| 145 | +- `sync_detailed`: Blocking request returning full `Response` object |
| 146 | +- `asyncio`: Async version of `sync` |
| 147 | +- `asyncio_detailed`: Async version of `sync_detailed` |
| 148 | + |
| 149 | +All path/query parameters and request bodies become function arguments. |
| 150 | +Functions are organized by API tags in `field_manager_python_client.api.*` modules. |
0 commit comments