Skip to content

Commit 89859db

Browse files
authored
Merge pull request #95 from Sovereign-Engineering/account-create-api
Add an account creation API.
2 parents b72667a + 1bf6768 commit 89859db

11 files changed

Lines changed: 67 additions & 30 deletions

File tree

doc/account-creation.md

Lines changed: 0 additions & 16 deletions
This file was deleted.

doc/auth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Authentication is a simple process.
44

5-
1. [Create an account](crate::doc::account_creation).
5+
1. [Create an account](crate::cmd::CreateAccount).
66
2. [Exchange Account Number for an auth token](crate::token::AcquireToken).
77
3. [Make requests with the auth token.](crate::token::AcquireToken2Output::auth_token)
88

doc/payments.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This API is only officially supported for our partners. If you are interested in
1313
This documentation will be critical to understanding how to manage funding.
1414

1515
- [Account Number](crate::types::AccountId)
16+
- [Account Creation](crate::cmd::CreateAccount)
1617
- [Authentication](crate::doc::auth)
1718
- [Errors](crate::doc::error)
1819
- [Making Requests](crate::doc::requests)

examples/api_cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ async fn main() -> anyhow::Result<()> {
190190
}
191191
Commands::UseReferralCode { code } => {
192192
eprintln!("Use referral code: {}", &code);
193-
client.run(UseReferralCode { code }).await?;
193+
client.run(UseReferralCode { code: ReferralCode(code) }).await?;
194194
}
195195
Commands::RotateReferralCode => {
196196
eprintln!("Rotate referral code");

src/client.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Client {
125125
pub async fn acquire_auth_token(&self) -> Result<AcquireToken2Output, ClientError> {
126126
if let Some(auth_token) = self.get_auth_token() {
127127
return Ok(AcquireToken2Output {
128-
auth_token: auth_token.into(),
128+
auth_token,
129129
url_override: None,
130130
});
131131
}
@@ -134,7 +134,7 @@ impl Client {
134134

135135
if let Some(auth_token) = self.get_auth_token() {
136136
return Ok(AcquireToken2Output {
137-
auth_token: auth_token.into(),
137+
auth_token,
138138
url_override: None,
139139
});
140140
}
@@ -143,7 +143,7 @@ impl Client {
143143

144144
let res = self.request_token(&self.account_id).await?;
145145
let body = res.into_body().context("No auth token in response")?;
146-
self.set_auth_token(Some(body.auth_token.clone().into()));
146+
self.set_auth_token(Some(body.auth_token.clone()));
147147

148148
drop(acquiring_auth_token);
149149
Ok(body)
@@ -270,7 +270,7 @@ impl Client {
270270
.transpose()
271271
.map_err(|_| ClientError::InvalidHeaderValue)?;
272272
for _ in 0..3 {
273-
let auth_token = self.acquire_auth_token().await?.auth_token.into();
273+
let auth_token = self.acquire_auth_token().await?.auth_token;
274274
if let Some(output) = self.run_once::<C>(&cmd, &auth_token, etag.clone()).await? {
275275
return Ok(output);
276276
}

src/cmd/account/create.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::cmd::ReferralCode;
2+
use crate::types::{AccountId, AuthToken};
3+
use crate::{cmd::Cmd, pow::PowOutput};
4+
use serde::{Deserialize, Serialize};
5+
6+
#[derive(Debug, Serialize, Deserialize, Clone)]
7+
pub struct CreateAccountInfo {
8+
pub id: AccountId,
9+
pub token: AuthToken,
10+
}
11+
12+
/// Create an Account
13+
///
14+
/// ## Expected Errors
15+
/// - [`InvalidReferralCode`](crate::cmd::ApiErrorKind::InvalidReferralCode)
16+
/// - [`SignupLimitExceeded`](crate::cmd::ApiErrorKind::SignupLimitExceeded)
17+
#[derive(Debug, Serialize, Deserialize, Clone)]
18+
pub struct CreateAccount {
19+
pub pow: Option<PowOutput>,
20+
pub referral_code: Option<ReferralCode>,
21+
}
22+
23+
impl Cmd for CreateAccount {
24+
type Output = CreateAccountInfo;
25+
const METHOD: http::Method = http::Method::POST;
26+
const PATH: &'static str = "account";
27+
}
28+
29+
#[test]
30+
fn test_account_info_json() {
31+
crate::cmd::check_cmd_json::<CreateAccount>(
32+
Some(
33+
r###"
34+
{
35+
"pow": null,
36+
"referral_code": "1234"
37+
}
38+
"###,
39+
),
40+
Some(
41+
r###"
42+
{
43+
"id": "0000000000000000000",
44+
"token": "abc-123-xyz"
45+
}
46+
"###,
47+
),
48+
);
49+
}

src/cmd/account/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
mod create;
12
mod delete;
23
mod info;
34

5+
pub use create::*;
46
pub use delete::*;
57
pub use info::*;

src/cmd/referrals.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ use serde::{Deserialize, Serialize};
22

33
use super::Cmd;
44

5+
#[derive(Debug, Serialize, Deserialize, Clone)]
6+
pub struct ReferralCode(pub String);
7+
58
#[derive(Debug, Serialize, Deserialize, Clone)]
69
pub struct UseReferralCode {
7-
pub code: String,
10+
pub code: ReferralCode,
811
}
912

1013
impl Cmd for UseReferralCode {

src/doc.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![doc = include_str!("../doc/index.md")]
22

3-
#[doc = include_str!("../doc/account-creation.md")]
4-
pub mod account_creation {}
53
#[doc = include_str!("../doc/auth.md")]
64
pub mod auth {}
75
#[doc = include_str!("../doc/error.md")]

src/token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::pow::PowOutput;
21
use crate::types::AccountId;
2+
use crate::{pow::PowOutput, types::AuthToken};
33
use serde::{Deserialize, Serialize};
44
use url::Url;
55

@@ -17,7 +17,7 @@ pub struct AcquireToken2Output {
1717
/// The token is used by adding an `Authorization: Bearer {token}` header to your requests.
1818
///
1919
/// The token has no definite expiry date. It is recommended to cache tokens indefinitely, only acquiring a new one when the API returns a [`MissingOrInvalidAuthToken`](crate::cmd::ApiErrorKind::MissingOrInvalidAuthToken) error.
20-
pub auth_token: String,
20+
pub auth_token: AuthToken,
2121

2222
/// Internal use.
2323
pub url_override: Option<UrlOverride>,

0 commit comments

Comments
 (0)