Skip to content

Commit 901dd81

Browse files
committed
Lint
1 parent 37ee605 commit 901dd81

File tree

1 file changed

+146
-142
lines changed

1 file changed

+146
-142
lines changed

docs/docs/python-sdk/guides/client.mdx

Lines changed: 146 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ If you prefer to jump right in, check out the ["Hello World" example](#hello-wor
2525

2626
:::info Asynchronous vs. Synchronous Clients
2727
The SDK offers both asynchronous and synchronous client implementations. Choose the one that best fits your application architecture:
28+
2829
- **Asynchronous client** (`InfrahubClient`): Ideal for modern async applications using Python's `async`/`await` syntax
2930
- **Synchronous client** (`InfrahubClientSync`): Better for traditional synchronous workflows or scripts
31+
3032
:::
3133

3234
<Tabs groupId="async-sync">
@@ -62,6 +64,7 @@ Next, configure the address of your Infrahub instance and set up authentication
6264

6365
:::info Configuration Methods
6466
You can configure the client in two ways:
67+
6568
1. **Environment variables**: Ideal for sensitive information
6669
2. **`Config` object**: Better for explicit configuration in code
6770

@@ -75,131 +78,131 @@ You can find the full list of configuration options in the [SDK configuration re
7578
API tokens provide secure, long-lived authentication and are the recommended method for most use cases, especially for automation scripts and non-interactive applications.
7679

7780
<Tabs groupId="async-sync">
78-
<TabItem value="Async" default>
79-
<Tabs groupId="env-config">
80-
<TabItem value="Config" default>
81+
<TabItem value="Async" default>
82+
<Tabs groupId="env-config">
83+
<TabItem value="Config" default>
8184

82-
We can set the address and API token directly in the `Config` object:
85+
We can set the address and API token directly in the `Config` object:
8386

84-
```python
85-
from infrahub_sdk import Config, InfrahubClient
86-
client = InfrahubClient(config=Config(address="http://localhost:8000", api_token="token"))
87-
```
87+
```python
88+
from infrahub_sdk import Config, InfrahubClient
89+
client = InfrahubClient(config=Config(address="http://localhost:8000", api_token="token"))
90+
```
8891

89-
</TabItem>
90-
<TabItem value="Environment">
92+
</TabItem>
93+
<TabItem value="Environment">
9194

92-
We need to export the address and API token as an environment variable:
95+
We need to export the address and API token as an environment variable:
9396

94-
```bash
95-
export INFRAHUB_ADDRESS="http://localhost:8000"
96-
export INFRAHUB_API_TOKEN="token"
97-
```
97+
```bash
98+
export INFRAHUB_ADDRESS="http://localhost:8000"
99+
export INFRAHUB_API_TOKEN="token"
100+
```
98101

99-
```python
100-
from infrahub_sdk import InfrahubClient
101-
client = InfrahubClient() # token and address are read from the INFRAHUB_API_TOKEN and INFRAHUB_ADDRESS environment variables
102-
```
102+
```python
103+
from infrahub_sdk import InfrahubClient
104+
client = InfrahubClient() # token and address are read from the INFRAHUB_API_TOKEN and INFRAHUB_ADDRESS environment variables
105+
```
103106

104-
</TabItem>
105-
</Tabs>
106-
</TabItem>
107-
<TabItem value="Sync">
108-
<Tabs groupId="env-config">
109-
<TabItem value="Config" default>
107+
</TabItem>
108+
</Tabs>
109+
</TabItem>
110+
<TabItem value="Sync">
111+
<Tabs groupId="env-config">
112+
<TabItem value="Config" default>
110113

111-
We can set the address and API token directly in the `Config` object:
114+
We can set the address and API token directly in the `Config` object:
112115

113-
```python
114-
from infrahub_sdk import Config, InfrahubClientSync
115-
client = InfrahubClientSync(config=Config(address="http://localhost:8000", api_token="token"))
116-
```
116+
```python
117+
from infrahub_sdk import Config, InfrahubClientSync
118+
client = InfrahubClientSync(config=Config(address="http://localhost:8000", api_token="token"))
119+
```
117120

118-
</TabItem>
119-
<TabItem value="Environment">
121+
</TabItem>
122+
<TabItem value="Environment">
120123

121-
We need to export the address and API token as an environment variable:
124+
We need to export the address and API token as an environment variable:
122125

123-
```bash
124-
export INFRAHUB_ADDRESS="http://localhost:8000"
125-
export INFRAHUB_API_TOKEN="token"
126-
```
126+
```bash
127+
export INFRAHUB_ADDRESS="http://localhost:8000"
128+
export INFRAHUB_API_TOKEN="token"
129+
```
127130

128-
```python
129-
from infrahub_sdk import InfrahubClientSync
130-
client = InfrahubClientSync() # token and address are read from the INFRAHUB_API_TOKEN and INFRAHUB_ADDRESS environment variables
131-
```
131+
```python
132+
from infrahub_sdk import InfrahubClientSync
133+
client = InfrahubClientSync() # token and address are read from the INFRAHUB_API_TOKEN and INFRAHUB_ADDRESS environment variables
134+
```
132135

133-
</TabItem>
134-
</Tabs>
135-
</TabItem>
136+
</TabItem>
137+
</Tabs>
138+
</TabItem>
136139
</Tabs>
137140

138141
### Using username/password
139142

140143
For interactive authentication, use username and password. The SDK automatically handles JWT token generation and refresh.
141144

142145
<Tabs groupId="async-sync">
143-
<TabItem value="Async" default>
144-
<Tabs groupId="env-config">
145-
<TabItem value="Config" default>
146+
<TabItem value="Async" default>
147+
<Tabs groupId="env-config">
148+
<TabItem value="Config" default>
146149

147-
We can set the address, username and password directly in the `Config` object:
150+
We can set the address, username and password directly in the `Config` object:
148151

149-
```python
150-
from infrahub_sdk import Config, InfrahubClient
151-
client = InfrahubClient(config=Config(address="http://localhost:8000", username="admin", password="infrahub"))
152-
```
152+
```python
153+
from infrahub_sdk import Config, InfrahubClient
154+
client = InfrahubClient(config=Config(address="http://localhost:8000", username="admin", password="infrahub"))
155+
```
153156

154-
</TabItem>
155-
<TabItem value="Environment">
157+
</TabItem>
158+
<TabItem value="Environment">
156159

157-
We need to export the address, username and password as environment variables:
160+
We need to export the address, username and password as environment variables:
158161

159-
```bash
160-
export INFRAHUB_ADDRESS="http://localhost:8000"
161-
export INFRAHUB_USERNAME="admin"
162-
export INFRAHUB_PASSWORD="infrahub"
163-
```
162+
```bash
163+
export INFRAHUB_ADDRESS="http://localhost:8000"
164+
export INFRAHUB_USERNAME="admin"
165+
export INFRAHUB_PASSWORD="infrahub"
166+
```
164167

165-
```python
166-
from infrahub_sdk import InfrahubClient
167-
client = InfrahubClient() # username and password are read from the INFRAHUB_USERNAME and INFRAHUB_PASSWORD environment variables
168-
```
168+
```python
169+
from infrahub_sdk import InfrahubClient
170+
client = InfrahubClient() # username and password are read from the INFRAHUB_USERNAME and INFRAHUB_PASSWORD environment variables
171+
```
169172

170-
</TabItem>
171-
</Tabs>
172-
</TabItem>
173-
<TabItem value="Sync">
174-
<Tabs groupId="env-config">
175-
<TabItem value="Config" default>
173+
</TabItem>
174+
</Tabs>
175+
</TabItem>
176+
<TabItem value="Sync">
177+
<Tabs groupId="env-config">
178+
<TabItem value="Config" default>
176179

177-
We can set the address, username and password directly in the `Config` object:
180+
We can set the address, username and password directly in the `Config` object:
178181

179-
```python
180-
from infrahub_sdk import Config, InfrahubClientSync
181-
client = InfrahubClientSync(config=Config(address="http://localhost:8000", username="admin", password="infrahub"))
182-
```
182+
```python
183+
from infrahub_sdk import Config, InfrahubClientSync
184+
client = InfrahubClientSync(config=Config(address="http://localhost:8000", username="admin", password="infrahub"))
185+
```
183186

184-
</TabItem>
185-
<TabItem value="Environment">
187+
</TabItem>
188+
<TabItem value="Environment">
186189

187-
We need to export the address, username and password as environment variables:
190+
We need to export the address, username and password as environment variables:
188191

189-
```bash
190-
export INFRAHUB_ADDRESS="http://localhost:8000"
191-
export INFRAHUB_USERNAME="admin"
192-
export INFRAHUB_PASSWORD="infrahub"
193-
```
192+
```bash
193+
export INFRAHUB_ADDRESS="http://localhost:8000"
194+
export INFRAHUB_USERNAME="admin"
195+
export INFRAHUB_PASSWORD="infrahub"
196+
```
194197

195-
```python
196-
from infrahub_sdk import InfrahubClientSync
197-
client = InfrahubClientSync() # username and password are read from the INFRAHUB_USERNAME and INFRAHUB_PASSWORD environment variables
198-
```
198+
```python
199+
from infrahub_sdk import InfrahubClientSync
200+
client = InfrahubClientSync() # username and password are read from the INFRAHUB_USERNAME and INFRAHUB_PASSWORD environment variables
201+
```
199202

200-
</TabItem>
201-
</Tabs>
202-
</TabItem>
203+
</TabItem>
204+
</Tabs>
205+
</TabItem>
203206
</Tabs>
204207

205208
:::success
@@ -211,35 +214,35 @@ Your client is now configured and ready to use!
211214
Configure your client to work with a specific Infrahub branch instead of the default `main` branch. This is particularly useful when working on feature branches or experimental changes, as it eliminates the need to specify the branch name in every method call.
212215

213216
<Tabs groupId="env-config">
214-
<TabItem value="Config" default>
217+
<TabItem value="Config" default>
215218

216-
```python
217-
from infrahub_sdk import InfrahubClient, Config
218-
config = Config(address="http://localhost:8000", api_token="token", default_branch="other-branch")
219-
client_other_branch = InfrahubClient(config=config)
219+
```python
220+
from infrahub_sdk import InfrahubClient, Config
221+
config = Config(address="http://localhost:8000", api_token="token", default_branch="other-branch")
222+
client_other_branch = InfrahubClient(config=config)
220223

221-
tag_other_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED")
222-
tag_main_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED", branch="main")
223-
```
224+
tag_other_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED")
225+
tag_main_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED", branch="main")
226+
```
224227

225-
</TabItem>
226-
<TabItem value="Environment">
228+
</TabItem>
229+
<TabItem value="Environment">
227230

228-
```bash
229-
export INFRAHUB_ADDRESS="http://localhost:8000"
230-
export INFRAHUB_API_TOKEN="token"
231-
export INFRAHUB_DEFAULT_BRANCH="other-branch"
232-
```
231+
```bash
232+
export INFRAHUB_ADDRESS="http://localhost:8000"
233+
export INFRAHUB_API_TOKEN="token"
234+
export INFRAHUB_DEFAULT_BRANCH="other-branch"
235+
```
233236

234-
```python
235-
from infrahub_sdk import InfrahubClient
236-
client_other_branch = InfrahubClient()
237+
```python
238+
from infrahub_sdk import InfrahubClient
239+
client_other_branch = InfrahubClient()
237240

238-
tag_other_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED")
239-
tag_main_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED", branch="main")
240-
```
241+
tag_other_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED")
242+
tag_main_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED", branch="main")
243+
```
241244

242-
</TabItem>
245+
</TabItem>
243246
</Tabs>
244247

245248
:::success Branch Configuration
@@ -263,50 +266,51 @@ Avoid naming your script `test.py` as this name could conflict with Python's tes
263266
2. Add the following code to `hello_world.py`:
264267

265268
<Tabs groupId="async-sync">
266-
<TabItem value="Async" default>
267-
```python
268-
import asyncio
269+
<TabItem value="Async" default>
269270

270-
from infrahub_sdk import Config, InfrahubClient
271+
```python
272+
import asyncio
271273

274+
from infrahub_sdk import Config, InfrahubClient
272275

273-
async def hello_world():
274-
client = InfrahubClient(config=Config(address="http://localhost:8000"))
275276

276-
# Try to query accounts to validate connection
277-
try:
278-
accounts = await client.all(kind="CoreAccount")
279-
print(f"Successfully connected to Infrahub! Found {len(accounts)} account(s)")
280-
except Exception as e:
281-
print(f"Something went wrong: {e}")
277+
async def hello_world():
278+
client = InfrahubClient(config=Config(address="http://localhost:8000"))
282279

280+
# Try to query accounts to validate connection
281+
try:
282+
accounts = await client.all(kind="CoreAccount")
283+
print(f"Successfully connected to Infrahub! Found {len(accounts)} account(s)")
284+
except Exception as e:
285+
print(f"Something went wrong: {e}")
283286

284-
asyncio.run(hello_world())
285-
```
286287

287-
</TabItem>
288-
<TabItem value="Sync">
288+
asyncio.run(hello_world())
289+
```
289290

290-
```python
291-
from infrahub_sdk import Config, InfrahubClientSync
291+
</TabItem>
292+
<TabItem value="Sync">
292293

294+
```python
295+
from infrahub_sdk import Config, InfrahubClientSync
293296

294-
# Test connection and authentication
295-
def hello_world():
296-
client = InfrahubClientSync(config=Config(address="http://localhost:8000"))
297297

298-
# Try to query accounts to validate connection
299-
try:
300-
accounts = client.all(kind="CoreAccount")
301-
print(f"Successfully connected to Infrahub! Found {len(accounts)} account(s)")
302-
except Exception as e:
303-
print(f"Something went wrong: {e}")
298+
# Test connection and authentication
299+
def hello_world():
300+
client = InfrahubClientSync(config=Config(address="http://localhost:8000"))
304301

302+
# Try to query accounts to validate connection
303+
try:
304+
accounts = client.all(kind="CoreAccount")
305+
print(f"Successfully connected to Infrahub! Found {len(accounts)} account(s)")
306+
except Exception as e:
307+
print(f"Something went wrong: {e}")
305308

306-
hello_world()
307-
```
308309

309-
</TabItem>
310+
hello_world()
311+
```
312+
313+
</TabItem>
310314
</Tabs>
311315

312316
3. Export the necessary environment variables:

0 commit comments

Comments
 (0)