// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the SubsonicAPI host service.
// It is intended for use in Navidrome plugins built with extism-pdk.

use extism_pdk::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct SubsonicAPICallRequest {
    uri: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SubsonicAPICallResponse {
    #[serde(default)]
    response_json: String,
    #[serde(default)]
    error: Option<String>,
}

#[host_fn]
extern "ExtismHost" {
    fn subsonicapi_call(input: Json<SubsonicAPICallRequest>) -> Json<SubsonicAPICallResponse>;
}

/// Call executes a Subsonic API request and returns the JSON response.
/// 
/// The uri parameter should be the Subsonic API path without the server prefix,
/// e.g., "getAlbumList2?type=random&size=10". The response is returned as raw JSON.
///
/// # Arguments
/// * `uri` - String parameter.
///
/// # Returns
/// The response_json value.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn call(uri: &str) -> Result<String, Error> {
    let response = unsafe {
        subsonicapi_call(Json(SubsonicAPICallRequest {
            uri: uri.to_owned(),
        }))?
    };

    if let Some(err) = response.0.error {
        return Err(Error::msg(err));
    }

    Ok(response.0.response_json)
}
