Vai al contenuto

SDK per le API

Tre client pronti per le API di iterscan: gestiscono il token, chiamano ogni indirizzo e segnalano gli errori. Copia il file nel tuo progetto e usa il tuo token.

C# (.NET)

File: IterScanClient.cs

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

public class IterScanClient
{
    private readonly HttpClient _http;

    public IterScanClient(string apiKey)
    {
        _http = new HttpClient();
        _http.DefaultRequestHeaders.Add("X-API-Key", apiKey);
        _http.BaseAddress = new Uri("https://www.iterscan.it/api/");
    }

    private async Task<T> GetAsync<T>(string endpoint)
    {
        var res = await _http.GetAsync(endpoint);
        var json = await res.Content.ReadAsStringAsync();
        if (!res.IsSuccessStatusCode) throw new Exception(json);
        return JsonConvert.DeserializeObject<T>(json);
    }

    private async Task<T> PostAsync<T>(string endpoint, HttpContent content)
    {
        var res = await _http.PostAsync(endpoint, content);
        var json = await res.Content.ReadAsStringAsync();
        if (!res.IsSuccessStatusCode) throw new Exception(json);
        return JsonConvert.DeserializeObject<T>(json);
    }

    public Task<dynamic> GetSites() => GetAsync<dynamic>("sites");
    public Task<dynamic> GetSiteStatus(int id) => GetAsync<dynamic>($"sitestatus?id={id}");
    public Task<dynamic> GetUptime(int id) => GetAsync<dynamic>($"uptime?id={id}");

    public Task<dynamic> AddSite(string url)
    {
        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("url", url)
        });
        return PostAsync<dynamic>("addsite", content);
    }
}

Esempio d'uso

var client = new IterScanClient("IL_TUO_TOKEN");

var sites  = await client.GetSites();
var status = await client.GetSiteStatus(12);
var uptime = await client.GetUptime(12);

await client.AddSite("https://nuovosito.it");

PHP

File: IterScanClient.php

<?php

class IterScanClient {

    private $apiKey;
    private $baseUrl = "https://www.iterscan.it/api/";

    public function __construct($apiKey) {
        $this->apiKey = $apiKey;
    }

    private function request($endpoint, $method = "GET", $data = null) {
        $ch = curl_init();
        $opts = [
            CURLOPT_URL => $this->baseUrl . $endpoint,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => ["X-API-Key: " . $this->apiKey]
        ];
        if ($method === "POST") {
            $opts[CURLOPT_POST] = true;
            $opts[CURLOPT_POSTFIELDS] = $data;
        }
        curl_setopt_array($ch, $opts);
        $response = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($status >= 400) {
            throw new Exception($response);
        }
        return json_decode($response, true);
    }

    public function getSites()          { return $this->request("sites"); }
    public function getSiteStatus($id)  { return $this->request("sitestatus?id=$id"); }
    public function getUptime($id)      { return $this->request("uptime?id=$id"); }
    public function addSite($url)       { return $this->request("addsite", "POST", ["url" => $url]); }
}

Esempio d'uso

$client = new IterScanClient("IL_TUO_TOKEN");

$sites  = $client->getSites();
$status = $client->getSiteStatus(12);
$uptime = $client->getUptime(12);

$client->addSite("https://nuovosito.it");

JavaScript (Node.js e browser)

File: IterScan.js

class IterScanClient {

    constructor(apiKey) {
        this.apiKey = apiKey;
        this.baseUrl = "https://www.iterscan.it/api/";
    }

    async request(endpoint, method = "GET", body = null) {
        const options = { method, headers: { "X-API-Key": this.apiKey } };
        if (body) options.body = body;
        const res = await fetch(this.baseUrl + endpoint, options);
        const json = await res.json();
        if (!res.ok) throw new Error(JSON.stringify(json));
        return json;
    }

    getSites()          { return this.request("sites"); }
    getSiteStatus(id)   { return this.request(`sitestatus?id=${id}`); }
    getUptime(id)       { return this.request(`uptime?id=${id}`); }

    addSite(url) {
        const form = new FormData();
        form.append("url", url);
        return this.request("addsite", "POST", form);
    }
}

Esempio d'uso (Node.js)

const client = new IterScanClient("IL_TUO_TOKEN");

const sites  = await client.getSites();
const status = await client.getSiteStatus(12);
const uptime = await client.getUptime(12);

await client.addSite("https://nuovosito.it");

Esempio d'uso (browser)

<script src="IterScan.js"></script>
<script>
const api = new IterScanClient("IL_TUO_TOKEN");
api.getSites().then(console.log);
</script>