summaryrefslogtreecommitdiffstats
path: root/rust/fatcat-cli/src/api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/fatcat-cli/src/api.rs')
-rw-r--r--rust/fatcat-cli/src/api.rs66
1 files changed, 8 insertions, 58 deletions
diff --git a/rust/fatcat-cli/src/api.rs b/rust/fatcat-cli/src/api.rs
index 2db2efb..cc6fa6a 100644
--- a/rust/fatcat-cli/src/api.rs
+++ b/rust/fatcat-cli/src/api.rs
@@ -1,4 +1,4 @@
-use crate::{parse_macaroon_editor_id, ClientStatus, EntityType, Specifier};
+use crate::{parse_macaroon_editor_id, EntityType, Specifier};
use anyhow::{anyhow, Context, Result};
use fatcat_openapi::models;
use fatcat_openapi::{ApiNoContext, ContextWrapperExt};
@@ -14,16 +14,13 @@ type FatcatApiContextType = swagger::make_context_ty!(
pub struct FatcatApiClient {
pub api: Box<dyn ApiNoContext<FatcatApiContextType>>,
pub rt: tokio::runtime::Runtime,
- api_token: Option<String>,
- api_host: String,
+ pub api_token: Option<String>,
+ pub api_host: String,
pub editor_id: Option<String>,
}
impl FatcatApiClient {
- pub fn new(
- api_host: String,
- api_token: Option<String>,
- ) -> Result<Self> {
+ pub fn new(api_host: String, api_token: Option<String>) -> Result<Self> {
let auth_data = match api_token {
Some(ref token) => Some(AuthData::Bearer(auth::Bearer {
token: token.clone(),
@@ -39,9 +36,11 @@ impl FatcatApiClient {
);
//let wrapped_client: swagger::ContextWrapper<
- let client = fatcat_openapi::client::Client::try_new(&api_host).context("failed to create HTTP(S) client")?;
+ let client = fatcat_openapi::client::Client::try_new(&api_host)
+ .context("failed to create HTTP(S) client")?;
let wrapped_client = Box::new(client.with_context(context));
- let rt: tokio::runtime::Runtime = tokio::runtime::Runtime::new().expect("create tokio runtime");
+ let rt: tokio::runtime::Runtime =
+ tokio::runtime::Runtime::new().expect("create tokio runtime");
let editor_id = match api_token {
Some(ref token) => {
@@ -59,55 +58,6 @@ impl FatcatApiClient {
})
}
- pub fn status(&mut self) -> Result<ClientStatus> {
- let last_changelog = match self.rt.block_on(self.api.get_changelog(Some(1))) {
- Ok(fatcat_openapi::GetChangelogResponse::Success(entry_vec)) => {
- Some(entry_vec[0].index)
- }
- Ok(_) | Err(_) => None,
- };
- let has_api_token = self.api_token.is_some();
- let account: Option<models::Editor> = if has_api_token && last_changelog.is_some() {
- match self
- .rt
- .block_on(self.api.auth_check(None))
- .context("check auth token")?
- {
- fatcat_openapi::AuthCheckResponse::Success(_) => Ok(()),
- fatcat_openapi::AuthCheckResponse::Forbidden(err) => {
- Err(anyhow!("Forbidden ({}): {}", err.error, err.message))
- }
- fatcat_openapi::AuthCheckResponse::NotAuthorized { body: err, .. } => {
- Err(anyhow!("Bad Request ({}): {}", err.error, err.message))
- }
- resp => return Err(anyhow!("{:?}", resp)).context("auth check failed"),
- }
- .context("check auth token")?;
- match self
- .rt
- .block_on(
- self.api
- .get_editor(self.editor_id.as_ref().unwrap().to_string()),
- )
- .context("fetching editor account info")?
- {
- fatcat_openapi::GetEditorResponse::Found(editor) => Some(editor),
- fatcat_openapi::GetEditorResponse::NotFound(err) => {
- return Err(anyhow!("Not Found: {}", err.message))
- }
- resp => return Err(anyhow!("{:?}", resp)).context("editor fetch failed"),
- }
- } else {
- None
- };
- Ok(ClientStatus {
- api_host: self.api_host.clone(),
- has_api_token,
- last_changelog,
- account,
- })
- }
-
pub fn update_editgroup_submit(
&mut self,
editgroup_id: String,