Skip to content

Commit

Permalink
Merge branch 'simplify-rest-client'
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Oct 30, 2023
2 parents 1c24b27 + ecf3a20 commit df106cd
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 449 deletions.
14 changes: 8 additions & 6 deletions mullvad-api/src/access.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
rest,
rest::{RequestFactory, RequestServiceHandle},
API,
};
use futures::{
channel::{mpsc, oneshot},
Expand All @@ -13,7 +14,7 @@ use tokio::select;

pub const AUTH_URL_PREFIX: &str = "auth/v1";

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct AccessTokenStore {
tx: mpsc::UnboundedSender<StoreAction>,
}
Expand All @@ -36,7 +37,8 @@ struct AccountState {
}

impl AccessTokenStore {
pub(crate) fn new(service: RequestServiceHandle, factory: RequestFactory) -> Self {
pub(crate) fn new(service: RequestServiceHandle) -> Self {
let factory = rest::RequestFactory::new(&API.host, None);
let (tx, rx) = mpsc::unbounded();
tokio::spawn(Self::service_requests(rx, service, factory));
Self { tx }
Expand Down Expand Up @@ -174,8 +176,8 @@ async fn fetch_access_token(
account_number: account_token,
};

let rest_request = factory.post_json(&format!("{AUTH_URL_PREFIX}/token"), &request)?;
let response = service.request(rest_request).await?;
let response = rest::parse_rest_response(response, &[StatusCode::OK]).await?;
rest::deserialize_body(response).await
let rest_request = factory
.post_json(&format!("{AUTH_URL_PREFIX}/token"), &request)?
.expected_status(&[StatusCode::OK]);
service.request(rest_request).await?.deserialize().await
}
110 changes: 31 additions & 79 deletions mullvad-api/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::{DateTime, Utc};
use http::{Method, StatusCode};
use http::StatusCode;
use mullvad_types::{
account::AccountToken,
device::{Device, DeviceId, DeviceName},
Expand Down Expand Up @@ -51,25 +51,13 @@ impl DevicesProxy {

let service = self.handle.service.clone();
let factory = self.handle.factory.clone();
let access_proxy = self.handle.token_store.clone();

async move {
let access_token = access_proxy.get_token(&account).await?;

let response = rest::send_json_request(
&factory,
service,
&format!("{ACCOUNTS_URL_PREFIX}/devices"),
Method::POST,
&submission,
Some(access_token),
&[StatusCode::CREATED],
)
.await;

access_proxy.check_response(&account, &response);

let response: DeviceResponse = rest::deserialize_body(response?).await?;
let request = factory
.post_json(&format!("{ACCOUNTS_URL_PREFIX}/devices"), &submission)?
.account(account)?
.expected_status(&[StatusCode::CREATED]);
let response = service.request(request).await?;
let DeviceResponse {
id,
name,
Expand All @@ -79,7 +67,7 @@ impl DevicesProxy {
hijack_dns,
created,
..
} = response;
} = response.deserialize().await?;

Ok((
Device {
Expand All @@ -104,21 +92,12 @@ impl DevicesProxy {
) -> impl Future<Output = Result<Device, rest::Error>> {
let service = self.handle.service.clone();
let factory = self.handle.factory.clone();
let access_proxy = self.handle.token_store.clone();
async move {
let access_token = access_proxy.get_token(&account).await?;
let response = rest::send_request(
&factory,
service,
&format!("{ACCOUNTS_URL_PREFIX}/devices/{id}"),
Method::GET,
Some(access_token),
&[StatusCode::OK],
)
.await;
access_proxy.check_response(&account, &response);
let device = rest::deserialize_body(response?).await?;
Ok(device)
let request = factory
.get(&format!("{ACCOUNTS_URL_PREFIX}/devices/{id}"))?
.expected_status(&[StatusCode::OK])
.account(account)?;
service.request(request).await?.deserialize().await
}
}

Expand All @@ -128,21 +107,12 @@ impl DevicesProxy {
) -> impl Future<Output = Result<Vec<Device>, rest::Error>> {
let service = self.handle.service.clone();
let factory = self.handle.factory.clone();
let access_proxy = self.handle.token_store.clone();
async move {
let access_token = access_proxy.get_token(&account).await?;
let response = rest::send_request(
&factory,
service,
&format!("{ACCOUNTS_URL_PREFIX}/devices"),
Method::GET,
Some(access_token),
&[StatusCode::OK],
)
.await;
access_proxy.check_response(&account, &response);
let devices = rest::deserialize_body(response?).await?;
Ok(devices)
let request = factory
.get(&format!("{ACCOUNTS_URL_PREFIX}/device"))?
.expected_status(&[StatusCode::OK])
.account(account)?;
service.request(request).await?.deserialize().await
}
}

Expand All @@ -153,21 +123,12 @@ impl DevicesProxy {
) -> impl Future<Output = Result<(), rest::Error>> {
let service = self.handle.service.clone();
let factory = self.handle.factory.clone();
let access_proxy = self.handle.token_store.clone();
async move {
let access_token = access_proxy.get_token(&account).await?;
let response = rest::send_request(
&factory,
service,
&format!("{ACCOUNTS_URL_PREFIX}/devices/{id}"),
Method::DELETE,
Some(access_token),
&[StatusCode::NO_CONTENT],
)
.await;
access_proxy.check_response(&account, &response);

response?;
let request = factory
.delete(&format!("{ACCOUNTS_URL_PREFIX}/devices/{id}"))?
.expected_status(&[StatusCode::NO_CONTENT])
.account(account)?;
service.request(request).await?;
Ok(())
}
}
Expand All @@ -187,30 +148,21 @@ impl DevicesProxy {

let service = self.handle.service.clone();
let factory = self.handle.factory.clone();
let access_proxy = self.handle.token_store.clone();

async move {
let access_token = access_proxy.get_token(&account).await?;

let response = rest::send_json_request(
&factory,
service,
&format!("{ACCOUNTS_URL_PREFIX}/devices/{id}/pubkey"),
Method::PUT,
&req_body,
Some(access_token),
&[StatusCode::OK],
)
.await;

access_proxy.check_response(&account, &response);

let updated_device: DeviceResponse = rest::deserialize_body(response?).await?;
let request = factory
.put_json(
&format!("{ACCOUNTS_URL_PREFIX}/devices/{id}/pubkey"),
&req_body,
)?
.expected_status(&[StatusCode::OK])
.account(account)?;
let response = service.request(request).await?;
let DeviceResponse {
ipv4_address,
ipv6_address,
..
} = updated_device;
} = response.deserialize().await?;
Ok(mullvad_types::wireguard::AssociatedAddresses {
ipv4_address,
ipv6_address,
Expand Down
Loading

0 comments on commit df106cd

Please sign in to comment.