13

Networking

HTTP requests with Otura domain

?? Not Available in Playground

Network operations require internet access and won't work in the browser playground. Run these locally.

Otura Domain

Otura handles HTTP requests and network operations.

HTTP Methods

Method Description
get(url) HTTP GET request
post(url, body) HTTP POST request
put(url, body) HTTP PUT request
delete(url) HTTP DELETE request

GET Request

// Simple GET request
ayanmo response = Otura.get("https://api.example.com/data");

Irosu.fo("Status: " + response["status"]);
Irosu.fo("Body: " + response["body"]);

POST Request with JSON

// Create JSON body
ayanmo data = {
    "name": "Ifá",
    "type": "language"
};

ayanmo response = Otura.post(
    "https://api.example.com/items",
    data
);

ti (response["status"] == 201) {
    Irosu.fo("Created successfully!");
} bib?k? {
    Irosu.kigbe("Failed: " + response["body"]);
}

Handling Responses

ayanmo response = Otura.get("https://api.example.com/users");

// Check status code
ti (response["status"] >= 200 && response["status"] < 300) {
    // Parse JSON body
    ayanmo users = Ogbe.parse_json(response["body"]);
    
    fun user ninu users {
        Irosu.fo("User: " + user["name"]);
    }
} bib?k? {
    Irosu.kigbe("Request failed with status " + response["status"]);
}

Custom Headers

// Set custom headers
ayanmo headers = {
    "Authorization": "Bearer my-token",
    "Content-Type": "application/json"
};

ayanmo response = Otura.get(
    "https://api.example.com/protected",
    headers
);

Irosu.fo(response["body"]);

Error Handling

// Network requests can fail
ayanmo result = Okanran.try(|| {
    padap? Otura.get("https://unreachable.example.com");
});

ti (Okanran.is_error(result)) {
    Irosu.kigbe("Network error: " + Okanran.message(result));
} bib?k? {
    Irosu.fo("Response: " + result["body"]);
}

Practical Example: Fetch Weather

ise get_weather(city) {
    ayanmo url = "https://api.weather.com/v1/current?city=" + city;
    
    ayanmo result = Okanran.try(|| {
        padap? Otura.get(url);
    });
    
    ti (Okanran.is_error(result)) {
        padap? Okanran.error("Could not fetch weather");
    }
    
    ti (result["status"] != 200) {
        padap? Okanran.error("API returned " + result["status"]);
    }
    
    padap? Ogbe.parse_json(result["body"]);
}

ayanmo weather = get_weather("Lagos");
ti (!Okanran.is_error(weather)) {
    Irosu.fo("Temperature: " + weather["temp"] + "°C");
    Irosu.fo("Conditions: " + weather["description"]);
}

REST API Client

// Reusable API client
ayanmo BASE_URL = "https://api.example.com";

ise api_get(endpoint) {
    padap? Otura.get(BASE_URL + endpoint);
}

ise api_post(endpoint, data) {
    padap? Otura.post(BASE_URL + endpoint, data);
}

// Usage
ayanmo users = api_get("/users");
ayanmo new_user = api_post("/users", {"name": "Adé"});