Section: API Usage and Automation
RESTful protocols in Clore allow developers to interact with the platform's resources and execute various operations on wallets, servers, and marketplace listings. This guide will cover basic and advanced implementations of RESTful API requests, including CRUD operations, authentication handling, and dynamic request management.
1. Basic REST API Request to Fetch Server Listings
Copy import requests
api_key = "YOUR_API_KEY"
url = "https://api.clore.ai/v1/marketplace"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
servers = response.json().get("servers", [])
print("Available Servers:", servers)
2. Using POST Method for Creating a New Server Rental
Copy url = "https://api.clore.ai/v1/create_order"
data = {
"currency": "clore",
"image": "cloreai/ubuntu20.04-jupyter",
"renting_server": 6,
"type": "on-demand",
"spotprice": 0.000001,
"ports": {"22": "tcp", "8888": "http"}
}
response = requests.post(url, headers=headers, json=data)
print("Order Creation Response:", response.json())
3. PUT Request to Update Server Configuration
Copy update_url = "https://api.clore.ai/v1/set_server_settings"
update_data = {
"name": "Server_A",
"availability": True,
"mrl": 72,
"on_demand": 0.0001,
"spot": 0.0000005
}
response = requests.put(update_url, headers=headers, json=update_data)
print("Update Response:", response.json())
4. DELETE Request to Cancel a Rental Order
Copy cancel_url = "https://api.clore.ai/v1/cancel_order"
cancel_data = {
"id": 39,
"issue": "Order no longer needed"
}
response = requests.delete(cancel_url, headers=headers, json=cancel_data)
print("Cancel Order Response:", response.json())
5. Handling Status Codes and Errors
Copy def make_request(url, method="GET", data=None):
response = requests.request(method, url, headers=headers, json=data)
if response.status_code == 200:
return response.json()
elif response.status_code == 401:
print("Unauthorized: Check API Key")
elif response.status_code == 404:
print("Not Found: Check URL")
else:
print("Error:", response.status_code)
return None
# Example usage
response = make_request(url="https://api.clore.ai/v1/wallets")
print("Wallets Response:", response)
6. Batch Processing of Multiple API Requests
Copy server_ids = [6, 7, 8]
for server_id in server_ids:
data = {"currency": "clore", "renting_server": server_id, "type": "spot"}
response = requests.post(url, headers=headers, json=data)
print(f"Response for server {server_id}:", response.json())
7. Request Throttling and Rate Limiting
Copy import time
def rate_limited_request(url, method="GET", data=None, limit=1):
time.sleep(limit)
return requests.request(method, url, headers=headers, json=data)
# Example usage
for i in range(5):
response = rate_limited_request(url="https://api.clore.ai/v1/marketplace")
print("Marketplace Response:", response.json())
8. Using Query Parameters for Custom Searches
Copy params = {
"currency": "clore",
"min_price": 0.00001,
"max_price": 0.001
}
url_with_params = "https://api.clore.ai/v1/marketplace"
response = requests.get(url_with_params, headers=headers, params=params)
print("Filtered Marketplace Results:", response.json())
9. Retry Logic for Handling Network Errors
Copy import requests
from requests.exceptions import RequestException
def reliable_request(url, retries=3):
for attempt in range(retries):
try:
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
except RequestException as e:
print(f"Attempt {attempt + 1} failed. Retrying...")
return None
# Example usage
response = reliable_request("https://api.clore.ai/v1/wallets")
print("Wallets Data:", response)
10. Advanced Filtering and Sorting Options
Copy params = {
"sort": "price",
"order": "asc",
"availability": True
}
response = requests.get("https://api.clore.ai/v1/marketplace", headers=headers, params=params)
print("Sorted and Filtered Data:", response.json())
11. Error Logging and Alerting
Copy import logging
logging.basicConfig(filename="api_errors.log", level=logging.ERROR)
def log_error(message):
logging.error(message)
# Usage in request
try:
response = requests.get("https://api.clore.ai/v1/wallets", headers=headers)
response.raise_for_status()
except RequestException as e:
log_error(f"Error occurred: {e}")
Copy page = 1
per_page = 10
has_more = True
while has_more:
response = requests.get(
f"https://api.clore.ai/v1/marketplace?page={page}&per_page={per_page}",
headers=headers
)
data = response.json()
print(f"Page {page}:", data)
has_more = len(data["servers"]) == per_page
page += 1
13. Session Management for Multiple Requests
Copy session = requests.Session()
session.headers.update(headers)
# Multiple requests using the same session
response1 = session.get("https://api.clore.ai/v1/marketplace")
response2 = session.get("https://api.clore.ai/v1/wallets")
print("Marketplace:", response1.json())
print("Wallets:", response2.json())
Last updated 6 months ago