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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::error::{HoundifyError, InvalidRequestInfoError};
use crate::Result;
use serde_json::{Map, Number, Value};
use url::form_urlencoded;

pub trait Query {
    fn get_url(&self, api_url: &str) -> String;
}

#[derive(Debug, Clone)]
pub struct RequestInfo {
    request_info_map: Map<String, Value>,
}

impl RequestInfo {
    pub fn new() -> Self {
        RequestInfo {
            request_info_map: Map::new(),
        }
    }

    /// Set the latitude of the request
    pub fn latitude(&mut self, v: f64) -> Option<InvalidRequestInfoError> {
        if v < -90.0 || v > 90.0 {
            return Some(InvalidRequestInfoError::new(
                "Latitude must between -90 and 90",
            ));
        }
        let n = match Number::from_f64(v) {
            Some(n) => n,
            None => return Some(InvalidRequestInfoError::new("Cannot parse latitude")),
        };
        &self
            .request_info_map
            .insert("Latitude".to_string(), Value::Number(n));
        None
    }

    /// Set the longitude of the request
    pub fn longitude(&mut self, v: f64) -> Option<InvalidRequestInfoError> {
        if v < -180.0 || v > 180.0 {
            return Some(InvalidRequestInfoError::new(
                "Longitude must between -180 and 180",
            ));
        }
        let n = match Number::from_f64(v) {
            Some(n) => n,
            None => return Some(InvalidRequestInfoError::new("Cannot parse longitude")),
        };
        &self
            .request_info_map
            .insert("Longitude".to_string(), Value::Number(n));
        None
    }

    /// Set timestamp
    pub fn timestamp(&mut self, v: u64) -> Option<InvalidRequestInfoError> {
        &self
            .request_info_map
            .insert("TimeStamp".to_string(), Value::Number(Number::from(v)));
        None
    }

    /// Set ClientID
    pub fn client_id(&mut self, v: &str) -> Option<InvalidRequestInfoError> {
        &self
            .request_info_map
            .insert("ClientID".to_string(), Value::String(v.to_string()));
        None
    }

    /// Set PositionTime
    pub fn position_time(&mut self, v: u64) -> Option<InvalidRequestInfoError> {
        &self
            .request_info_map
            .insert("PositionTime".to_string(), Value::Number(Number::from(v)));
        None
    }

    /// Set InputLanguageEnglishName
    pub fn input_language_english_name(&mut self, v: &str) -> Option<InvalidRequestInfoError> {
        &self.request_info_map.insert(
            "InputLanguageEnglishName".to_string(),
            Value::String(v.to_string()),
        );
        None
    }

    /// Set InputLanguageNativeName
    pub fn input_language_native_name(&mut self, v: &str) -> Option<InvalidRequestInfoError> {
        &self.request_info_map.insert(
            "InputLanguageNativeName".to_string(),
            Value::String(v.to_string()),
        );
        None
    }

    /// Set InputLanguageIETFTag
    pub fn input_language_ietf_tag(&mut self, v: &str) -> Option<InvalidRequestInfoError> {
        &self.request_info_map.insert(
            "InputLanguageIETFTag".to_string(),
            Value::String(v.to_string()),
        );
        None
    }

    /// Set OutputLanguageEnglishName
    pub fn output_language_english_name(&mut self, v: &str) -> Option<InvalidRequestInfoError> {
        &self.request_info_map.insert(
            "OutputLanguageEnglishName".to_string(),
            Value::String(v.to_string()),
        );
        None
    }

    /// Set OutputLanguageNativeName
    pub fn output_language_native_name(&mut self, v: &str) -> Option<InvalidRequestInfoError> {
        &self.request_info_map.insert(
            "OutputLanguageNativeName".to_string(),
            Value::String(v.to_string()),
        );
        None
    }

    /// Set OutputLanguageIETFTag
    pub fn output_language_ietf_tag(&mut self, v: &str) -> Option<InvalidRequestInfoError> {
        &self.request_info_map.insert(
            "OutputLanguageIETFTag".to_string(),
            Value::String(v.to_string()),
        );
        None
    }

    /// Set PartialTranscriptsDesired
    pub fn partial_transcript_desired(&mut self, v: bool) -> Option<InvalidRequestInfoError> {
        &self
            .request_info_map
            .insert("PartialTranscriptsDesired".to_string(), Value::Bool(v));
        None
    }

    /// Set arbitrary RequestInfo
    pub fn set(&mut self, k: String, v: Value) -> Option<InvalidRequestInfoError> {
        &self.request_info_map.insert(k, v);
        None
    }

    pub fn serialize(self) -> Result<String> {
        match serde_json::to_string(&self.request_info_map) {
            Ok(j) => Ok(j),
            Err(e) => Err(HoundifyError::new(e.into())),
        }
    }
}

#[derive(Debug)]
pub struct TextQuery<'a> {
    pub(crate) query: &'a str,
    pub(crate) user_id: &'a str,
    pub(crate) request_info: RequestInfo,
}

impl<'a> TextQuery<'a> {
    pub fn new(query: &'a str, user_id: &'a str, mut request_info: RequestInfo) -> TextQuery<'a> {
        request_info.set(
            "SDK".to_string(),
            Value::String("houndify-sdk-rust/1.0".to_string()),
        ); // TODO: get the SDK version from manifest?
        request_info.set("UserID".to_string(), Value::String(user_id.to_string()));
        TextQuery {
            query,
            user_id,
            request_info,
        }
    }
}

impl Query for TextQuery<'_> {
    fn get_url(&self, api_url: &str) -> String {
        let url: String = form_urlencoded::Serializer::new(format!("{}v1/text?", api_url))
            .append_pair("query", &self.query)
            .finish();
        url
    }
}

pub struct VoiceQuery<'a> {
    pub(crate) audio_stream: Box<dyn std::io::Read + Send>,
    pub(crate) user_id: &'a str,
    pub(crate) request_info: RequestInfo,
}

impl Query for VoiceQuery<'_> {
    fn get_url(&self, api_url: &str) -> String {
        return format!("{}v1/audio", api_url);
    }
}

impl<'a> VoiceQuery<'a> {
    pub fn new(
        audio_stream: Box<dyn std::io::Read + Send>,
        user_id: &'a str,
        mut request_info: RequestInfo,
    ) -> Self {
        request_info.set(
            "SDK".to_string(),
            Value::String("houndify-sdk-rust/1.0".to_string()),
        ); // TODO: get the SDK version from manifest?
        request_info.set("UserID".to_string(), Value::String(user_id.to_string()));
        VoiceQuery {
            audio_stream,
            user_id,
            request_info,
        }
    }
}