Skip to content

Commit

Permalink
Implement initial seed spreadsheet models in kolomoni_seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
simongoricar committed Dec 1, 2024
1 parent 182cdf5 commit b16a3d6
Show file tree
Hide file tree
Showing 25 changed files with 2,522 additions and 19 deletions.
33 changes: 31 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,19 @@ uuid = { version = "^1.8.0", features = ["v7", "serde"] }
httpdate = "^1.0.3"
bytes = "^1.7.1"
dotenvy = "^0.15.7"
csv = "1.3.0"

reqwest = "^0.12.7"
tantivy = "^0.22.0"
slotmap = "^1.0.7"

fastrand = "^2.2.0"

pathdiff = "^0.2.1"
path-slash = "^0.2.1"

parking_lot = "^0.12.3"

fs-more = "^0.7.1"

futures-core = "^0.3.30"
Expand Down
18 changes: 14 additions & 4 deletions kolomoni_api_client/src/api/dictionary/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ use kolomoni_core::{
};
use reqwest::StatusCode;
use thiserror::Error;
use uuid::Uuid;

use crate::{
errors::{ClientError, ClientResult},
macros::{
handle_error_reasons_or_catch_unexpected_status,
handle_internal_server_error,
handle_unexpected_error_reason,
handle_unexpected_status_code,
handlers,
handle_unexpected_error_reason,
},
request::RequestBuilder,
AuthenticatedClient,
Expand All @@ -42,7 +41,7 @@ pub struct CategoryToCreate {


pub struct CategoryFieldsToUpdate {
pub new_parent_category_id: Option<Option<Uuid>>,
pub new_parent_category_id: Option<Option<CategoryId>>,

pub new_slovene_name: Option<String>,

Expand Down Expand Up @@ -197,11 +196,14 @@ async fn update_category(
return Err(CategoryUpdatingError::NoFieldsToUpdate);
}

let new_parent_category_id = category_fields_to_update
.new_parent_category_id
.map(|outer| outer.map(|inner| inner.into_uuid()));

let response = RequestBuilder::patch(client)
.endpoint_url(format!("/dictionary/category/{}", category_id))
.json(&CategoryUpdateRequest {
new_parent_category_id: category_fields_to_update.new_parent_category_id,
new_parent_category_id,
new_english_name: category_fields_to_update.new_english_name,
new_slovene_name: category_fields_to_update.new_slovene_name,
})
Expand Down Expand Up @@ -328,6 +330,10 @@ pub struct DictionaryCategoriesApi<'c> {


impl<'c> DictionaryCategoriesApi<'c> {
pub(crate) const fn new(client: &'c Client) -> Self {
Self { client }
}

pub async fn get_categories(&self) -> ClientResult<Vec<Category>, ClientError> {
get_categories(self.client).await
}
Expand All @@ -347,6 +353,10 @@ pub struct DictionaryCategoriesAuthenticatedApi<'c> {
}

impl<'c> DictionaryCategoriesAuthenticatedApi<'c> {
pub(crate) const fn new(client: &'c AuthenticatedClient) -> Self {
Self { client }
}

pub async fn get_categories(&self) -> ClientResult<Vec<Category>, ClientError> {
get_categories(self.client).await
}
Expand Down
8 changes: 8 additions & 0 deletions kolomoni_api_client/src/api/dictionary/english.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,10 @@ pub struct EnglishDictionaryApi<'c> {
}

impl<'c> EnglishDictionaryApi<'c> {
pub(crate) const fn new(client: &'c Client) -> Self {
Self { client }
}

/*
* Word-related (word meanings are in the next section)
*/
Expand Down Expand Up @@ -791,6 +795,10 @@ pub struct EnglishDictionaryAuthenticatedApi<'c> {
}

impl<'c> EnglishDictionaryAuthenticatedApi<'c> {
pub(crate) const fn new(client: &'c AuthenticatedClient) -> Self {
Self { client }
}

/*
* Word-related (word meanings are in the next section)
*/
Expand Down
8 changes: 8 additions & 0 deletions kolomoni_api_client/src/api/dictionary/slovene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,10 @@ pub struct SloveneDictionaryApi<'c> {
}

impl<'c> SloveneDictionaryApi<'c> {
pub(crate) const fn new(client: &'c Client) -> Self {
Self { client }
}

/*
* Word-related (word meanings are in the next section)
*/
Expand Down Expand Up @@ -752,6 +756,10 @@ pub struct SloveneDictionaryAuthenticatedApi<'c> {
}

impl<'c> SloveneDictionaryAuthenticatedApi<'c> {
pub(crate) const fn new(client: &'c AuthenticatedClient) -> Self {
Self { client }
}

/*
* Word-related (word meanings are in the next section)
*/
Expand Down
48 changes: 41 additions & 7 deletions kolomoni_api_client/src/api/health.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
use kolomoni_core::api_models::PingResponse;

use crate::{errors::ClientResult, request::RequestBuilder, Client};
use crate::{
errors::ClientResult,
request::RequestBuilder,
AuthenticatedClient,
Client,
HttpClient,
};


async fn ping<C>(client: &C) -> ClientResult<bool>
where
C: HttpClient,
{
let response = RequestBuilder::get(client)
.endpoint_url("/health/ping")
.send()
.await?;

let response_body: PingResponse = response.json().await?;

Ok(response_body.ok)
}


pub struct HealthApi<'c> {
client: &'c Client,
}

impl<'c> HealthApi<'c> {
pub(crate) const fn new(client: &'c Client) -> Self {
Self { client }
}

pub async fn ping(&self) -> ClientResult<bool> {
let response = RequestBuilder::get(self.client)
.endpoint_url("/health/ping")
.send()
.await?;
ping(self.client).await
}
}

let response_body: PingResponse = response.json().await?;

Ok(response_body.ok)
pub struct HealthAuthenticatedApi<'c> {
client: &'c AuthenticatedClient,
}

impl<'c> HealthAuthenticatedApi<'c> {
pub(crate) const fn new(client: &'c AuthenticatedClient) -> Self {
Self { client }
}

pub async fn ping(&self) -> ClientResult<bool> {
ping(self.client).await
}
}
4 changes: 4 additions & 0 deletions kolomoni_api_client/src/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub struct AccessToken {
}

impl AccessToken {
pub fn new(access_token: String) -> Self {
Self { access_token }
}

pub async fn log_in<U, P>(
client: &Client,
username: U,
Expand Down
58 changes: 54 additions & 4 deletions kolomoni_api_client/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ use reqwest::Body;
use url::Url;

use crate::{
api::{
dictionary::{
categories::{DictionaryCategoriesApi, DictionaryCategoriesAuthenticatedApi},
english::{EnglishDictionaryApi, EnglishDictionaryAuthenticatedApi},
slovene::{SloveneDictionaryApi, SloveneDictionaryAuthenticatedApi},
},
health::{HealthApi, HealthAuthenticatedApi},
},
authentication::AccessToken,
errors::{ClientError, ClientInitializationError, ClientResult},
response::ServerResponse,
Expand Down Expand Up @@ -41,15 +49,22 @@ pub struct Client {
}

impl Client {
pub fn new(server: Rc<ApiServer>) -> Result<Self, ClientInitializationError> {
let http_client = reqwest::Client::builder()
pub fn new(server: &Rc<ApiServer>) -> Result<Self, ClientInitializationError> {
let http_client_partial = reqwest::Client::builder()
.zstd(true)
.user_agent(build_client_user_agent())
.user_agent(build_client_user_agent());

let http_client_partial = match server.is_https() {
true => http_client_partial.https_only(true),
false => http_client_partial,
};

let http_client = http_client_partial
.build()
.map_err(|error| ClientInitializationError::UnableToInitializeReqwestClient { error })?;

Ok(Self {
server,
server: server.clone(),
http_client,
})
}
Expand All @@ -63,6 +78,24 @@ impl Client {
}
}

impl Client {
pub fn health(&self) -> HealthApi<'_> {
HealthApi::new(self)
}

pub fn categories(&self) -> DictionaryCategoriesApi<'_> {
DictionaryCategoriesApi::new(self)
}

pub fn english_dictionary(&self) -> EnglishDictionaryApi<'_> {
EnglishDictionaryApi::new(self)
}

pub fn slovene_dictionary(&self) -> SloveneDictionaryApi<'_> {
SloveneDictionaryApi::new(self)
}
}

impl HttpClient for Client {
fn server(&self) -> &ApiServer {
&self.server
Expand Down Expand Up @@ -142,6 +175,23 @@ impl AuthenticatedClient {
}
}

impl AuthenticatedClient {
pub fn health(&self) -> HealthAuthenticatedApi<'_> {
HealthAuthenticatedApi::new(self)
}

pub fn categories(&self) -> DictionaryCategoriesAuthenticatedApi<'_> {
DictionaryCategoriesAuthenticatedApi::new(self)
}

pub fn english_dictionary(&self) -> EnglishDictionaryAuthenticatedApi<'_> {
EnglishDictionaryAuthenticatedApi::new(self)
}

pub fn slovene_dictionary(&self) -> SloveneDictionaryAuthenticatedApi<'_> {
SloveneDictionaryAuthenticatedApi::new(self)
}
}

impl HttpClient for AuthenticatedClient {
fn server(&self) -> &ApiServer {
Expand Down
4 changes: 4 additions & 0 deletions kolomoni_api_client/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ impl ApiServer {
pub(crate) fn base_url(&self) -> &str {
&self.base_api_url
}

pub(crate) fn is_https(&self) -> bool {
self.base_api_url.starts_with("https")
}
}
13 changes: 13 additions & 0 deletions kolomoni_seeder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ version = "0.1.0"
edition = "2021"

[dependencies]
kolomoni_core = { path = "../kolomoni_core" }
kolomoni_api_client = { path = "../kolomoni_api_client" }

tokio = { workspace = true }

parking_lot = { workspace = true, features = ["arc_lock"] }

serde = { workspace = true }
csv = { workspace = true }

clap = { workspace = true }

miette = { workspace = true, features = ["fancy"] }
thiserror = { workspace = true }

fastrand = { workspace = true }
Loading

0 comments on commit b16a3d6

Please sign in to comment.