Rust

A JSON API client with sso-key authentication, the base of key-and-secret registrar APIs.

Contents

use utopia_domains::api::Api;
Rust
pub struct Api

An HTTP API client: JSON by default, authenticated with Authorization: sso-key <key>:<secret>, following redirects.

Rust
use std::sync::Arc;
use utopia_domains::api::{Answer, Api, Params};
use utopia_domains::transport::{Request, Response, Transport, TransportError};
use utopia_domains::BoxFuture;

struct Echo;

impl Transport for Echo {
    fn send<'a>(&'a self, request: Request) -> BoxFuture<'a, Result<Response, TransportError>> {
        assert_eq!(request.url, "https://api.example.com/v1/domains?limit=5");
        assert_eq!(request.header("Authorization"), Some("sso-key key:secret"));
        let headers = vec![("Content-Type".to_owned(), "application/json; charset=utf-8".to_owned())];
        Box::pin(async move { Ok(Response { status: 200, headers, body: br#"{"total":1}"#.to_vec() }) })
    }
}

let api = Api::new("https://api.example.com", "key", "secret").with_transport(Arc::new(Echo));
let params = Params::Fields(serde_json::json!({"limit": 5}));
let answer = tokio::runtime::Runtime::new()?.block_on(api.call("GET", "/v1/domains", &params, &[]))?;
assert_eq!(answer, Answer::Json(serde_json::json!({"total": 1})));
Also implements Debug.
use utopia_domains::api::Answer;
Rust
pub enum Answer

What an API answered: decoded JSON, or the body as text.

use utopia_domains::api::Params;
Rust
pub enum Params

What Api::call sends: fields (encoded by the Content-Type) or a body as is (text/xml).