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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use chrono::Utc;
pub use raystack::{
    is_tag_name, skyspark_tz_string_to_tz, BasicNumber, Coord, Date, DateTime, Error,
    FromHaysonError, Grid, Hayson, HisReadRange, Marker, Na, NewSkySparkClientError, Number,
    ParseJsonGridError, ParseRefError, ParseTagNameError, Ref, RemoveMarker, ScientificNumber,
    Symbol, TagName, Time, Uri, ValueExt, Xstr,
};
use std::sync::Arc;
use tokio::runtime::Runtime;
use url::Url;

pub mod auth;

type Result<T> = std::result::Result<T, Error>;

/// A client for interacting with a SkySpark server.
#[derive(Debug)]
pub struct SkySparkClient {
    client: raystack::SkySparkClient,
    rt: Arc<Runtime>,
}

impl SkySparkClient {
    /// Create a new `SkySparkClient`.
    ///
    /// # Example
    /// ```rust,no_run
    /// # fn run() {
    /// use raystack_blocking::SkySparkClient;
    /// use url::Url;
    /// let url = Url::parse("https://skyspark.company.com/api/bigProject/").unwrap();
    /// let mut client = SkySparkClient::new(url, "username", "p4ssw0rd").unwrap();
    /// # }
    /// ```
    pub fn new(
        project_api_url: Url,
        username: &str,
        password: &str,
    ) -> std::result::Result<Self, NewSkySparkClientError> {
        let rt = Runtime::new().expect("could not create a new Tokio runtime");
        Self::new_with_runtime(project_api_url, username, password, Arc::new(rt))
    }

    /// Create a new `SkySparkClient` using an existing Tokio runtime.
    pub fn new_with_runtime(
        project_api_url: Url,
        username: &str,
        password: &str,
        rt: Arc<Runtime>,
    ) -> std::result::Result<Self, NewSkySparkClientError> {
        let rclient = reqwest::Client::new();
        let client = rt.block_on(raystack::SkySparkClient::new_with_client(
            project_api_url,
            username,
            password,
            rclient,
        ))?;
        Ok(Self { client, rt })
    }

    /// Return the project name for this client.
    pub fn project_name(&self) -> &str {
        self.client.project_name()
    }

    /// Return the project API url being used by this client.
    pub fn project_api_url(&self) -> &Url {
        self.client.project_api_url()
    }
}

impl SkySparkClient {
    /// Returns a grid containing basic server information.
    pub fn about(&mut self) -> Result<Grid> {
        self.rt.block_on(self.client.about())
    }

    /// Returns a grid describing what MIME types are available.
    pub fn formats(&mut self) -> Result<Grid> {
        self.rt.block_on(self.client.formats())
    }

    /// Returns a grid of history data for a single point.
    pub fn his_read(&mut self, id: &Ref, range: &HisReadRange) -> Result<Grid> {
        self.rt.block_on(self.client.his_read(id, range))
    }

    /// Writes boolean values to a single point.
    pub fn his_write_bool(&mut self, id: &Ref, his_data: &[(DateTime, bool)]) -> Result<Grid> {
        self.rt.block_on(self.client.his_write_bool(id, his_data))
    }

    /// Writes numeric values to a single point. `unit` must be a valid
    /// Haystack unit literal, such as `L/s` or `celsius`.
    pub fn his_write_num(&mut self, id: &Ref, his_data: &[(DateTime, Number)]) -> Result<Grid> {
        self.rt.block_on(self.client.his_write_num(id, his_data))
    }

    /// Writes string values to a single point.
    pub fn his_write_str(&mut self, id: &Ref, his_data: &[(DateTime, String)]) -> Result<Grid> {
        self.rt.block_on(self.client.his_write_str(id, his_data))
    }

    /// Writes boolean values with UTC timestamps to a single point.
    /// `time_zone_name` must be a valid SkySpark timezone name.
    pub fn utc_his_write_bool(
        &mut self,
        id: &Ref,
        time_zone_name: &str,
        his_data: &[(chrono::DateTime<Utc>, bool)],
    ) -> Result<Grid> {
        self.rt
            .block_on(self.client.utc_his_write_bool(id, time_zone_name, his_data))
    }

    /// Writes numeric values with UTC timestamps to a single point.
    /// `unit` must be a valid Haystack unit literal, such as `L/s` or
    /// `celsius`.
    /// `time_zone_name` must be a valid SkySpark timezone name.
    pub fn utc_his_write_num(
        &mut self,
        id: &Ref,
        time_zone_name: &str,
        his_data: &[(chrono::DateTime<Utc>, Number)],
    ) -> Result<Grid> {
        self.rt
            .block_on(self.client.utc_his_write_num(id, time_zone_name, his_data))
    }

    /// Writes string values with UTC timestamps to a single point.
    /// `time_zone_name` must be a valid SkySpark timezone name.
    pub fn utc_his_write_str(
        &mut self,
        id: &Ref,
        time_zone_name: &str,
        his_data: &[(chrono::DateTime<Utc>, String)],
    ) -> Result<Grid> {
        self.rt
            .block_on(self.client.utc_his_write_str(id, time_zone_name, his_data))
    }

    /// The Haystack nav operation.
    pub fn nav(&mut self, nav_id: Option<&Ref>) -> Result<Grid> {
        self.rt.block_on(self.client.nav(nav_id))
    }

    /// Returns a grid containing the operations available on the server.
    pub fn ops(&mut self) -> Result<Grid> {
        self.rt.block_on(self.client.ops())
    }

    /// Returns a grid containing the records matching the given Axon
    /// filter string.
    pub fn read(&mut self, filter: &str, limit: Option<u64>) -> Result<Grid> {
        self.rt.block_on(self.client.read(filter, limit))
    }

    /// Returns a grid containing the records matching the given id
    /// `Ref`s.
    pub fn read_by_ids(&mut self, ids: &[Ref]) -> Result<Grid> {
        self.rt.block_on(self.client.read_by_ids(ids))
    }
}

impl SkySparkClient {
    pub fn eval(&mut self, axon_expr: &str) -> Result<Grid> {
        self.rt.block_on(self.client.eval(axon_expr))
    }
}