Skip to main content

snippets_py

Integration

New integration from credential
import requests
session = yepcode.integration.http("credential_slug")
New integration from plain authentication data
import requests
from integrations.http import BaseUrlSession

session = BaseUrlSession("https://some-domain.com/api/",
auth=("user", "password"), headers={"X-Custom-Header": "foobar"})

GET

GET
try:
response = session.get("/users/12345")

# Raise an exception if an error occurs
response.raise_for_status()

print("Response status: ", response.status_code)
print("User: ", response.json())
print(response.headers)
except requests.exceptions.HTTPError as e:
logging.error("HTTP error: " + str(e))
except requests.exceptions.ConnectionError as e:
logging.error("Connection error: " + str(e))
except requests.exceptions.Timeout as e:
logging.error("Request timeout: " + str(e))
except requests.exceptions.RequestException as e:
logging.error("Error: " + str(e))

POST

POST
try:
response = session.post("/users/12345",
json={"firstName": "John", "lastName": "Doe"})

# Raise an exception if an error occurs
response.raise_for_status()

print("Response status: ", response.status_code)
print("User was updated successfully: ", response.json().user)
print(response.headers)
except requests.exceptions.HTTPError as e:
logging.error("HTTP error: " + str(e))
except requests.exceptions.ConnectionError as e:
logging.error("Connection error: " + str(e))
except requests.exceptions.Timeout as e:
logging.error("Request timeout: " + str(e))
except requests.exceptions.RequestException as e:
logging.error("Error: " + str(e))

PUT

PUT
try:
response = session.put("/users/12345",
json={"firstName": "John", "lastName": "Doe"})

# Raise an exception if an error occurs
response.raise_for_status()

print("Response status: ", response.status_code)
print("User was updated successfully: ", response.json().user)
print(response.headers)
except requests.exceptions.HTTPError as e:
logging.error("HTTP error: " + str(e))
except requests.exceptions.ConnectionError as e:
logging.error("Connection error: " + str(e))
except requests.exceptions.Timeout as e:
logging.error("Request timeout: " + str(e))
except requests.exceptions.RequestException as e:
logging.error("Error: " + str(e))

DELETE

DELETE
try:
response = session.delete("/users/12345")

# Raise an exception if an error occurs
response.raise_for_status()

print("Response status: ", response.status_code)
print("User was deleted successfully: ", response.json().user)
print(response.headers)
except requests.exceptions.HTTPError as e:
logging.error("HTTP error: " + str(e))
except requests.exceptions.ConnectionError as e:
logging.error("Connection error: " + str(e))
except requests.exceptions.Timeout as e:
logging.error("Request timeout: " + str(e))
except requests.exceptions.RequestException as e:
logging.error("Error: " + str(e))