dbcrossbarlib 0.5.3

Library for copying data between databases (pre-release)
Documentation
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! A Google Cloud REST client.

use bigml::wait::{wait, BackoffType, WaitOptions, WaitStatus};
use hyper::StatusCode;
use mime::{self, Mime};
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use reqwest::{
    self,
    header::{HeaderMap, CONTENT_TYPE},
    IntoUrl,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{error, fmt, time::Duration};

use super::auth::{authenticator, AccessToken, Authenticator};
use crate::common::*;
use crate::tokio_glue::IdiomaticBytesStream;

/// The OAuth2 scopes that we'll need.
///
/// TODO: For pure storage operations, consider having a storage-only scope.
static SCOPES: &[&str] = &[
    "https://www.googleapis.com/auth/devstorage.read_write",
    "https://www.googleapis.com/auth/bigquery",
];

/// An empty `GET` query.
#[derive(Debug, Serialize)]
pub(crate) struct NoQuery;

/// Alternative media types for Google Cloud REST APIs.
#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
#[allow(dead_code)]
pub(crate) enum Alt {
    /// Return data in JSON format.
    Json,
    /// Return the underlying media data.
    Media,
    /// Return data in Protobuf format.
    Proto,
}

/// An HTTP client error. We break out a few specified statuses our caller might
/// care about.
#[derive(Debug)]
pub(crate) enum ClientError {
    /// The resource at URL was not found.
    NotFound { method: String, url: Url },
    /// Another error occured. We don't currently care about the details.
    Other(Error),
}

impl fmt::Display for ClientError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ClientError::NotFound { method, url } => {
                write!(f, "cannot {} {}: Not Found", method, url)
            }
            ClientError::Other(err) => err.fmt(f),
        }
    }
}

impl error::Error for ClientError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            ClientError::NotFound { .. } => None,
            ClientError::Other(err) => err.source(),
        }
    }
}

impl From<Error> for ClientError {
    fn from(err: Error) -> Self {
        ClientError::Other(err)
    }
}

impl From<serde_json::Error> for ClientError {
    fn from(err: serde_json::Error) -> Self {
        ClientError::Other(err.into())
    }
}

impl From<bigml::Error> for ClientError {
    fn from(err: bigml::Error) -> Self {
        ClientError::Other(err.into())
    }
}

/// A Google Cloud REST client using OAuth2.
pub(crate) struct Client {
    /// An authenticator that provides OAuth2 tokens.
    authenticator: Authenticator,

    /// Our HTTP client.
    client: reqwest::Client,
}

impl Client {
    /// Create a new Google Cloud client.
    #[instrument(level = "trace")]
    pub(crate) async fn new() -> Result<Client, ClientError> {
        let authenticator = authenticator().await?;
        let client = reqwest::Client::new();
        Ok(Client {
            authenticator,
            client,
        })
    }

    /// Make an HTTP GET request and return the response.
    async fn get_helper(
        &self,
        url: &Url,
        headers: HeaderMap,
    ) -> Result<reqwest::Response, ClientError> {
        trace!("GET {}", url);
        let token = self.token().await?;
        let wait_options = WaitOptions::default()
            .backoff_type(BackoffType::Exponential)
            .retry_interval(Duration::from_secs(10))
            // Don't retry too much because we're probably classifying some
            // permanent errors as temporary.
            .allowed_errors(3);
        wait(&wait_options, move || {
            let token = token.clone();
            let headers = headers.clone();
            async move {
                let resp_result = self
                    .client
                    .get(url.as_str())
                    .bearer_auth(token.as_str())
                    .headers(headers)
                    .send()
                    .await;
                match resp_result {
                    // The HTTP request failed outright, because of something
                    // like a DNS error or whatever.
                    Err(err) => {
                        // Request and timeout errors look like the kind of
                        // things we should probably retry. But this is based on
                        // guesswork not experience.
                        let temporary = err.is_request() || err.is_timeout();
                        let err: Error = err.into();
                        let err: ClientError =
                            err.context(format!("could not GET {}", url)).into();
                        if temporary {
                            WaitStatus::FailedTemporarily(err)
                        } else {
                            WaitStatus::FailedPermanently(err)
                        }
                    }
                    // We talked to the server and it returned a server-side
                    // error (50-599). There's a chance that things might work
                    // next time, we hope.
                    Ok(resp) if resp.status().is_server_error() => {
                        WaitStatus::FailedTemporarily(
                            self.handle_error("GET", url, resp).await,
                        )
                    }
                    Ok(resp) => WaitStatus::Finished(resp),
                }
            }
            .boxed()
        })
        .await
    }

    /// Make an HTTP GET request with the specified URL and query parameters,
    /// and deserialize the result.
    #[instrument(level = "trace", skip(self))]
    pub(crate) async fn get<Output, U, Query>(
        &self,
        url: U,
        query: Query,
    ) -> Result<Output, ClientError>
    where
        Output: fmt::Debug + DeserializeOwned,
        U: IntoUrl + fmt::Debug,
        Query: fmt::Debug + Serialize,
    {
        let url = build_url(url, query)?;
        let headers = HeaderMap::default();
        let http_resp = self.get_helper(&url, headers).await?;
        self.handle_response("GET", &url, http_resp).await
    }

    /// Make an HTTP GET request with the specified URL and query parameters,
    /// and return the result as a stream.
    #[instrument(level = "trace", skip(self, headers))]
    pub(crate) async fn get_response<U, Query>(
        &self,
        url: U,
        query: Query,
        headers: HeaderMap,
    ) -> Result<reqwest::Response, ClientError>
    where
        U: IntoUrl + fmt::Debug,
        Query: fmt::Debug + Serialize,
    {
        let url = build_url(url, query)?;
        let http_resp = self.get_helper(&url, headers).await?;
        if http_resp.status().is_success() {
            Ok(http_resp)
        } else {
            Err(self.handle_error("GET", &url, http_resp).await)
        }
    }

    /// Make an HTTP POST request with the specified URL and body.
    #[instrument(level = "trace", skip(self, body))]
    pub(crate) async fn post<Output, U, Query, Body>(
        &self,
        url: U,
        query: Query,
        body: Body,
    ) -> Result<Output, ClientError>
    where
        Output: fmt::Debug + DeserializeOwned,
        U: IntoUrl + fmt::Debug,
        Query: fmt::Debug + Serialize,
        Body: fmt::Debug + Serialize,
    {
        let url = build_url(url, query)?;
        trace!("POST {} {:?}", url, body);
        trace!("serialied {}", serde_json::to_string(&body)?);
        let token = self.token().await?;
        let http_resp = self
            .client
            .post(url.as_str())
            .bearer_auth(token.as_str())
            .json(&body)
            .send()
            .await
            .with_context(|| format!("could not POST {}", url))?;
        self.handle_response("POST", &url, http_resp).await
    }

    /// Post a stream of data to the specified URL.
    #[instrument(level = "trace", skip(self, stream))]
    pub(crate) async fn post_stream<U, Query>(
        &self,
        url: U,
        query: Query,
        stream: IdiomaticBytesStream,
    ) -> Result<(), ClientError>
    where
        U: IntoUrl + fmt::Debug,
        Query: fmt::Debug + Serialize,
    {
        let url = build_url(url, query)?;
        trace!("POST {} with stream", url);
        let body = reqwest::Body::wrap_stream(stream);
        let token = self.token().await?;
        let http_resp = self
            .client
            .post(url.as_str())
            .bearer_auth(token.as_str())
            .body(body)
            .send()
            .await
            .with_context(|| format!("could not POST {}", url))?;
        if http_resp.status().is_success() {
            Ok(())
        } else {
            Err(self.handle_error("POST", &url, http_resp).await)
        }
    }

    /// Delete the specified URL.
    #[instrument(level = "trace", skip(self))]
    pub(crate) async fn delete<U, Query>(
        &self,
        url: U,
        query: Query,
    ) -> Result<(), ClientError>
    where
        U: IntoUrl + fmt::Debug,
        Query: fmt::Debug + Serialize,
    {
        let url = build_url(url, query)?;
        trace!("DELETE {}", url);
        let token = self.token().await?;
        let http_resp = self
            .client
            .delete(url.as_str())
            .bearer_auth(token.as_str())
            .send()
            .await
            .with_context(|| format!("error deleting {}", url))?;
        if http_resp.status().is_success() {
            Ok(())
        } else {
            Err(self.handle_error("DELETE", &url, http_resp).await)
        }
    }

    /// Get an access token.
    #[instrument(level = "trace", skip(self))]
    async fn token(&self) -> Result<AccessToken> {
        self.authenticator
            .token(SCOPES)
            .await
            .context("could not get Google Cloud OAuth2 token")
    }

    /// Handle an HTTP response.
    async fn handle_response<Output>(
        &self,
        method: &str,
        url: &Url,
        http_resp: reqwest::Response,
    ) -> Result<Output, ClientError>
    where
        Output: fmt::Debug + DeserializeOwned,
    {
        if http_resp.status().is_success() {
            let resp = http_resp.json::<Output>().await.with_context(|| {
                format!("error fetching JSON response from {}", url)
            })?;
            trace!("{} returned {:?}", method, resp);
            Ok(resp)
        } else {
            Err(self.handle_error(method, url, http_resp).await)
        }
    }

    /// Handle an HTPP error response.
    async fn handle_error(
        &self,
        method: &str,
        url: &Url,
        http_resp: reqwest::Response,
    ) -> ClientError {
        // Return 404 Not Found as a special case.
        if http_resp.status() == StatusCode::NOT_FOUND {
            return ClientError::NotFound {
                method: method.to_owned(),
                url: url.to_owned(),
            };
        }

        // Decide if we should even try to parse this response as JSON before we
        // consume our http_resp.
        let should_parse_as_json = response_claims_to_be_json(&http_resp);

        // Fetch the error body.
        let err_body_result = http_resp
            .bytes()
            .await
            .with_context(|| format!("error fetching error response from {}", url));
        let err_body = match err_body_result {
            Ok(err_body) => err_body,
            Err(err) => return err.into(),
        };

        // Try to return a nice JSON error.
        if should_parse_as_json {
            if let Ok(resp) = serde_json::from_slice::<ErrorResponse>(&err_body) {
                trace!("{} error {:?}", method, resp);
                let err: Error = resp.error.into();
                return err.context(format!("{} error {}", method, url)).into();
            }
        }

        // We've run afoul of
        // https://github.com/googleapis/google-cloud-ruby/issues/5180 or
        // something equally terrible, so just report whatever we have.
        let raw_err = String::from_utf8_lossy(&err_body);
        trace!(
            "{} {}: expected JSON describing error, but got {:?}",
            method,
            url,
            raw_err,
        );
        let err = format_err!("expected JSON describing error, but got {:?}", raw_err);
        err.context(format!("{} error {}", method, url)).into()
    }
}

/// Construct a URL from something we can convert to URL, and something that we
/// can serialize as a query string.
fn build_url<U, Query>(url: U, query: Query) -> Result<Url>
where
    U: IntoUrl,
    Query: fmt::Debug + Serialize,
{
    let mut url = url.into_url().context("could not parse URL")?;
    let query_str = serde_urlencoded::to_string(&query)?;
    if !query_str.is_empty() {
        url.set_query(Some(&query_str));
    }
    Ok(url)
}

/// A Google Cloud error response.
#[derive(Debug, Deserialize)]
struct ErrorResponse {
    /// The actual error.
    error: GCloudError,
}

/// Information about a GCloud error.
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub(crate) struct GCloudError {
    pub(crate) code: i32,
    pub(crate) message: String,
    pub(crate) errors: Vec<ErrorDetail>,
}

impl fmt::Display for GCloudError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Google Cloud error: {} {}", self.code, self.message)
    }
}

impl error::Error for GCloudError {}

/// Details about an individial GCloud error.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub(crate) struct ErrorDetail {
    pub(crate) domain: String,
    pub(crate) reason: String,
    pub(crate) message: String,
    pub(crate) location_type: Option<String>,
    pub(crate) location: Option<String>,
}

/// Percent-encode a string for use as a URL path component.
pub(crate) fn percent_encode(s: &str) -> impl fmt::Display + '_ {
    utf8_percent_encode(s, NON_ALPHANUMERIC)
}

/// Returns `true` if `http_response` claims to be a JSON response.
pub(crate) fn response_claims_to_be_json(http_resp: &reqwest::Response) -> bool {
    let content_type = match http_resp.headers().get(CONTENT_TYPE) {
        Some(content_type) => content_type,
        None => return false,
    };
    let content_type_str = match content_type.to_str() {
        Ok(content_type_str) => content_type_str,
        Err(err) => {
            error!("Non-ASCII content type {:?}: {}", content_type, err);
            return false;
        }
    };
    let content_type_mime = match content_type_str.parse::<Mime>() {
        Ok(content_type_mime) => content_type_mime,
        Err(err) => {
            error!(
                "Could not parse content type {:?}: {}",
                content_type_str, err,
            );
            return false;
        }
    };
    content_type_mime.type_() == mime::APPLICATION
        && content_type_mime.subtype() == mime::JSON
}

/// Given an `Error`, look to see if it's a wrapper around `reqwest::Error`, and
/// if so, return the original error. Otherwise return `None`.
pub(crate) fn original_http_error(err: &Error) -> Option<&reqwest::Error> {
    // Walk the chain of all errors, ending with the original root cause.
    for cause in err.chain() {
        // If this error is a `reqwest::Error`, return it.
        if let Some(reqwest_error) = cause.downcast_ref::<reqwest::Error>() {
            return Some(reqwest_error);
        }
    }
    None
}