Skip to content

Commit

Permalink
Eve URLs are configurables in the EsiBuilder (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Ory <olivier.ory@uliege.be>
  • Loading branch information
normegil and Olivier Ory committed Jan 31, 2024
1 parent de5a845 commit b5037cc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 17 deletions.
54 changes: 53 additions & 1 deletion src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ pub struct EsiBuilder {
pub(crate) client_secret: Option<String>,
pub(crate) application_auth: Option<bool>,
pub(crate) callback_url: Option<String>,
pub(crate) base_api_url: Option<String>,
pub(crate) authorize_url: Option<String>,
pub(crate) token_url: Option<String>,
pub(crate) spec_url: Option<String>,
pub(crate) scope: Option<String>,
pub(crate) access_token: Option<String>,
pub(crate) access_expiration: Option<i64>,
Expand Down Expand Up @@ -118,6 +122,30 @@ impl EsiBuilder {
self
}

/// Set the base_api_url.
pub fn base_api_url(mut self, val: &str) -> Self {
self.base_api_url = Some(val.to_owned());
self
}

/// Set the authorize_url.
pub fn authorize_url(mut self, val: &str) -> Self {
self.authorize_url = Some(val.to_owned());
self
}

/// Set the token_url.
pub fn token_url(mut self, val: &str) -> Self {
self.token_url = Some(val.to_owned());
self
}

/// Set the spec_url.
pub fn spec_url(mut self, val: &str) -> Self {
self.spec_url = Some(val.to_owned());
self
}

/// Set the scope.
pub fn scope(mut self, val: &str) -> Self {
self.scope = Some(val.to_owned().replace(' ', "%20"));
Expand Down Expand Up @@ -237,11 +265,35 @@ mod tests {
assert_eq!(b.client_id, None);
assert_eq!(b.client_secret, None);
assert_eq!(b.callback_url, None);
assert_eq!(b.base_api_url, "https://esi.evetech.net/");
assert_eq!(
b.authorize_url,
"https://login.eveonline.com/v2/oauth/authorize"
);
assert_eq!(b.token_url, "https://login.eveonline.com/v2/oauth/token");
assert_eq!(b.spec_url, "https://esi.evetech.net/_latest/swagger.json");
assert_eq!(b.version, "latest");
assert_eq!(b.access_token, None);
assert_eq!(b.spec, None);
}

#[test]
fn test_builder_change_urls() {
let b = EsiBuilder::new()
.user_agent("d")
.base_api_url("http://eve-api/")
.authorize_url("http://authorize-url/")
.token_url("http://token-url")
.spec_url("http://spec-url/")
.build()
.unwrap();

assert_eq!(b.base_api_url, "http://eve-api/");
assert_eq!(b.authorize_url, "http://authorize-url/");
assert_eq!(b.token_url, "http://token-url");
assert_eq!(b.spec_url, "http://spec-url/");
}

#[test]
fn test_builder_missing_value() {
let res = EsiBuilder::new().build();
Expand Down Expand Up @@ -274,7 +326,7 @@ mod tests {

#[test]
fn test_builder_to_json_empty() {
let json = r#"{"version":null,"client_id":null,"client_secret":null,"application_auth":null,"callback_url":null,"scope":null,"access_token":null,"access_expiration":null,"refresh_token":null,"user_agent":null,"http_timeout":null,"spec":null}"#;
let json = r#"{"version":null,"client_id":null,"client_secret":null,"application_auth":null,"callback_url":null,"base_api_url":null,"authorize_url":null,"token_url":null,"spec_url":null,"scope":null,"access_token":null,"access_expiration":null,"refresh_token":null,"user_agent":null,"http_timeout":null,"spec":null}"#;
assert_eq!(json, serde_json::to_string(&EsiBuilder::new()).unwrap());
}

Expand Down
29 changes: 16 additions & 13 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ pub struct Esi {
pub(crate) client_id: Option<String>,
pub(crate) client_secret: Option<String>,
pub(crate) callback_url: Option<String>,
pub(crate) base_api_url: String,
pub(crate) authorize_url: String,
pub(crate) token_url: String,
pub(crate) spec_url: String,
pub(crate) scope: String,
pub(crate) application_auth: bool,
/// The access token from ESI, if set.
Expand All @@ -107,10 +111,16 @@ impl Esi {
let client = builder.construct_client()?;
let version = builder.version.unwrap_or_else(|| "latest".to_owned());
let e = Esi {
version,
version: version.clone(),
client_id: builder.client_id,
client_secret: builder.client_secret,
callback_url: builder.callback_url,
base_api_url: builder.base_api_url.unwrap_or(BASE_URL.to_string()),
authorize_url: builder.authorize_url.unwrap_or(AUTHORIZE_URL.to_string()),
token_url: builder.token_url.unwrap_or(TOKEN_URL.to_string()),
spec_url: builder
.spec_url
.unwrap_or(format!("{}{}{}", SPEC_URL_START, version, SPEC_URL_END)),
scope: builder.scope.unwrap_or_else(|| "".to_owned()),
application_auth: builder.application_auth.unwrap_or(false),
access_token: builder.access_token,
Expand Down Expand Up @@ -146,14 +156,7 @@ impl Esi {
/// # }
pub async fn update_spec(&mut self) -> EsiResult<()> {
debug!("Updating spec with version {}", self.version);
let resp = self
.client
.get(&format!(
"{}{}{}",
SPEC_URL_START, self.version, SPEC_URL_END
))
.send()
.await?;
let resp = self.client.get(&self.spec_url).send().await?;
if !resp.status().is_success() {
error!("Got status {} when requesting spec", resp.status());
return Err(EsiError::InvalidStatusCode(resp.status().as_u16()));
Expand Down Expand Up @@ -223,7 +226,7 @@ impl Esi {
let state = "rfesi_unused".to_string();
let mut url = format!(
"{}?response_type=code&redirect_uri={}&client_id={}&scope={}&state={}",
AUTHORIZE_URL,
self.authorize_url,
self.callback_url.as_ref().unwrap(),
self.client_id.as_ref().unwrap(),
self.scope,
Expand Down Expand Up @@ -324,7 +327,7 @@ impl Esi {

let resp = self
.client
.post(TOKEN_URL)
.post(&self.token_url)
.headers(self.get_auth_headers()?)
.form(&body)
.send()
Expand Down Expand Up @@ -430,7 +433,7 @@ impl Esi {
}
let resp = self
.client
.post(TOKEN_URL)
.post(&self.token_url)
.headers(self.get_auth_headers()?)
.form(&body)
.send()
Expand Down Expand Up @@ -516,7 +519,7 @@ impl Esi {
}
map
};
let url = format!("{BASE_URL}{endpoint}");
let url = format!("{}{endpoint}", self.base_api_url);
let mut req_builder = self
.client
.request(Method::from_str(method)?, &url)
Expand Down
4 changes: 1 addition & 3 deletions src/groups/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub struct WalletGroup<'a> {
}

impl<'a> WalletGroup<'a> {

api_get!(
/// Returns a character’s wallet balance
get_wallet,
Expand All @@ -17,5 +16,4 @@ impl<'a> WalletGroup<'a> {
f64,
(character_id: i32) => "{character_id}"
);

}
}

0 comments on commit b5037cc

Please sign in to comment.