#Extension module Request
When it comes to integrating with a SOAP Service, a REST API or connecting with another service, the package
requests is one of the most stable and widely used Python modules to perform such integrations.
The full reference documentation of the Python
requests package can be found here.
To use the
requests package, let’s tick the checkbox Requests in the Extension module configuration.
Below it’s reported an example of a function with 3 returns (
3: Generic error) that:
- import the
- get the value of the variable
cityfrom the conversation
- call the Open Weather Map API
- check if the API didn’t respond with a header code
- parse the JSON response and save the new variables
humidity, that can be used in the conversation or other functions
- trigger the return
1: Warmif the temperature is higher than 20 C degrees, otherwise trigger the return
- in case of unexpected error or unsuccessful API call, trigger the
return 3: Generic error.
# Import the requests module import requests city = 'London' # Target city appid = 'abcdefg123456' # Subscription key # Body of the integration try: Make API call req = requests.get("https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + appid) # If status_code is not 200 then log the error and exit if req.status_code != 200: print("API responded with a " + str(req.status_code) + " error") print(req.content) exit(3) # Return 3: Generic error # Get the JSON result result = req.json() save("weather", result["weather"]["main"]) # Save weather description save("temperature", result["main"]["temp"]) # Save temperature save("pressure", result["main"]["pressure"]) # Save pressure save("humidity", result["main"]["humidity"]) # Save humidity if result["main"]["temp"] > 22: exit(1) # Return 1: Warm else: exit(2) # Return 2: Cold except Exception as e: print("An unexpected exception was raised:\n" + str(e)) exit(3) # Return 3: Generic error
⚠️Important note: If the Requests box in the Extension modules configuration table is not ticked, the system will show the following message in the Log Error console:
[x] Security error in importing a Python module: package "requests" is forbidden. Will be ignored