|
| 1 | +""" |
| 2 | +# This example shows how to manipulate settings like retry strategy and certificate |
| 3 | +verification on requests by including session object with the client request. |
| 4 | +Possible options are for examples: |
| 5 | +
|
| 6 | +1. Set retry strategy in the session |
| 7 | +2. Set default headers for the session (e.g., for authentication) |
| 8 | +3. Disable certificate verification / Load custom SSL certificate |
| 9 | +""" |
| 10 | + |
| 11 | +import requests |
| 12 | +import urllib3 |
| 13 | +from requests.adapters import HTTPAdapter |
| 14 | +from urllib3 import Retry |
| 15 | + |
| 16 | +from filip.clients.ngsi_v2 import ContextBrokerClient |
| 17 | +from filip.config import settings |
| 18 | +from filip.models.base import FiwareHeader |
| 19 | + |
| 20 | +urllib3.disable_warnings() |
| 21 | +session = requests.Session() |
| 22 | +CB_URL = settings.CB_URL |
| 23 | +# FIWARE-Service |
| 24 | +SERVICE = "filip" |
| 25 | +# FIWARE-Servicepath |
| 26 | +SERVICE_PATH = "/" |
| 27 | + |
| 28 | +if __name__ == "__main__": |
| 29 | + |
| 30 | + # Create fiware header |
| 31 | + fiware_header = FiwareHeader(service=SERVICE, service_path=SERVICE_PATH) |
| 32 | + |
| 33 | + # Option 1. Set retry strategy in the session |
| 34 | + retry_strategy = Retry( |
| 35 | + total=5, # Maximum number of retries |
| 36 | + backoff_factor=1, # Exponential backoff (1, 2, 4, 8, etc.) |
| 37 | + status_forcelist=[ |
| 38 | + 429, |
| 39 | + 500, |
| 40 | + 502, |
| 41 | + 503, |
| 42 | + 504, |
| 43 | + ], # Retry on these HTTP status codes |
| 44 | + ) |
| 45 | + adapter = HTTPAdapter(max_retries=retry_strategy) |
| 46 | + session.mount("https://", adapter) |
| 47 | + session.mount("http://", adapter) |
| 48 | + |
| 49 | + # Option 2. set default headers for the session (e.g., for authentication) |
| 50 | + session.headers.update({"Authorization": "Bearer your_token"}) |
| 51 | + |
| 52 | + # Option 3. Disable certificate verification for the request |
| 53 | + session.verify = False |
| 54 | + |
| 55 | + # Create a context broker client including the session object |
| 56 | + cb_client = ContextBrokerClient( |
| 57 | + url=CB_URL, fiware_header=fiware_header, session=session |
| 58 | + ) |
| 59 | + |
| 60 | + # Make desired requests using the client |
| 61 | + entity_list = cb_client.get_entity_list() |
0 commit comments