Skip to content

Commit

Permalink
twiq-light: Add CredentialStore
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Dec 28, 2022
1 parent 31a5e72 commit 223b1d6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
13 changes: 13 additions & 0 deletions twiq-light/src/credential.rs
@@ -0,0 +1,13 @@
use crate::token::Token;

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct TwitterClientKey {
pub id: String,
pub secret: String,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Credential {
pub client: TwitterClientKey,
pub token: Token,
}
1 change: 1 addition & 0 deletions twiq-light/src/main.rs
@@ -1,4 +1,5 @@
mod authorize;
mod credential;
mod dequeue;
mod domain;
mod enqueue;
Expand Down
2 changes: 2 additions & 0 deletions twiq-light/src/store.rs
@@ -1,5 +1,7 @@
pub mod credential;
pub mod tweet;
pub mod tweet_queue;

pub use credential::CredentialStore;
pub use tweet::TweetStore;
pub use tweet_queue::TweetQueueStore;
45 changes: 45 additions & 0 deletions twiq-light/src/store/credential.rs
@@ -0,0 +1,45 @@
use crate::{
credential::Credential,
storage::{firestore::FirestoreStorage, Storage},
};

pub struct CredentialStore {
storage: FirestoreStorage,
}

impl CredentialStore {
const DATABASE_ID: &str = "(default)";
const COLLECTION_ID: &str = "twiq-light";
const DOCUMENT_ID: &str = "credential";

pub async fn new(
project_id: String,
google_application_credentials: Option<String>,
) -> anyhow::Result<Self> {
let storage = FirestoreStorage::new(
google_application_credentials,
project_id,
Self::DATABASE_ID.to_owned(),
Self::COLLECTION_ID.to_owned(),
)
.await?;
Ok(Self { storage })
}

pub async fn read(&self) -> anyhow::Result<Option<Credential>> {
self.storage
.get_item(Self::DOCUMENT_ID.to_owned())
.await?
.map(|s| Ok(serde_json::from_str::<'_, Credential>(s.as_str())?))
.transpose()
}

pub async fn write(&self, credential: &Credential) -> anyhow::Result<()> {
self.storage
.set_item(
Self::DOCUMENT_ID.to_owned(),
serde_json::to_string(&credential)?,
)
.await
}
}

0 comments on commit 223b1d6

Please sign in to comment.