shuttle_api_client/
lib.rs

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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use std::time::Duration;

use anyhow::{Context, Result};
use headers::{Authorization, HeaderMapExt};
use percent_encoding::utf8_percent_encode;
use reqwest::header::HeaderMap;
use reqwest::Response;
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
use serde::{Deserialize, Serialize};
use shuttle_common::certificate::{
    AddCertificateRequest, CertificateListResponse, CertificateResponse, DeleteCertificateRequest,
};
use shuttle_common::log::{LogsRange, LogsResponseBeta};
use shuttle_common::models::deployment::{
    DeploymentListResponseBeta, DeploymentRequest, DeploymentRequestBeta, UploadArchiveResponseBeta,
};
use shuttle_common::models::project::{ProjectCreateRequestBeta, ProjectListResponseBeta};
use shuttle_common::models::{deployment, project, service, team, user};
use shuttle_common::resource::{
    ProvisionResourceRequestBeta, ResourceListResponseBeta, ResourceResponseBeta,
};
use shuttle_common::{resource, LogItem, VersionInfo};
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
use uuid::Uuid;

#[cfg(feature = "tracing")]
mod middleware;
#[cfg(feature = "tracing")]
use crate::middleware::LoggingMiddleware;
#[cfg(feature = "tracing")]
use tracing::{debug, error};

mod util;
use util::ToJson;

#[derive(Clone)]
pub struct ShuttleApiClient {
    pub client: ClientWithMiddleware,
    pub api_url: String,
    pub api_key: Option<String>,
}

impl ShuttleApiClient {
    pub fn new(api_url: String, api_key: Option<String>, headers: Option<HeaderMap>) -> Self {
        let mut builder = reqwest::Client::builder();
        if let Some(h) = headers {
            builder = builder.default_headers(h);
        }
        let client = builder.timeout(Duration::from_secs(60)).build().unwrap();

        let builder = reqwest_middleware::ClientBuilder::new(client);
        #[cfg(feature = "tracing")]
        let builder = builder.with(LoggingMiddleware);
        let client = builder.build();

        Self {
            client,
            api_url,
            api_key,
        }
    }

    pub fn set_auth_bearer(&self, builder: RequestBuilder) -> RequestBuilder {
        if let Some(ref api_key) = self.api_key {
            builder.bearer_auth(api_key)
        } else {
            builder
        }
    }

    pub async fn get_api_versions(&self) -> Result<VersionInfo> {
        let url = format!("{}/versions", self.api_url);

        self.client
            .get(url)
            .send()
            .await?
            .json()
            .await
            .context("parsing API version info")
    }

    pub async fn get_device_auth_ws(&self) -> Result<WebSocketStream<MaybeTlsStream<TcpStream>>> {
        self.ws_get("/device-auth/ws".to_owned())
            .await
            .with_context(|| "failed to connect to auth endpoint")
    }

    pub async fn check_project_name(&self, project_name: &str) -> Result<bool> {
        let url = format!("{}/projects/name/{project_name}", self.api_url);

        self.client
            .get(url)
            .send()
            .await
            .context("failed to check project name availability")?
            .to_json()
            .await
            .context("parsing name check response")
    }

    pub async fn check_project_name_beta(&self, project_name: &str) -> Result<bool> {
        let url = format!("{}/projects/{project_name}/name", self.api_url);

        self.client
            .get(url)
            .send()
            .await
            .context("failed to check project name availability")?
            .to_json()
            .await
            .context("parsing name check response")
    }

    pub async fn get_current_user_beta(&self) -> Result<user::UserResponse> {
        self.get_json("/users/me".to_owned()).await
    }

    pub async fn deploy(
        &self,
        project: &str,
        deployment_req: DeploymentRequest,
    ) -> Result<deployment::Response> {
        let path = format!("/projects/{project}/services/{project}");
        let deployment_req = rmp_serde::to_vec(&deployment_req)
            .context("serialize DeploymentRequest as a MessagePack byte vector")?;

        let url = format!("{}{}", self.api_url, path);
        let mut builder = self.client.post(url);
        builder = self.set_auth_bearer(builder);

        builder
            .header("Transfer-Encoding", "chunked")
            .body(deployment_req)
            .send()
            .await
            .context("failed to send deployment to the Shuttle server")?
            .to_json()
            .await
    }

    pub async fn deploy_beta(
        &self,
        project: &str,
        deployment_req: DeploymentRequestBeta,
    ) -> Result<deployment::DeploymentResponseBeta> {
        let path = format!("/projects/{project}/deployments");
        self.post_json(path, Some(deployment_req)).await
    }

    pub async fn upload_archive_beta(
        &self,
        project: &str,
        data: Vec<u8>,
    ) -> Result<UploadArchiveResponseBeta> {
        let path = format!("/projects/{project}/archives");

        let url = format!("{}{}", self.api_url, path);
        let mut builder = self.client.post(url);
        builder = self.set_auth_bearer(builder);

        builder
            .body(data)
            .send()
            .await
            .context("failed to upload archive")?
            .to_json()
            .await
    }

    pub async fn redeploy_beta(
        &self,
        project: &str,
        deployment_id: &str,
    ) -> Result<deployment::DeploymentResponseBeta> {
        let path = format!("/projects/{project}/deployments/{deployment_id}/redeploy");

        self.post_json(path, Option::<()>::None).await
    }

    pub async fn stop_service(&self, project: &str) -> Result<service::Summary> {
        let path = format!("/projects/{project}/services/{project}");

        self.delete_json(path).await
    }

    pub async fn stop_service_beta(&self, project: &str) -> Result<String> {
        let path = format!("/projects/{project}/deployments");

        self.delete_json(path).await
    }

    pub async fn get_service(&self, project: &str) -> Result<service::Summary> {
        let path = format!("/projects/{project}/services/{project}");

        self.get_json(path).await
    }

    pub async fn get_service_resources(&self, project: &str) -> Result<Vec<resource::Response>> {
        self.get_json(format!("/projects/{project}/services/{project}/resources"))
            .await
    }
    pub async fn get_service_resources_beta(
        &self,
        project: &str,
    ) -> Result<ResourceListResponseBeta> {
        self.get_json(format!("/projects/{project}/resources"))
            .await
    }

    pub async fn dump_service_resource(
        &self,
        project: &str,
        resource_type: &resource::Type,
    ) -> Result<Vec<u8>> {
        let r#type = resource_type.to_string();
        let r#type = utf8_percent_encode(&r#type, percent_encoding::NON_ALPHANUMERIC).to_owned();

        let res = self
            .get(
                format!(
                    "/projects/{project}/services/{project}/resources/{}/dump",
                    r#type
                ),
                Option::<()>::None,
            )
            .await?;

        let bytes = res.bytes().await?;

        Ok(bytes.to_vec())
    }

    pub async fn delete_service_resource(
        &self,
        project: &str,
        resource_type: &resource::Type,
    ) -> Result<()> {
        let r#type = resource_type.to_string();
        let r#type = utf8_percent_encode(&r#type, percent_encoding::NON_ALPHANUMERIC).to_owned();

        self.delete_json(format!(
            "/projects/{project}/services/{project}/resources/{}",
            r#type
        ))
        .await
    }
    pub async fn delete_service_resource_beta(
        &self,
        project: &str,
        resource_type: &resource::Type,
    ) -> Result<String> {
        let r#type = resource_type.to_string();
        let r#type = utf8_percent_encode(&r#type, percent_encoding::NON_ALPHANUMERIC).to_owned();

        self.delete_json(format!("/projects/{project}/resources/{}", r#type))
            .await
    }
    pub async fn provision_resource_beta(
        &self,
        project: &str,
        req: ProvisionResourceRequestBeta,
    ) -> Result<ResourceResponseBeta> {
        self.post_json(format!("/projects/{project}/resources"), Some(req))
            .await
    }
    pub async fn get_secrets_beta(&self, project: &str) -> Result<ResourceResponseBeta> {
        self.get_json(format!("/projects/{project}/resources/secrets"))
            .await
    }

    pub async fn list_certificates_beta(&self, project: &str) -> Result<CertificateListResponse> {
        self.get_json(format!("/projects/{project}/certificates"))
            .await
    }
    pub async fn add_certificate_beta(
        &self,
        project: &str,
        subject: String,
    ) -> Result<CertificateResponse> {
        self.post_json(
            format!("/projects/{project}/certificates"),
            Some(AddCertificateRequest { subject }),
        )
        .await
    }
    pub async fn delete_certificate_beta(&self, project: &str, subject: String) -> Result<String> {
        self.delete_json_with_body(
            format!("/projects/{project}/certificates"),
            DeleteCertificateRequest { subject },
        )
        .await
    }

    pub async fn create_project(
        &self,
        project: &str,
        config: &project::Config,
    ) -> Result<project::Response> {
        self.post_json(format!("/projects/{project}"), Some(config))
            .await
            .context("failed to make create project request")
    }
    pub async fn create_project_beta(&self, name: &str) -> Result<project::ProjectResponseBeta> {
        self.post_json(
            "/projects",
            Some(ProjectCreateRequestBeta {
                name: name.to_string(),
            }),
        )
        .await
    }

    pub async fn clean_project(&self, project: &str) -> Result<String> {
        let path = format!("/projects/{project}/clean");

        self.post_json(path, Option::<String>::None)
            .await
            .context("failed to get clean output")
    }

    pub async fn get_project(&self, project: &str) -> Result<project::Response> {
        self.get_json(format!("/projects/{project}")).await
    }
    pub async fn get_project_beta(&self, project: &str) -> Result<project::ProjectResponseBeta> {
        self.get_json(format!("/projects/{project}")).await
    }

    pub async fn get_projects_list(&self) -> Result<Vec<project::Response>> {
        self.get_json("/projects".to_owned()).await
    }
    pub async fn get_projects_list_beta(&self) -> Result<ProjectListResponseBeta> {
        self.get_json("/projects".to_owned()).await
    }

    pub async fn update_project_beta(
        &self,
        project: &str,
        req: project::ProjectUpdateRequestBeta,
    ) -> Result<project::ProjectResponseBeta> {
        self.put_json(format!("/projects/{project}"), Some(req))
            .await
    }

    pub async fn stop_project(&self, project: &str) -> Result<project::Response> {
        let path = format!("/projects/{project}");

        self.delete_json(path).await
    }

    pub async fn delete_project(&self, project: &str) -> Result<String> {
        let path = format!("/projects/{project}/delete");
        let url = format!("{}{}", self.api_url, path);
        let mut builder = self.client.delete(url);
        builder = self.set_auth_bearer(builder);
        // project delete on alpha can take a while
        builder = builder.timeout(Duration::from_secs(60 * 5));

        builder
            .send()
            .await
            .context("failed to make delete request")?
            .to_json()
            .await
    }
    pub async fn delete_project_beta(&self, project: &str) -> Result<String> {
        self.delete_json(format!("/projects/{project}")).await
    }

    pub async fn get_teams_list(&self) -> Result<Vec<team::Response>> {
        self.get_json("/teams".to_string()).await
    }

    pub async fn get_team_projects_list(&self, team_id: &str) -> Result<Vec<project::Response>> {
        self.get_json(format!("/teams/{team_id}/projects")).await
    }
    pub async fn get_team_projects_list_beta(
        &self,
        team_id: &str,
    ) -> Result<ProjectListResponseBeta> {
        self.get_json(format!("/teams/{team_id}/projects")).await
    }

    pub async fn get_logs(
        &self,
        project: &str,
        deployment_id: &str,
        range: LogsRange,
    ) -> Result<Vec<LogItem>> {
        let mut path = format!("/projects/{project}/deployments/{deployment_id}/logs");
        Self::add_range_query(range, &mut path);

        self.get_json(path).await
    }
    pub async fn get_deployment_logs_beta(
        &self,
        project: &str,
        deployment_id: &str,
    ) -> Result<LogsResponseBeta> {
        let path = format!("/projects/{project}/deployments/{deployment_id}/logs");

        self.get_json(path).await
    }
    pub async fn get_project_logs_beta(&self, project: &str) -> Result<LogsResponseBeta> {
        let path = format!("/projects/{project}/logs");

        self.get_json(path).await
    }

    pub async fn get_logs_ws(
        &self,
        project: &str,
        deployment_id: &str,
        range: LogsRange,
    ) -> Result<WebSocketStream<MaybeTlsStream<TcpStream>>> {
        let mut path = format!("/projects/{project}/ws/deployments/{deployment_id}/logs");
        Self::add_range_query(range, &mut path);

        self.ws_get(path).await
    }

    fn add_range_query(range: LogsRange, path: &mut String) {
        match range {
            LogsRange::Head(n) => {
                path.push_str("?head=");
                path.push_str(&n.to_string())
            }
            LogsRange::Tail(n) => {
                path.push_str("?tail=");
                path.push_str(&n.to_string())
            }
            _ => {}
        };
    }

    pub async fn get_deployments(
        &self,
        project: &str,
        page: u32,
        limit: u32,
    ) -> Result<Vec<deployment::Response>> {
        let path = format!(
            "/projects/{project}/deployments?page={}&limit={}",
            page.saturating_sub(1),
            limit,
        );

        self.get_json(path).await
    }
    pub async fn get_deployments_beta(
        &self,
        project: &str,
        page: i32,
        per_page: i32,
    ) -> Result<DeploymentListResponseBeta> {
        let path = format!(
            "/projects/{project}/deployments?page={}&per_page={}",
            page.saturating_sub(1).max(0),
            per_page.max(1),
        );

        self.get_json(path).await
    }
    pub async fn get_current_deployment_beta(
        &self,
        project: &str,
    ) -> Result<Option<deployment::DeploymentResponseBeta>> {
        let path = format!("/projects/{project}/deployments/current");

        self.get_json(path).await
    }

    pub async fn get_deployment_beta(
        &self,
        project: &str,
        deployment_id: &str,
    ) -> Result<deployment::DeploymentResponseBeta> {
        let path = format!("/projects/{project}/deployments/{deployment_id}");

        self.get_json(path).await
    }

    pub async fn get_deployment_details(
        &self,
        project: &str,
        deployment_id: &Uuid,
    ) -> Result<deployment::Response> {
        let path = format!("/projects/{project}/deployments/{deployment_id}");

        self.get_json(path).await
    }

    pub async fn reset_api_key(&self) -> Result<Response> {
        self.put("/users/reset-api-key", Option::<()>::None).await
    }

    pub async fn ws_get(&self, path: String) -> Result<WebSocketStream<MaybeTlsStream<TcpStream>>> {
        let ws_url = self.api_url.clone().replace("http", "ws");
        let url = format!("{ws_url}{path}");
        let mut req = url.into_client_request()?;

        #[cfg(feature = "tracing")]
        debug!("WS Request: {} {}", req.method(), req.uri());

        if let Some(ref api_key) = self.api_key {
            let auth_header = Authorization::bearer(api_key.as_ref())?;
            req.headers_mut().typed_insert(auth_header);
        }

        let (stream, _) = connect_async(req).await.with_context(|| {
            #[cfg(feature = "tracing")]
            error!("failed to connect to websocket");
            "could not connect to websocket"
        })?;

        Ok(stream)
    }

    pub async fn get<T: Serialize>(
        &self,
        path: impl AsRef<str>,
        body: Option<T>,
    ) -> Result<Response> {
        let url = format!("{}{}", self.api_url, path.as_ref());

        let mut builder = self.client.get(url);
        builder = self.set_auth_bearer(builder);

        if let Some(body) = body {
            let body = serde_json::to_string(&body)?;
            #[cfg(feature = "tracing")]
            debug!("Outgoing body: {}", body);
            builder = builder.body(body);
            builder = builder.header("Content-Type", "application/json");
        }

        Ok(builder.send().await?)
    }

    pub async fn get_json<R>(&self, path: impl AsRef<str>) -> Result<R>
    where
        R: for<'de> Deserialize<'de>,
    {
        self.get(path, Option::<()>::None).await?.to_json().await
    }

    pub async fn get_json_with_body<R, T: Serialize>(
        &self,
        path: impl AsRef<str>,
        body: T,
    ) -> Result<R>
    where
        R: for<'de> Deserialize<'de>,
    {
        self.get(path, Some(body)).await?.to_json().await
    }

    pub async fn post<T: Serialize>(
        &self,
        path: impl AsRef<str>,
        body: Option<T>,
    ) -> Result<Response> {
        let url = format!("{}{}", self.api_url, path.as_ref());

        let mut builder = self.client.post(url);
        builder = self.set_auth_bearer(builder);

        if let Some(body) = body {
            let body = serde_json::to_string(&body)?;
            #[cfg(feature = "tracing")]
            debug!("Outgoing body: {}", body);
            builder = builder.body(body);
            builder = builder.header("Content-Type", "application/json");
        }

        Ok(builder.send().await?)
    }

    pub async fn post_json<T: Serialize, R>(
        &self,
        path: impl AsRef<str>,
        body: Option<T>,
    ) -> Result<R>
    where
        R: for<'de> Deserialize<'de>,
    {
        self.post(path, body).await?.to_json().await
    }

    pub async fn put<T: Serialize>(
        &self,
        path: impl AsRef<str>,
        body: Option<T>,
    ) -> Result<Response> {
        let url = format!("{}{}", self.api_url, path.as_ref());

        let mut builder = self.client.put(url);
        builder = self.set_auth_bearer(builder);

        if let Some(body) = body {
            let body = serde_json::to_string(&body)?;
            #[cfg(feature = "tracing")]
            debug!("Outgoing body: {}", body);
            builder = builder.body(body);
            builder = builder.header("Content-Type", "application/json");
        }

        Ok(builder.send().await?)
    }

    pub async fn put_json<T: Serialize, R>(
        &self,
        path: impl AsRef<str>,
        body: Option<T>,
    ) -> Result<R>
    where
        R: for<'de> Deserialize<'de>,
    {
        self.put(path, body).await?.to_json().await
    }

    pub async fn delete<T: Serialize>(
        &self,
        path: impl AsRef<str>,
        body: Option<T>,
    ) -> Result<Response> {
        let url = format!("{}{}", self.api_url, path.as_ref());

        let mut builder = self.client.delete(url);
        builder = self.set_auth_bearer(builder);

        if let Some(body) = body {
            let body = serde_json::to_string(&body)?;
            #[cfg(feature = "tracing")]
            debug!("Outgoing body: {}", body);
            builder = builder.body(body);
            builder = builder.header("Content-Type", "application/json");
        }

        Ok(builder.send().await?)
    }

    pub async fn delete_json<R>(&self, path: impl AsRef<str>) -> Result<R>
    where
        R: for<'de> Deserialize<'de>,
    {
        self.delete(path, Option::<()>::None).await?.to_json().await
    }

    pub async fn delete_json_with_body<R, T: Serialize>(
        &self,
        path: impl AsRef<str>,
        body: T,
    ) -> Result<R>
    where
        R: for<'de> Deserialize<'de>,
    {
        self.delete(path, Some(body)).await?.to_json().await
    }
}