Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod authorize; | ||
mod credential; | ||
mod dequeue; | ||
mod domain; | ||
mod enqueue; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |