Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Login Panic and Add Clone Derives #12

Merged
merged 12 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use wikijs::{Api, Credentials};
let api = Api::new(
"http://localhost:3000".to_string(),
Credentials::Key("my-api-key".to_string()),
);
).unwrap();
println!("{:?}", api.page_get(1).unwrap());

```
Expand Down
5 changes: 4 additions & 1 deletion src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ enum Command {
fn main() {
let cli = Cli::parse();
let credentials = Credentials::Key(cli.key.clone());
let api = Api::new(cli.url.clone(), credentials);
let api = Api::new(cli.url.clone(), credentials).unwrap_or_else(|e| {
eprintln!("{}: {}", "error".bold().red(), e);
std::process::exit(1);
});

// TODO each command should be in its own module
// TODO each subcommand should implement an Execute trait to call here
Expand Down
5 changes: 4 additions & 1 deletion src/fuse/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,10 @@ fn main() {
}

let credentials = Credentials::Key(cli.key);
let api = Api::new(cli.url, credentials);
let api = Api::new(cli.url, credentials).unwrap_or_else(|error| {
error!("{}", error);
exit(1);
});
let fs = Fs::new(api, cli.locale);

mount2(fs, &cli.mountpoint, &[FSName("wikijs-fuse".to_string())])
Expand Down
6 changes: 3 additions & 3 deletions src/lib/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::common::{
UnknownError,
};

#[derive(Debug, Error, PartialEq)]
#[derive(Clone, Debug, Error, PartialEq)]
pub enum AnalyticsError {
#[error("Unknown response error code: {code}: {message}")]
UnknownErrorCode { code: i64, message: String },
Expand Down Expand Up @@ -50,7 +50,7 @@ impl KnownErrorCodes for AnalyticsError {
}
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct AnalyticsProvider {
#[serde(rename = "isEnabled")]
pub is_enabled: Boolean,
Expand All @@ -65,7 +65,7 @@ pub struct AnalyticsProvider {
pub config: Option<Vec<Option<KeyValuePair>>>,
}

#[derive(Serialize, Debug)]
#[derive(Clone, Serialize, Debug)]
pub struct AnalyticsProviderInput {
#[serde(rename = "isEnabled")]
pub is_enabled: Boolean,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::common::{
KnownErrorCodes, ResponseStatus, UnknownError,
};

#[derive(Error, Debug, PartialEq)]
#[derive(Clone, Error, Debug, PartialEq)]
pub enum AssetError {
#[error("An unexpected error occurred during asset operation.")]
AssetGenericError,
Expand Down Expand Up @@ -82,7 +82,7 @@ impl KnownErrorCodes for AssetError {
}
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct AssetItem {
pub id: Int,
pub filename: String,
Expand All @@ -100,14 +100,14 @@ pub struct AssetItem {
pub author: Option<Int>,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct AssetFolder {
pub id: Int,
pub slug: String,
pub name: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum AssetKind {
IMAGE,
BINARY,
Expand Down
28 changes: 20 additions & 8 deletions src/lib/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::common::{
};
use crate::user::UserError;

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct AuthenticationLoginResponse {
#[serde(rename = "responseResult")]
pub response_result: Option<ResponseStatus>,
Expand All @@ -26,7 +26,7 @@ pub struct AuthenticationLoginResponse {
pub tfa_qr_image: Option<String>,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct ApiKey {
pub id: Int,
pub name: String,
Expand All @@ -41,7 +41,7 @@ pub struct ApiKey {
pub is_revoked: Boolean,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct AuthenticationStrategy {
pub key: String,
pub props: Option<Vec<Option<KeyValuePair>>>,
Expand All @@ -59,7 +59,7 @@ pub struct AuthenticationStrategy {
pub icon: Option<String>,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct AuthenticationActiveStrategy {
pub key: String,
pub strategy: AuthenticationStrategy,
Expand All @@ -77,21 +77,21 @@ pub struct AuthenticationActiveStrategy {
pub auto_enroll_groups: Vec<Option<Int>>,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct AuthenticationCreateApiKeyResponse {
#[serde(rename = "responseResult")]
pub response_result: Option<ResponseStatus>,
pub key: Option<String>,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct AuthenticationRegisterResponse {
#[serde(rename = "responseResult")]
pub response_result: Option<ResponseStatus>,
pub jwt: Option<String>,
}

#[derive(Serialize, Debug)]
#[derive(Clone, Serialize, Debug)]
pub struct AuthenticationStrategyInput {
pub key: String,
#[serde(rename = "strategyKey")]
Expand Down Expand Up @@ -174,7 +174,19 @@ pub fn login(
if let Some(data) = response_body.data {
if let Some(authentication) = data.authentication {
if let Some(login) = authentication.login {
return Ok(login);
// TODO we need similar error handling
// for other functions here, too
// TODO is this clone necessary?
let login_copy = login.clone();
if let Some(response_result) = login.response_result {
if response_result.succeeded {
return Ok(login_copy);
} else {
return Err(classify_response_status_error(
response_result,
));
}
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::common::{
UnknownError,
};

#[derive(Debug, Error, PartialEq)]
#[derive(Clone, Debug, Error, PartialEq)]
pub enum CommentError {
#[error("An unexpected error occurred.")]
CommentGenericError,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl KnownErrorCodes for CommentError {
}
}

#[derive(Deserialize)]
#[derive(Clone, Deserialize)]
pub struct Comment {
pub id: Int,
pub content: String,
Expand All @@ -89,7 +89,7 @@ pub struct Comment {
pub updated_at: Date,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct CommentProvider {
#[serde(rename = "isEnabled")]
pub is_enabled: Boolean,
Expand All @@ -103,22 +103,22 @@ pub struct CommentProvider {
pub config: Option<Vec<Option<KeyValuePair>>>,
}

#[derive(Serialize, Debug)]
#[derive(Clone, Serialize, Debug)]
pub struct CommentProviderInput {
#[serde(rename = "isEnabled")]
pub is_enabled: Boolean,
pub key: String,
pub config: Option<Vec<Option<KeyValuePairInput>>>,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct CommentCreateResponse {
#[serde(rename = "responseResult")]
pub response_result: Option<ResponseStatus>,
pub id: Option<Int>,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct CommentUpdateResponse {
#[serde(rename = "responseResult")]
pub response_result: Option<ResponseStatus>,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ pub type Boolean = bool;
pub type Int = i64;
pub type Date = String;

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct KeyValuePair {
pub key: String,
pub value: String,
}

#[derive(Serialize, Debug)]
#[derive(Clone, Serialize, Debug)]
pub struct KeyValuePairInput {
pub key: String,
pub value: String,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
#[allow(unused)]
pub struct ResponseStatus {
pub succeeded: Boolean,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/contribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use thiserror::Error;

use crate::common::{classify_response_error, Date, UnknownError};

#[derive(Error, Debug, PartialEq)]
#[derive(Clone, Error, Debug, PartialEq)]
pub enum ContributeError {
#[error("Unknown response error code: {code}: {message}")]
UnknownErrorCode { code: i64, message: String },
Expand Down Expand Up @@ -36,7 +36,7 @@ impl UnknownError for ContributeError {
}
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct Contributor {
pub id: String,
pub source: String,
Expand Down
14 changes: 7 additions & 7 deletions src/lib/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::common::{
};
use crate::user::UserMinimal;

#[derive(Error, Debug, PartialEq)]
#[derive(Clone, Error, Debug, PartialEq)]
pub enum GroupError {
#[error("Unknown response error code: {code}: {message}")]
UnknownErrorCode { code: i64, message: String },
Expand Down Expand Up @@ -50,14 +50,14 @@ impl KnownErrorCodes for GroupError {
}
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct GroupResponse {
#[serde(rename = "responseResult")]
pub response_result: ResponseStatus,
pub group: Option<Group>,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct GroupMinimal {
pub id: Int,
pub name: String,
Expand All @@ -71,7 +71,7 @@ pub struct GroupMinimal {
pub updated_at: Date,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct Group {
pub id: Int,
pub name: String,
Expand All @@ -88,7 +88,7 @@ pub struct Group {
pub updated_at: Date,
}

#[derive(Deserialize, Debug)]
#[derive(Clone, Deserialize, Debug)]
pub struct PageRule {
pub id: String,
pub deny: Boolean,
Expand All @@ -98,7 +98,7 @@ pub struct PageRule {
pub locales: Vec<String>,
}

#[derive(Deserialize, Serialize, Debug)]
#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct PageRuleInput {
pub id: String,
pub deny: Boolean,
Expand All @@ -108,7 +108,7 @@ pub struct PageRuleInput {
pub locales: Vec<String>,
}

#[derive(Deserialize, Serialize, Debug)]
#[derive(Clone, Deserialize, Serialize, Debug)]
pub enum PageRuleMatch {
START,
EXACT,
Expand Down
17 changes: 10 additions & 7 deletions src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! let api = Api::new(
//! "http://localhost:3000".to_string(),
//! Credentials::Key("my-api-key".to_string()),
//! );
//! ).unwrap();
//! // this returns a page::Page
//! let page = api.page_get(1).unwrap();
//! println!("{:?}", page);
Expand Down Expand Up @@ -128,7 +128,10 @@ impl Api {
///
/// # Returns
/// A new API struct.
pub fn new(url: String, credentials: Credentials) -> Self {
pub fn new(
url: String,
credentials: Credentials,
) -> Result<Self, user::UserError> {
let key = match credentials {
Credentials::Key(key) => key,
Credentials::UsernamePassword(username, password, strategy) => {
Expand All @@ -142,12 +145,12 @@ impl Api {
username,
password,
strategy,
)
.unwrap();
)?;
println!("{:?}", auth_response);
auth_response.jwt.unwrap()
}
};
Self {
Ok(Self {
url,
client: Client::builder()
.user_agent("wikijs-rs/0.1.0")
Expand All @@ -161,7 +164,7 @@ impl Api {
)
.build()
.unwrap(),
}
})
}

// asset functions
Expand Down Expand Up @@ -302,7 +305,7 @@ impl Api {
/// let api = Api::new(
/// "http://localhost:3000".to_string(),
/// Credentials::Key("my-api-key".to_string()),
/// );
/// ).unwrap();
/// println!("{:?}", api.page_get(1).unwrap());
/// ```
pub fn page_get(&self, id: i64) -> Result<page::Page, page::PageError> {
Expand Down
Loading
Loading