1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use serde::{Deserialize, Serialize};

/// Holds the necessary information to make api calls.
#[derive(Debug)]
pub struct DeveloperGatewayApi<'a> {
    /// Resource locator of the api.
    pub url: &'a str,

    /// Http method expected by the api call.
    pub method: RequestMethod,
}

#[derive(Debug, Clone, Copy)]
pub enum RequestMethod {
    GET,
    POST,
}

/// This is the main api call which allows the execution of a secure service function.
pub const SERVICE_EXECUTE_API: DeveloperGatewayApi = DeveloperGatewayApi {
    url: "v0/api/service/execute",
    method: RequestMethod::POST,
};

/// This api call allows deploying a new service.
pub const SERVICE_DEPLOY_API: DeveloperGatewayApi = DeveloperGatewayApi {
    url: "v0/api/service/deploy",
    method: RequestMethod::POST,
};

/// This api allows users to poll for the status of submitted asynchronous requests.
pub const SERVICE_POLL_API: DeveloperGatewayApi = DeveloperGatewayApi {
    url: "v0/api/service/poll",
    method: RequestMethod::POST,
};

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged, rename_all = "camelCase")]
pub enum GatewayRequest {
    ///  Used to trigger a service deployment with provided initcode.
    Deploy {
        /// Hex-encoded initcode.
        data: String,
    },

    ///  Used to trigger a service execution with user provided arguments.
    Execute {
        /// Hex-encoded address where the service can be found.
        address: String,

        /// Hex-encoded data that the user wants to pass to the service.
        data: String,
    },

    /// Request that allows the user to poll for the status asynchronous responses
    Poll {
        /// Offset at which events need to be provided. Events are all ordered
        /// with sequence numbers and it is up to the user to specify which
        /// events it wants to receive from an offset in the sequence.
        offset: u64,

        /// The maximum number of items the user would prefer to receive in a single response.
        count: u32,

        /// If this option is set, the server will discard the events that have a
        /// sequence number lower than the Offset.
        #[serde(skip_serializing_if = "std::ops::Not::not", default)]
        discard_previous: bool,
    },
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ExecuteGatewayRequest {
    pub data: String,
}

/// Response returned by requests that are asynchronous.
#[derive(Debug, Deserialize)]
pub struct AsyncResponse {
    /// The response identifier. It uniquely identifies the event and
    /// orders it in the sequence of events expected by the user. The
    /// identifier can be used by the user to receive a response to the
    /// request once it is ready.
    pub id: u64,
}

/// Response returned by a poll request containing a list of events.
#[derive(Debug, Serialize, Deserialize)]
pub struct PollEventResponse {
    /// The offset at which the list of events start.
    pub offset: u64,

    /// The list of events starting from the provided offset.
    pub events: Vec<Event>,
}

/// Structure of events that can be returned from the server.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Event {
    /// The event as a result of a request that has failed.
    Error {
        /// Identifier of the asynchronous response.
        id: u64,

        /// Numeric representation fo the error.
        error_code: i32,

        /// Description of the error.
        description: String,
    },

    /// The event as a result of an asynchronous service request that has succeeded.
    // This needs to be defined before `DeployService` due to field ambiguity.
    ExecuteService {
        /// Identifier of the asynchronous response.
        id: u64,

        /// Hex-encoded address of the service that emitted the event.
        address: String,

        /// Output generated by the service at the end of its execution.
        output: String,
    },

    /// The event as a result of an asynchronous service request that has succeeded.
    DeployService {
        /// Identifier of the asynchronous response.
        id: u64,

        /// Hex-encoded address of the deployed service.
        address: String,
    },
}