autumn-plugin-shopify 0.3.0

Autumn plugin: Shopify apps — signed webhooks routed by topic, OAuth install with expiring tokens, and a throttle-aware Admin GraphQL client.
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
//! Admin GraphQL API client.
//!
//! [`ShopifyClient`] posts GraphQL documents to
//! `https://{shop}/admin/api/{version}/graphql.json` with the
//! `X-Shopify-Access-Token` header.
//!
//! Shopify does not run a throttled request, so a retry is safe. The
//! client retries a `THROTTLED` GraphQL error and an HTTP 429, up to
//! [`RetryPolicy::max_retries`]. It does not retry other failures: a
//! second run of a mutation can change data two times.

use std::time::Duration;

use serde::Deserialize;
use serde::de::DeserializeOwned;

use crate::error::{Error, Result};
use crate::types::{ApiVersion, ShopDomain, Topic};

/// Request timeout for Shopify calls.
pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
/// The longest error body the client keeps, in chars.
pub(crate) const MAX_ERROR_BODY_CHARS: usize = 500;
/// Header that carries the Admin API access token.
pub const ACCESS_TOKEN_HEADER: &str = "x-shopify-access-token";

/// How the client retries throttled calls.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct RetryPolicy {
    /// The number of retries after the first try.
    pub max_retries: u32,
    /// The longest wait before one retry.
    pub max_wait: Duration,
}

impl RetryPolicy {
    /// Make a policy.
    #[must_use]
    pub const fn new(max_retries: u32, max_wait: Duration) -> Self {
        Self {
            max_retries,
            max_wait,
        }
    }
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_retries: 3,
            max_wait: Duration::from_secs(10),
        }
    }
}

/// The shop record from [`ShopifyClient::shop_info`].
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ShopInfo {
    /// The shop name.
    pub name: String,
    /// The `*.myshopify.com` domain.
    pub myshopify_domain: String,
}

/// An Admin GraphQL client for one shop and one token.
///
/// A clone is cheap. `Debug` redacts the token.
#[derive(Clone)]
pub struct ShopifyClient {
    http: reqwest::Client,
    shop: ShopDomain,
    base_url: String,
    api_version: ApiVersion,
    token: String,
    retry: RetryPolicy,
}

impl std::fmt::Debug for ShopifyClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ShopifyClient")
            .field("shop", &self.shop)
            .field("base_url", &self.base_url)
            .field("api_version", &self.api_version)
            .field("token", &"<redacted>")
            .field("retry", &self.retry)
            .finish_non_exhaustive()
    }
}

impl ShopifyClient {
    /// Make a client for `shop` with a new HTTP connection pool.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Config`] when the token is blank, or
    /// [`Error::Http`] when the HTTP client does not build.
    pub fn new(
        shop: ShopDomain,
        api_version: ApiVersion,
        access_token: impl Into<String>,
    ) -> Result<Self> {
        let token = access_token.into();
        if token.trim().is_empty() {
            return Err(Error::Config("the access token is blank".to_owned()));
        }
        Ok(Self::from_parts(http_client()?, shop, api_version, token))
    }

    /// Make a client on a shared HTTP pool.
    pub(crate) fn from_parts(
        http: reqwest::Client,
        shop: ShopDomain,
        api_version: ApiVersion,
        token: String,
    ) -> Self {
        let base_url = format!("https://{shop}");
        Self {
            http,
            shop,
            base_url,
            api_version,
            token,
            retry: RetryPolicy::default(),
        }
    }

    /// Send requests to `base_url` instead of `https://{shop}`. Use it for
    /// a local stub in tests. The requests carry the access token, so use
    /// only a host you trust.
    #[must_use]
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        base_url
            .into()
            .trim_end_matches('/')
            .clone_into(&mut self.base_url);
        self
    }

    /// Set the retry policy.
    #[must_use]
    pub const fn with_retry(mut self, retry: RetryPolicy) -> Self {
        self.retry = retry;
        self
    }

    /// The shop this client calls.
    #[must_use]
    pub const fn shop(&self) -> &ShopDomain {
        &self.shop
    }

    /// The API version this client uses.
    #[must_use]
    pub const fn api_version(&self) -> &ApiVersion {
        &self.api_version
    }

    /// The GraphQL endpoint URL.
    #[must_use]
    pub fn endpoint(&self) -> String {
        format!(
            "{}/admin/api/{}/graphql.json",
            self.base_url, self.api_version
        )
    }

    /// Run a GraphQL document and return its `data`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Http`] on a transport failure,
    /// [`Error::ShopifyApi`] on a non-success status, [`Error::GraphQl`]
    /// when the response has errors, [`Error::UnexpectedResponse`] when the
    /// body has no `data`, and [`Error::Throttled`] when the retries run
    /// out.
    pub async fn graphql(
        &self,
        query: &str,
        variables: serde_json::Value,
    ) -> Result<serde_json::Value> {
        let payload = serde_json::json!({ "query": query, "variables": variables });
        let mut retries = 0;
        loop {
            let response = self
                .http
                .post(self.endpoint())
                .header(ACCESS_TOKEN_HEADER, &self.token)
                .json(&payload)
                .send()
                .await
                .map_err(Error::Http)?;
            let wait = if response.status() == reqwest::StatusCode::TOO_MANY_REQUESTS {
                retry_after(response.headers())
            } else if response.status().is_success() {
                let body: serde_json::Value = read_json(response).await?;
                match throttle_wait(&body) {
                    Some(wait) => wait,
                    None => return into_data(body),
                }
            } else {
                return Err(api_error(response).await);
            };
            if retries >= self.retry.max_retries {
                return Err(Error::Throttled { retries });
            }
            retries += 1;
            tracing::debug!(shop = %self.shop, retries, "shopify throttled the call; retrying");
            tokio::time::sleep(wait.min(self.retry.max_wait)).await;
        }
    }

    /// Run a GraphQL document and deserialize its `data` into `T`.
    ///
    /// # Errors
    ///
    /// See [`graphql`](Self::graphql). Also returns
    /// [`Error::UnexpectedResponse`] when `data` does not match `T`.
    pub async fn query<T: DeserializeOwned>(
        &self,
        query: &str,
        variables: serde_json::Value,
    ) -> Result<T> {
        let data = self.graphql(query, variables).await?;
        serde_json::from_value(data)
            .map_err(|err| unexpected(&format!("data does not match: {err}")))
    }

    /// Read the shop name and domain. The health check uses it.
    ///
    /// # Errors
    ///
    /// See [`query`](Self::query).
    pub async fn shop_info(&self) -> Result<ShopInfo> {
        #[derive(Deserialize)]
        struct Data {
            shop: ShopInfo,
        }
        let data: Data = self
            .query(
                "query AutumnShopInfo { shop { name myshopifyDomain } }",
                serde_json::json!({}),
            )
            .await?;
        Ok(data.shop)
    }

    /// Subscribe `callback_url` to `topic` (for example `orders/create`)
    /// with `webhookSubscriptionCreate`. Returns the subscription id.
    ///
    /// The compliance topics are not in the GraphQL topic enum. Declare
    /// them in `shopify.app.toml` (`compliance_topics`).
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidRequest`] for a malformed topic and
    /// [`Error::Config`] for a compliance topic. See also
    /// [`query`](Self::query). Returns [`Error::GraphQl`] with the
    /// `userErrors` messages when Shopify refuses the subscription.
    pub async fn subscribe_webhook(&self, topic: &str, callback_url: &str) -> Result<String> {
        #[derive(Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct Data {
            webhook_subscription_create: Payload,
        }
        #[derive(Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct Payload {
            webhook_subscription: Option<Subscription>,
            #[serde(default)]
            user_errors: Vec<UserError>,
        }
        #[derive(Deserialize)]
        struct Subscription {
            id: String,
        }
        #[derive(Deserialize)]
        struct UserError {
            message: String,
        }
        let topic = Topic::parse(topic)?;
        if topic.is_compliance() {
            return Err(Error::Config(format!(
                "{topic} is a compliance topic; declare it in shopify.app.toml compliance_topics"
            )));
        }
        let data: Data = self
            .query(
                SUBSCRIBE_MUTATION,
                serde_json::json!({
                    "topic": topic.graphql_enum(),
                    "webhookSubscription": { "uri": callback_url },
                }),
            )
            .await?;
        let payload = data.webhook_subscription_create;
        if !payload.user_errors.is_empty() {
            return Err(Error::GraphQl(
                payload.user_errors.into_iter().map(|e| e.message).collect(),
            ));
        }
        payload
            .webhook_subscription
            .map(|subscription| subscription.id)
            .ok_or_else(|| unexpected("webhookSubscriptionCreate returned no subscription"))
    }
}

/// Build the shared HTTP client: timeout and user agent.
///
/// # Errors
///
/// Returns [`Error::Http`] when the client does not build.
pub(crate) fn http_client() -> Result<reqwest::Client> {
    // No redirects: a redirect would carry the access token or the client
    // secret to another host.
    reqwest::Client::builder()
        .timeout(REQUEST_TIMEOUT)
        .redirect(reqwest::redirect::Policy::none())
        .user_agent(concat!("autumn-plugin-shopify/", env!("CARGO_PKG_VERSION")))
        .build()
        .map_err(Error::Http)
}

/// Turn a non-success response into [`Error::ShopifyApi`] with a
/// truncated body.
pub(crate) async fn api_error(response: reqwest::Response) -> Error {
    let status = response.status().as_u16();
    let body = response.text().await.unwrap_or_default();
    Error::ShopifyApi {
        status,
        body: body.chars().take(MAX_ERROR_BODY_CHARS).collect(),
    }
}

/// Read a success body as JSON.
async fn read_json(response: reqwest::Response) -> Result<serde_json::Value> {
    let bytes = response.bytes().await.map_err(Error::Http)?;
    serde_json::from_slice(&bytes).map_err(|_| unexpected("the response is not JSON"))
}

/// Return `data`, or the `errors` messages.
fn into_data(mut body: serde_json::Value) -> Result<serde_json::Value> {
    if let Some(errors) = body.get("errors").and_then(serde_json::Value::as_array)
        && !errors.is_empty()
    {
        let messages = errors
            .iter()
            .map(|error| {
                error
                    .get("message")
                    .and_then(serde_json::Value::as_str)
                    .unwrap_or("unknown error")
                    .to_owned()
            })
            .collect();
        return Err(Error::GraphQl(messages));
    }
    match body.get_mut("data").map(serde_json::Value::take) {
        Some(data) if !data.is_null() => Ok(data),
        _ => Err(unexpected("the response has no data")),
    }
}

/// An [`Error::UnexpectedResponse`] for a 200 answer of the wrong shape.
fn unexpected(detail: &str) -> Error {
    Error::UnexpectedResponse(detail.chars().take(MAX_ERROR_BODY_CHARS).collect())
}

/// The wait a `THROTTLED` response asks for, from `extensions.cost`.
///
/// Wait = (requested cost - available points) / restore rate, at least
/// [`MIN_THROTTLE_WAIT`]. One second when the cost data is missing.
/// `None` when the response is not throttled.
pub(crate) fn throttle_wait(body: &serde_json::Value) -> Option<Duration> {
    let throttled = body.get("errors")?.as_array()?.iter().any(|error| {
        error
            .pointer("/extensions/code")
            .and_then(serde_json::Value::as_str)
            == Some("THROTTLED")
    });
    if !throttled {
        return None;
    }
    let cost = body.pointer("/extensions/cost");
    let number = |path: &str| {
        cost.and_then(|c| c.pointer(path))
            .and_then(serde_json::Value::as_f64)
    };
    let wait = match (
        number("/requestedQueryCost"),
        number("/throttleStatus/currentlyAvailable"),
        number("/throttleStatus/restoreRate"),
    ) {
        (Some(requested), Some(available), Some(rate)) if rate > 0.0 => {
            Duration::try_from_secs_f64(((requested - available) / rate).max(0.0))
                .unwrap_or(DEFAULT_WAIT)
                .max(MIN_THROTTLE_WAIT)
        }
        _ => DEFAULT_WAIT,
    };
    Some(wait)
}

/// The wait an HTTP 429 asks for, from `Retry-After` (seconds).
/// One second when the header is missing or bad.
pub(crate) fn retry_after(headers: &reqwest::header::HeaderMap) -> Duration {
    headers
        .get(reqwest::header::RETRY_AFTER)
        .and_then(|value| value.to_str().ok())
        .and_then(|value| value.trim().parse::<f64>().ok())
        .and_then(|secs| Duration::try_from_secs_f64(secs).ok())
        .unwrap_or(DEFAULT_WAIT)
}

/// The wait when Shopify gives no hint. Shopify recommends one second.
const DEFAULT_WAIT: Duration = Duration::from_secs(1);
/// The shortest wait after a throttle.
const MIN_THROTTLE_WAIT: Duration = Duration::from_millis(100);

/// The `webhookSubscriptionCreate` mutation.
const SUBSCRIBE_MUTATION: &str = "mutation AutumnSubscribeWebhook($topic: WebhookSubscriptionTopic!, $webhookSubscription: WebhookSubscriptionInput!) { webhookSubscriptionCreate(topic: $topic, webhookSubscription: $webhookSubscription) { webhookSubscription { id } userErrors { field message } } }";

#[cfg(test)]
mod tests;