skir-client-async

    SkirRPC integration for MoonBit

    moonbit
    async
    rpc-framework
    rpc
    skir
    skirrpc
    Download zip
    Author
    Version
    0.1.2
    License
    MIT
    Last updated
    yesterday
    Downloads
    64

    #skir-client-async

    Async MoonBit runtime for SkirRPC Service and ServiceClient.

    Website: https://skir.build/docs/skirrpc

    #Install

    moon add gepheum/skir-client-async

    #Development

    ./pre_commit.sh

    #License

    MIT

    HttpErrorCode

    pub(all) enum HttpErrorCode {
    Code_400_BadRequest
    Code_401_Unauthorized
    Code_402_PaymentRequired
    Code_403_Forbidden
    Code_404_NotFound
    Code_405_MethodNotAllowed
    Code_406_NotAcceptable
    Code_407_ProxyAuthenticationRequired
    Code_408_RequestTimeout
    Code_409_Conflict
    Code_410_Gone
    Code_411_LengthRequired
    Code_412_PreconditionFailed
    Code_413_ContentTooLarge
    Code_414_UriTooLong
    Code_415_UnsupportedMediaType
    Code_416_RangeNotSatisfiable
    Code_417_ExpectationFailed
    Code_418_ImATeapot
    Code_421_MisdirectedRequest
    Code_422_UnprocessableContent
    Code_423_Locked
    Code_424_FailedDependency
    Code_425_TooEarly
    Code_426_UpgradeRequired
    Code_428_PreconditionRequired
    Code_429_TooManyRequests
    Code_431_RequestHeaderFieldsTooLarge
    Code_451_UnavailableForLegalReasons
    Code_500_InternalServerError
    Code_501_NotImplemented
    Code_502_BadGateway
    Code_503_ServiceUnavailable
    Code_504_GatewayTimeout
    Code_505_HttpVersionNotSupported
    Code_506_VariantAlsoNegotiates
    Code_507_InsufficientStorage
    Code_508_LoopDetected
    Code_510_NotExtended
    Code_511_NetworkAuthenticationRequired
    } derive(Eq, Hash,
    Debug
    )

    HTTP status codes used by service-side errors.

    The constructors mirror common 4xx/5xx responses and can be converted to integers via HttpErrorCode::as_int.

    HttpErrorCode::as_int

    fn HttpErrorCode::as_int(self : HttpErrorCode) -> Int

    Converts a status enum value to its integer HTTP code.

    MethodEntry

    pub struct MethodEntry[Meta] {
    // private fields
    }

    Precompiled method entry consumed by Service::new.

    Entries are created through MethodEntry::new.

    MethodEntry::new

    fn[Req, Resp, Meta] MethodEntry::new(rpc_method :
    Method
    [Req, Resp], impl_fn : async (Req, Meta) -> Result[Resp, ServiceError]) -> MethodEntry[Meta]

    Creates a method entry from generated method metadata and an implementation.

    Example from e2e: let get_user = @client_async.MethodEntry::new(@skirout_service_skir.get_user_method, (req, _meta) => { ... })

    MethodErrorInfo

    pub(all) struct MethodErrorInfo[Meta] {
    error_message : String
    method_name : String
    status_code : HttpErrorCode?
    request_meta : Meta
    }

    Context passed to error hooks configured in Service::new.

    RawResponse

    pub(all) struct RawResponse {
    data : String
    status_code : Int
    status_text : String
    content_type : String
    }

    Raw HTTP-like response returned by the service runtime.

    This is primarily produced by Service::handle_request.

    RpcError

    pub(all) struct RpcError {
    status_code : Int
    message : String
    }

    Error returned by the RPC client.

    status_code=0 represents transport/decoding failures.

    Service

    pub struct Service[Meta] {
    // private fields
    }

    Immutable RPC service runtime.

    Create with Service::new, then call handle_request for each incoming request payload.

    Service::handle_request

    async fn[Meta] Service::handle_request(self : Service[Meta], body : String, meta : Meta) -> RawResponse

    Handles a single incoming request body and returns a raw response.

    The body parameter must match the HTTP method exactly:
    • for POST requests, pass the raw HTTP request body text;
    • for GET requests, pass the percent-decoded query fragment.

    For GET requests, obtain this value with get_percent_decoded_query_from_url(request.path).

    Example from e2e (start_service.mbt): let body_text = match request.meth { Get => @client_async.get_percent_decoded_query_from_url(request.path) _ => body.read_all().text() } let response = service.handle_request(body_text, ()) conn.send_response(response.status_code, response.status_text, extra_headers={ "Content-Type": response.content_type, }) conn.write_string(response.data)

    Service::new

    fn[Meta] Service::new(methods : Array[MethodEntry[Meta]], unrecognized_values? :
    UnrecognizedValues
    , can_send_unknown_error_message? : (MethodErrorInfo[Meta]) -> Bool, error_logger? : (MethodErrorInfo[Meta]) -> Unit, studio_app_js_url? : String) -> Result[Service[Meta], String]

    Builds a Service from method entries and runtime hooks.

    Parameters:
    • methods: registered RPC methods (required).
    • unrecognized_values: controls whether unknown fields are preserved or dropped during request decoding (default: Drop).
    • can_send_unknown_error_message: decides whether unknown/internal error details can be sent back to clients (default: _ => false).
    • error_logger: receives method error context for logging/observability (default: prints method_name + error_message).
    • studio_app_js_url: JS bundle URL used by the built-in "studio" endpoint (default: Skir CDN standalone URL).

    Example from e2e: @client_async.Service::new( [get_user, add_user], unrecognized_values=@client.UnrecognizedValues::Keep, )

    ServiceClient

    pub struct ServiceClient {
    // private fields
    }

    Client for invoking methods on a remote Skir service endpoint.

    Construct with ServiceClient::new and call invoke_remote.

    ServiceClient::invoke_remote

    async fn[Req, Resp] ServiceClient::invoke_remote(self : ServiceClient, rpc_method :
    Method
    [Req, Resp], request : Req, extra_headers : Array[(String, String)]) -> Result[Resp, RpcError]

    Invokes a remote method and decodes the typed response.

    extra_headers are merged on top of default_headers for this call.

    Example from e2e: client.invoke_remote(@skirout_service_skir.get_user_method, get_req, [])

    ServiceClient::new

    fn ServiceClient::new(service_url : String, default_headers? :
    HashMap
    [String, String]) -> ServiceClient

    Creates a service client.

    • default_headers is copied so later caller mutations do not affect the client.

    Example from e2e: let client = @client_async.ServiceClient::new("http://localhost:8787/myapi")

    ServiceError

    pub(all) struct ServiceError {
    message : String
    status_code : HttpErrorCode?
    }

    Error returned by method implementations.

    • message is logged and may be returned to clients depending on policy.
    • status_code when present drives the HTTP response code.

    ServiceError::new

    fn ServiceError::new(message : String, status_code? : HttpErrorCode?) -> ServiceError

    Builds a ServiceError.

    Example: ServiceError::new("user_id must be non-zero", status_code=Some(HttpErrorCode::Code_400_BadRequest))

    get_percent_decoded_query_from_url

    fn get_percent_decoded_query_from_url(path : String) -> String

    Extracts the query section from a URL path and percent-decodes it.

    Returns "" if the URL has no ?query section.

    Example from e2e: Get => @client_async.get_percent_decoded_query_from_url(request.path)