Files
Maison/arti-api/API_EXAMPLES.md
2026-02-10 12:12:11 +01:00

16 KiB

API Examples and Testing

This document provides examples for testing the Arti-API endpoints using curl commands.

Health Check Examples

Get API Status

curl -X GET "http://localhost:8000/" \
  -H "accept: application/json"

Health Check

curl -X GET "http://localhost:8000/health" \
  -H "accept: application/json"

Debian Package Management Examples

Upload a Debian Package

# Upload for amd64 architecture (default)
curl -X POST "http://localhost:8000/debian/upload?architecture=amd64" \
  -H "accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -F "file=@my-package_1.0.0_amd64.deb"

# Upload for arm64 architecture
curl -X POST "http://localhost:8000/debian/upload?architecture=arm64" \
  -H "accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -F "file=@my-package_1.0.0_arm64.deb"

List All Debian Packages

curl -X GET "http://localhost:8000/debian/packages" \
  -H "accept: application/json"

Delete a Debian Package

curl -X DELETE "http://localhost:8000/debian/package/my-package_1.0.0_amd64.deb" \
  -H "accept: application/json"

Refresh Debian Repository

curl -X POST "http://localhost:8000/refresh/debian" \
  -H "accept: application/json"

Helm Chart Management Examples

Upload a Helm Chart

curl -X POST "http://localhost:8000/helm/upload" \
  -H "accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -F "file=@my-chart-0.1.0.tgz"

List All Helm Charts

curl -X GET "http://localhost:8000/helm/charts" \
  -H "accept: application/json"

Delete a Helm Chart

curl -X DELETE "http://localhost:8000/helm/chart/my-chart-0.1.0.tgz" \
  -H "accept: application/json"

Refresh Helm Repository

curl -X POST "http://localhost:8000/refresh/helm" \
  -H "accept: application/json"

Docker Registry Examples

List Docker Images

curl -X GET "http://localhost:8000/docker/images" \
  -H "accept: application/json"

User Management Examples

List All Users

curl -X GET "http://localhost:8000/users" \
  -H "accept: application/json"

Get User Information

curl -X GET "http://localhost:8000/users/admin" \
  -H "accept: application/json"

Create/Update User

# Create a new user
curl -X POST "http://localhost:8000/users" \
  -H "accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "developer",
    "password": "secure_password123"
  }'

# Update existing user password
curl -X POST "http://localhost:8000/users" \
  -H "accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "new_secure_password456"
  }'

Delete User

curl -X DELETE "http://localhost:8000/users/olduser" \
  -H "accept: application/json"

Repository Refresh Examples

Refresh All Repositories

curl -X POST "http://localhost:8000/refresh/all" \
  -H "accept: application/json"

Python Examples

Using requests library

import requests

# Health check
response = requests.get("http://localhost:8000/health")
print(response.json())

# Upload a package
with open("my-package_1.0.0_amd64.deb", "rb") as f:
    files = {"file": f}
    params = {"architecture": "amd64"}
    response = requests.post(
        "http://localhost:8000/debian/upload",
        files=files,
        params=params
    )
    print(response.json())

# List packages
response = requests.get("http://localhost:8000/debian/packages")
print(response.json())

# User management
# Create a user
user_data = {"username": "testuser", "password": "testpass123"}
response = requests.post("http://localhost:8000/users", json=user_data)
print(response.json())

# List users
response = requests.get("http://localhost:8000/users")
print(response.json())

# Get user info
response = requests.get("http://localhost:8000/users/testuser")
print(response.json())

PHP Examples

Using cURL in PHP

<?php

// Health check
function checkHealth() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/health");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($httpCode === 200) {
        return json_decode($response, true);
    }
    return false;
}

// Upload Debian package
function uploadDebianPackage($filePath, $architecture = 'amd64') {
    $ch = curl_init();
    
    $postFields = [
        'file' => new CURLFile($filePath, 'application/vnd.debian.binary-package'),
    ];
    
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/debian/upload?architecture=" . urlencode($architecture));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return [
        'success' => $httpCode === 200,
        'data' => json_decode($response, true),
        'http_code' => $httpCode
    ];
}

// List Debian packages
function listDebianPackages() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/debian/packages");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($httpCode === 200) {
        return json_decode($response, true);
    }
    return false;
}

// Upload Helm chart
function uploadHelmChart($filePath) {
    $ch = curl_init();
    
    $postFields = [
        'file' => new CURLFile($filePath, 'application/gzip'),
    ];
    
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/helm/upload");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return [
        'success' => $httpCode === 200,
        'data' => json_decode($response, true),
        'http_code' => $httpCode
    ];
}

// List Helm charts
function listHelmCharts() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/helm/charts");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($httpCode === 200) {
        return json_decode($response, true);
    }
    return false;
}

// User management
function createUser($username, $password) {
    $ch = curl_init();
    
    $userData = [
        'username' => $username,
        'password' => $password
    ];
    
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/users");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Accept: application/json'
    ]);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return [
        'success' => $httpCode === 200,
        'data' => json_decode($response, true),
        'http_code' => $httpCode
    ];
}

function listUsers() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/users");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($httpCode === 200) {
        return json_decode($response, true);
    }
    return false;
}

function getUserInfo($username) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/users/" . urlencode($username));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return [
        'success' => $httpCode === 200,
        'data' => json_decode($response, true),
        'http_code' => $httpCode
    ];
}

function deleteUser($username) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/users/" . urlencode($username));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return [
        'success' => $httpCode === 200,
        'data' => json_decode($response, true),
        'http_code' => $httpCode
    ];
}

function refreshAllRepositories() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/refresh/all");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return [
        'success' => $httpCode === 200,
        'data' => json_decode($response, true),
        'http_code' => $httpCode
    ];
}

// Example usage
try {
    // Check API health
    $health = checkHealth();
    if ($health) {
        echo "API Status: " . $health['status'] . "\n";
    }
    
    // List packages
    $packages = listDebianPackages();
    if ($packages) {
        echo "Found " . count($packages['packages']) . " Debian packages\n";
    }
    
    // Create a user
    $result = createUser('php_user', 'secure_password123');
    if ($result['success']) {
        echo "User created: " . $result['data']['message'] . "\n";
    } else {
        echo "Failed to create user: HTTP " . $result['http_code'] . "\n";
    }
    
    // List users
    $users = listUsers();
    if ($users) {
        echo "Registry users: " . implode(', ', $users['users']) . "\n";
    }
    
    // Refresh repositories
    $refresh = refreshAllRepositories();
    if ($refresh['success']) {
        echo "Repositories refreshed: " . $refresh['data']['message'] . "\n";
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

?>
<?php
require_once 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class ArtiApiClient {
    private $client;
    private $baseUrl;
    
    public function __construct($baseUrl = 'http://localhost:8000') {
        $this->baseUrl = $baseUrl;
        $this->client = new Client([
            'base_uri' => $baseUrl,
            'timeout' => 30,
            'headers' => [
                'Accept' => 'application/json'
            ]
        ]);
    }
    
    public function checkHealth() {
        try {
            $response = $this->client->get('/health');
            return json_decode($response->getBody(), true);
        } catch (RequestException $e) {
            return false;
        }
    }
    
    public function uploadDebianPackage($filePath, $architecture = 'amd64') {
        try {
            $response = $this->client->post('/debian/upload', [
                'multipart' => [
                    [
                        'name' => 'file',
                        'contents' => fopen($filePath, 'r'),
                        'filename' => basename($filePath)
                    ]
                ],
                'query' => ['architecture' => $architecture]
            ]);
            
            return [
                'success' => true,
                'data' => json_decode($response->getBody(), true)
            ];
        } catch (RequestException $e) {
            return [
                'success' => false,
                'error' => $e->getMessage(),
                'http_code' => $e->getCode()
            ];
        }
    }
    
    public function listDebianPackages() {
        try {
            $response = $this->client->get('/debian/packages');
            return json_decode($response->getBody(), true);
        } catch (RequestException $e) {
            return false;
        }
    }
    
    public function createUser($username, $password) {
        try {
            $response = $this->client->post('/users', [
                'json' => [
                    'username' => $username,
                    'password' => $password
                ]
            ]);
            
            return [
                'success' => true,
                'data' => json_decode($response->getBody(), true)
            ];
        } catch (RequestException $e) {
            return [
                'success' => false,
                'error' => $e->getMessage(),
                'http_code' => $e->getCode()
            ];
        }
    }
    
    public function listUsers() {
        try {
            $response = $this->client->get('/users');
            return json_decode($response->getBody(), true);
        } catch (RequestException $e) {
            return false;
        }
    }
    
    public function deleteUser($username) {
        try {
            $response = $this->client->delete("/users/{$username}");
            return [
                'success' => true,
                'data' => json_decode($response->getBody(), true)
            ];
        } catch (RequestException $e) {
            return [
                'success' => false,
                'error' => $e->getMessage(),
                'http_code' => $e->getCode()
            ];
        }
    }
}

// Example usage with Guzzle
$api = new ArtiApiClient();

// Check health
$health = $api->checkHealth();
if ($health) {
    echo "API is healthy: " . $health['status'] . "\n";
}

// List packages
$packages = $api->listDebianPackages();
if ($packages) {
    foreach ($packages['packages'] as $package) {
        echo "Package: {$package['name']} ({$package['size']} bytes)\n";
    }
}

// Create user
$userResult = $api->createUser('guzzle_user', 'test123');
if ($userResult['success']) {
    echo "User created successfully\n";
} else {
    echo "Failed to create user: " . $userResult['error'] . "\n";
}

Response Examples

Successful Package Upload Response

{
  "message": "Package my-app_1.0.0_amd64.deb uploaded successfully",
  "path": "/data/debian/pool/my-app_1.0.0_amd64.deb"
}

Package List Response

{
  "packages": [
    {
      "name": "my-app_1.0.0_amd64.deb",
      "size": 1024000,
      "modified": "2023-12-01T10:30:00.123456"
    },
    {
      "name": "another-app_2.0.0_arm64.deb", 
      "size": 2048000,
      "modified": "2023-12-01T11:45:00.789012"
    }
  ]
}

Error Response Example

{
  "detail": "File must be a .deb package"
}

User Management Response Examples

User List Response

{
  "users": ["admin", "developer", "readonly", "testuser"]
}

User Creation Response

{
  "message": "User developer created successfully"
}

User Update Response

{
  "message": "User admin updated successfully"
}

User Info Response

{
  "username": "developer",
  "created": "2023-12-01T10:30:00.123456"
}

User Deletion Response

{
  "message": "User olduser deleted successfully"
}