dev.clore.ai
  • Getting Started
  • Introduction to Clore API
    • API Key Management
    • Transaction Processing with Clore Coin
    • RESTful Protocols in Clore
  • Marketplace Automation and Resource Optimization
    • Automated Spot Price Adjustment for Cost Optimization
    • Automated Server Retrieval and Analysis
    • Spot Price Optimization
    • Predictive Market Analytics
  • Server Management and Configuration
    • Bulk Server Onboarding and Dynamic Pricing Configuration
  • Dynamic Pricing and Profit Maximization
    • Dynamic Pricing Adjustments Based on Server Profitability
    • Automated Price Adjustment Based on Market Demand
    • Automated Spot Rental Based on Price Thresholds
  • Rental Strategies and Arbitrage
  • Monitoring and Notifications
    • Automated Monitoring and Notification of Rental Status
    • Automated Alert System for Low Server Utilization
    • Automated Retrieval and Analysis of Available Servers on the Marketplace
  • Security and Compliance
  • UI Automation and Visualization
  • API Performance Optimization
  • Community Extensions and Integrations
  • Advanced Data Processing and Analysis
  • Scalability and Infrastructure Management
  • Machine Learning and AI Integrations
    • Integrating ML Models into Server Operations
  • Developer Tools and SDKs
    • Setting Up the Clore Developer Toolkit
    • Using Clore SDK for Rapid Prototyping
  • Billing, Accounting, and Financial Reporting
  • Workflow Automation and Scripting
  • Multi-Cloud and Hybrid Cloud Integrations
  • Security Monitoring and Incident Management
  • Blockchain Interactions and Smart Contracts
  • Resource Optimization and Cost-Saving Techniques
Powered by GitBook
On this page
  1. Introduction to Clore API

RESTful Protocols in Clore

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

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

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

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

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

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

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

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

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

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

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

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}")

12. Pagination for Large Data Sets

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

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())
PreviousTransaction Processing with Clore CoinNextMarketplace Automation and Resource Optimization

Last updated 6 months ago