Skip to content

Commit b4cb04b

Browse files
committed
Merge branch 'master' of github.com:elabftw/elabapi-python
* 'master' of github.com:elabftw/elabapi-python: doc: update setting up proxy (#39)
2 parents cfaf50a + e9c00fa commit b4cb04b

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,35 @@ exp_client = elabapi_python.ExperimentsApi(api_client)
5454
items_client = elabapi_python.ItemsApi(api_client)
5555
~~~
5656

57+
## Using a proxy
58+
59+
To route API traffic through a proxy, set the standard environment variables before running the script. These variables are used by both the Python HTTP stack and the eLabFTW client.
60+
61+
### Environment configuration
62+
63+
Set the following variables according to your proxy setup:
64+
65+
- `HTTP_PROXY` and `HTTPS_PROXY`: Define the proxy server address, including the protocol and port (for example, `http://127.0.0.1:8080`).
66+
- `NO_PROXY`: Specify hostnames or IPs that should bypass the proxy (for example, localhost or internal domains).
67+
- `REQUESTS_CA_BUNDLE`: Optional path to a custom CA certificate file, required if the proxy intercepts HTTPS traffic with a self-signed certificate.
68+
69+
Example:
70+
~~~bash
71+
export HTTP_PROXY="http://127.0.0.1:8080"
72+
export HTTPS_PROXY="http://127.0.0.1:8080"
73+
export NO_PROXY="localhost,127.0.0.1"
74+
export REQUESTS_CA_BUNDLE="/path/to/proxy-ca.pem"
75+
~~~
76+
77+
### Client configuration
78+
79+
The client automatically detects these environment variables. To set the proxy manually, use the configuration object:
80+
81+
~~~python
82+
configuration.proxy = os.getenv("HTTPS_PROXY") or os.getenv("HTTP_PROXY")
83+
configuration.ssl_ca_cert = os.getenv("CA_PATH") or os.getenv("REQUESTS_CA_BUNDLE")
84+
~~~
85+
5786
# Unofficial documentation
5887

5988
From TU Graz, Shared RDM Project:
@@ -68,13 +97,13 @@ From TU Graz, Shared RDM Project:
6897

6998
The primary tool for generating the library is swagger-codegen. However, you can also use OpenAPI Generator as an alternative, if it better suits your requirements or you encounter issues with the default.
7099

71-
```bash
100+
~~~bash
72101
# Option 1: Generate using Swagger Codegen
73102
./helper.sh generate
74103

75104
# Option 2: Generate using OpenAPI Generator
76105
GENERATOR_TOOL=openapi ./helper.sh generate
77-
```
106+
~~~
78107

79108
### Or Generate from a local OpenAPI Specification
80109
Ensure the `openapi.yaml` file is located in the current working directory, then run:
@@ -83,9 +112,9 @@ Ensure the `openapi.yaml` file is located in the current working directory, then
83112
~~~
84113

85114
### Build packages
86-
```bash
115+
~~~bash
87116
./helper.sh build
88-
```
117+
~~~
89118

90119
## Installing the library for dev
91120

examples/client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@
2626
# Set this flag to True to get more verbose output
2727
configuration.debug = False
2828

29+
# PROXY CONFIG
30+
# Typical usage:
31+
# export HTTP_PROXY="http://127.0.0.1:8080"
32+
# export HTTPS_PROXY="http://127.0.0.1:8080"
33+
# export NO_PROXY=localhost,127.0.0.1
34+
# export REQUESTS_CA_BUNDLE=/path/to/mitmproxy-ca.pem
35+
# Many libraries (e.g. 'requests') read HTTP(S)_PROXY from the environment automatically.
36+
# However the generated elabapi_python client uses a generated Configuration + urllib3
37+
# which may not read those env vars. That is why we set the env vars along with 'configuration.proxy'
38+
# and 'configuration.ssl_ca_cert' so the generated client itself uses the proxy and trust bundle.
39+
proxy_url = os.getenv("HTTPS_PROXY") or os.getenv("HTTP_PROXY")
40+
if proxy_url:
41+
configuration.proxy = proxy_url
42+
43+
# set CA for both requests and the elabapi-client
44+
ca_path = os.getenv("CA_PATH") or os.getenv("REQUESTS_CA_BUNDLE")
45+
if ca_path:
46+
configuration.ssl_ca_cert = ca_path
47+
if not os.getenv("REQUESTS_CA_BUNDLE"):
48+
os.environ["REQUESTS_CA_BUNDLE"] = ca_path
49+
2950
# Create an API client object with our configuration
3051
api_client = elabapi_python.ApiClient(configuration)
3152

0 commit comments

Comments
 (0)