Skip to content

Commit

Permalink
Merge pull request #370 from RWTH-EBC/326-Add-option-to-disable-certi…
Browse files Browse the repository at this point in the history
…ficate-validation

chore: include example for session object
  • Loading branch information
djs0109 authored Jan 14, 2025
2 parents 70042b4 + be0d036 commit bc072cc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### v0.6.x
- add: Tutorial for using session object ([#370](https://github.com/RWTH-EBC/FiLiP/pull/370))

### v0.6.0
- add: Tutorial for connecting with secured endpoints ([#319](https://github.com/RWTH-EBC/FiLiP/pull/319))
- add: Example for notification based command ([#332](https://github.com/RWTH-EBC/FiLiP/pull/332))
Expand Down
61 changes: 61 additions & 0 deletions examples/ngsi_v2/e15_ngsi_v2_use_sessions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
# This example shows how to manipulate settings like retry strategy and certificate
verification on requests by including session object with the client request.
Possible options are for examples:
1. Set retry strategy in the session
2. Set default headers for the session (e.g., for authentication)
3. Disable certificate verification / Load custom SSL certificate
"""

import requests
import urllib3
from requests.adapters import HTTPAdapter
from urllib3 import Retry

from filip.clients.ngsi_v2 import ContextBrokerClient
from filip.config import settings
from filip.models.base import FiwareHeader

urllib3.disable_warnings()
session = requests.Session()
CB_URL = settings.CB_URL
# FIWARE-Service
SERVICE = "filip"
# FIWARE-Servicepath
SERVICE_PATH = "/"

if __name__ == "__main__":

# Create fiware header
fiware_header = FiwareHeader(service=SERVICE, service_path=SERVICE_PATH)

# Option 1. Set retry strategy in the session
retry_strategy = Retry(
total=5, # Maximum number of retries
backoff_factor=1, # Exponential backoff (1, 2, 4, 8, etc.)
status_forcelist=[
429,
500,
502,
503,
504,
], # Retry on these HTTP status codes
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount("https://", adapter)
session.mount("http://", adapter)

# Option 2. set default headers for the session (e.g., for authentication)
session.headers.update({"Authorization": "Bearer your_token"})

# Option 3. Disable certificate verification for the request
session.verify = False

# Create a context broker client including the session object
cb_client = ContextBrokerClient(
url=CB_URL, fiware_header=fiware_header, session=session
)

# Make desired requests using the client
entity_list = cb_client.get_entity_list()

0 comments on commit bc072cc

Please sign in to comment.