?? Otura: Networking

HTTP clients, API integrations, and network operations.

Use Case 1: REST API Client

// api_client.ifa - Reusable HTTP client

ayanmo API_BASE = "https://api.example.com/v1";
ayanmo API_KEY = Ogbe.env("API_KEY");

ise api_request(method, endpoint, body) {
    ayanmo headers = {
        "Authorization": "Bearer " + API_KEY,
        "Content-Type": "application/json"
    };
    
    ayanmo url = API_BASE + endpoint;
    ayanmo response = nil;
    
    ti (method == "GET") {
        ayanmo response = Otura.get(url, headers);
    } bib?k? ti (method == "POST") {
        ayanmo response = Otura.post(url, body, headers);
    } bib?k? ti (method == "PUT") {
        ayanmo response = Otura.put(url, body, headers);
    } bib?k? ti (method == "DELETE") {
        ayanmo response = Otura.delete(url, headers);
    }
    
    ti (response["status"] >= 400) {
        padap? Okanran.error("API Error: " + response["status"]);
    }
    
    padap? Ogbe.parse_json(response["body"]);
}

// Convenience wrappers
ise get_users() {
    padap? api_request("GET", "/users", nil);
}

ise get_user(id) {
    padap? api_request("GET", "/users/" + id, nil);
}

ise create_user(data) {
    padap? api_request("POST", "/users", data);
}

ise update_user(id, data) {
    padap? api_request("PUT", "/users/" + id, data);
}

ise delete_user(id) {
    padap? api_request("DELETE", "/users/" + id, nil);
}

// Usage
ayanmo users = get_users();
Irosu.fo("Found " + Ogunda.len(users) + " users");

ayanmo new_user = create_user({
    "name": "John Doe",
    "email": "john@example.com"
});
Irosu.fo("Created user: " + new_user["id"]);

Use Case 2: Weather Service

// weather.ifa - Weather API integration

ayanmo WEATHER_API = "https://api.openweathermap.org/data/2.5";
ayanmo WEATHER_KEY = Ogbe.env("OPENWEATHER_API_KEY");

ise get_weather(city) {
    ayanmo url = WEATHER_API + "/weather?q=" + city + "&appid=" + WEATHER_KEY + "&units=metric";
    
    ayanmo result = Okanran.try(|| {
        padap? Otura.get(url);
    });
    
    ti (Okanran.is_error(result)) {
        padap? Okanran.error("Network error");
    }
    
    ti (result["status"] == 404) {
        padap? Okanran.error("City not found: " + city);
    }
    
    ayanmo data = Ogbe.parse_json(result["body"]);
    
    padap? {
        "city": data["name"],
        "country": data["sys"]["country"],
        "temp": data["main"]["temp"],
        "feels_like": data["main"]["feels_like"],
        "humidity": data["main"]["humidity"],
        "description": data["weather"][0]["description"],
        "wind_speed": data["wind"]["speed"]
    };
}

ise display_weather(weather) {
    ti (Okanran.is_error(weather)) {
        Irosu.kigbe("Error: " + Okanran.message(weather));
        padap?;
    }
    
    Irosu.fo("\n??? Weather for " + weather["city"] + ", " + weather["country"]);
    Irosu.fo("-------------------------");
    Irosu.fo("Temperature: " + weather["temp"] + "°C");
    Irosu.fo("Feels like: " + weather["feels_like"] + "°C");
    Irosu.fo("Humidity: " + weather["humidity"] + "%");
    Irosu.fo("Conditions: " + weather["description"]);
    Irosu.fo("Wind: " + weather["wind_speed"] + " m/s");
}

// Usage
ayanmo cities = ["Lagos", "London", "Tokyo", "New York"];

fun city ninu cities {
    ayanmo weather = get_weather(city);
    display_weather(weather);
}

Use Case 3: Webhook Handler

// webhook.ifa - Send webhook notifications

ise send_webhook(url, event, data) {
    ayanmo payload = {
        "event": event,
        "timestamp": Iwori.now(),
        "data": data
    };
    
    ayanmo signature = Irete.sha256(Ogbe.format_json(payload) + WEBHOOK_SECRET);
    
    ayanmo headers = {
        "Content-Type": "application/json",
        "X-Webhook-Signature": signature
    };
    
    ayanmo response = Otura.post(url, payload, headers);
    
    padap? response["status"] == 200;
}

ise notify_order_created(order) {
    ayanmo webhooks = get_registered_webhooks("order.created");
    
    fun webhook ninu webhooks {
        ayanmo success = send_webhook(webhook["url"], "order.created", order);
        
        ti (success) {
            Irosu.fo("? Notified: " + webhook["name"]);
        } bib?k? {
            Irosu.kigbe("? Failed: " + webhook["name"]);
        }
    }
}

// Usage
ayanmo WEBHOOK_SECRET = "my_secret_key";

ayanmo order = {
    "id": "ORD-12345",
    "items": [{"sku": "WIDGET-A", "qty": 2}],
    "total": 99.99
};

notify_order_created(order);

Use Case 4: Health Check Monitor

// health_check.ifa - Monitor service availability

ayanmo services = [
    {"name": "API Server", "url": "https://api.example.com/health"},
    {"name": "Database", "url": "https://db.example.com/ping"},
    {"name": "Cache", "url": "https://cache.example.com/status"},
    {"name": "CDN", "url": "https://cdn.example.com/health"}
];

ise check_service(service) {
    ayanmo start = Iwori.now_ms();
    
    ayanmo result = Okanran.try(|| {
        padap? Otura.get(service["url"]);
    });
    
    ayanmo latency = Iwori.now_ms() - start;
    
    ti (Okanran.is_error(result)) {
        padap? {
            "name": service["name"],
            "status": "DOWN",
            "error": Okanran.message(result),
            "latency": latency
        };
    }
    
    ti (result["status"] != 200) {
        padap? {
            "name": service["name"],
            "status": "UNHEALTHY",
            "code": result["status"],
            "latency": latency
        };
    }
    
    padap? {
        "name": service["name"],
        "status": "UP",
        "latency": latency
    };
}

ise run_health_checks() {
    Irosu.fo("\n?? Service Health Check");
    Irosu.fo("-----------------------");
    
    ayanmo results = Osa.map(services, check_service);
    ayanmo all_healthy = otito;
    
    fun r ninu results {
        ayanmo icon = "";
        ti (r["status"] == "UP") {
            ayanmo icon = "?";
        } bib?k? ti (r["status"] == "UNHEALTHY") {
            ayanmo icon = "??";
            ayanmo all_healthy = iro;
        } bib?k? {
            ayanmo icon = "?";
            ayanmo all_healthy = iro;
        }
        
        Irosu.fo(icon + " " + r["name"] + " [" + r["latency"] + "ms]");
    }
    
    ti (all_healthy) {
        Irosu.fo("\n? All services healthy");
    } bib?k? {
        Irosu.fo("\n?? Some services have issues");
    }
    
    padap? results;
}

// Run every 30 seconds
nigba (otito) {
    run_health_checks();
    Oyeku.sleep(30000);
}