Skip to content

Commit

Permalink
twiq-light: Fix to use credential_store
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Dec 30, 2022
1 parent e1c35a8 commit 80dd509
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
16 changes: 8 additions & 8 deletions twiq-light/src/command/dequeue.rs
@@ -1,9 +1,9 @@
use anyhow::bail;
use anyhow::{bail, Context};
use time::{format_description::well_known::Rfc3339, Duration, OffsetDateTime};
use tracing::{debug, instrument};

use crate::{
store::TweetQueueStore,
store::{CredentialStore, TweetQueueStore},
token::Token,
twitter::{self, PostTweetsRequestBody},
};
Expand Down Expand Up @@ -44,12 +44,12 @@ async fn ensure_token(
}

#[instrument(skip_all)]
pub async fn run(
store: TweetQueueStore,
client_id: String,
client_secret: String,
) -> anyhow::Result<()> {
let token = ensure_token(&store, &client_id, &client_secret).await?;
pub async fn run(store: TweetQueueStore, credential_store: CredentialStore) -> anyhow::Result<()> {
let credential = credential_store
.read()
.await?
.context("Use `twiq-light queue authorize`")?;
let token = ensure_token(&store, &credential.client.id, &credential.client.secret).await?;
debug!("{:?}", token);
let mut queue = store.read_all().await?;
if let Some(item) = queue.pop_front() {
Expand Down
18 changes: 13 additions & 5 deletions twiq-light/src/command/fetch.rs
@@ -1,10 +1,10 @@
use anyhow::bail;
use anyhow::{bail, Context};
use time::{format_description::well_known::Rfc3339, Duration, OffsetDateTime};
use tracing::{debug, instrument};

use crate::{
domain::MyTweet,
store::{TweetQueueStore, TweetStore},
store::{CredentialStore, TweetQueueStore, TweetStore},
token::Token,
twitter::{
self, GetUsersIdTweetsPathParams, GetUsersIdTweetsQueryParams, TweetResponseDataItem,
Expand Down Expand Up @@ -51,10 +51,18 @@ async fn ensure_token(
pub async fn run(
store: TweetStore,
tweet_queue_store: TweetQueueStore,
client_id: String,
client_secret: String,
credential_store: CredentialStore,
) -> anyhow::Result<()> {
let token = ensure_token(&tweet_queue_store, &client_id, &client_secret).await?;
let credential = credential_store
.read()
.await?
.context("Use `twiq-light queue authorize`")?;
let token = ensure_token(
&tweet_queue_store,
&credential.client.id,
&credential.client.secret,
)
.await?;
debug!("{:?}", token);
let mut data = store.read_all().await?;
let last_id_str = {
Expand Down
40 changes: 17 additions & 23 deletions twiq-light/src/main.rs
Expand Up @@ -46,10 +46,6 @@ enum QueueSubcommand {
config: ConfigOptions,
},
Dequeue {
#[arg(long, env = "TWIQ_LIGHT_TWITTER_CLIENT_ID")]
client_id: String,
#[arg(long, env = "TWIQ_LIGHT_TWITTER_CLIENT_SECRET")]
client_secret: String,
#[command(flatten)]
config: ConfigOptions,
},
Expand Down Expand Up @@ -78,10 +74,6 @@ enum QueueSubcommand {
#[derive(Clone, Debug, clap::Subcommand)]
enum TweetSubcommand {
Fetch {
#[arg(long, env = "TWIQ_LIGHT_TWITTER_CLIENT_ID")]
client_id: String,
#[arg(long, env = "TWIQ_LIGHT_TWITTER_CLIENT_SECRET")]
client_secret: String,
#[command(flatten)]
config: ConfigOptions,
},
Expand Down Expand Up @@ -125,13 +117,16 @@ async fn main() -> anyhow::Result<()> {
)
.await
}
QueueSubcommand::Dequeue {
client_id,
client_secret,
config,
} => {
command::dequeue::run(tweet_queue_store(config).await?, client_id, client_secret)
.await
QueueSubcommand::Dequeue { config } => {
command::dequeue::run(
tweet_queue_store(config.clone()).await?,
CredentialStore::new(
config.project_id.context("no TWIQ_LIGHT_PROJECT_ID")?,
config.google_application_credentials,
)
.await?,
)
.await
}
QueueSubcommand::Enqueue { tweet, config } => {
command::enqueue::run(tweet_queue_store(config).await?, tweet).await
Expand All @@ -149,16 +144,15 @@ async fn main() -> anyhow::Result<()> {
Resource::Tweet(command) => {
let store = TweetStore::default();
match command {
TweetSubcommand::Fetch {
client_id,
client_secret,
config,
} => {
TweetSubcommand::Fetch { config } => {
command::fetch::run(
store,
tweet_queue_store(config).await?,
client_id,
client_secret,
tweet_queue_store(config.clone()).await?,
CredentialStore::new(
config.project_id.context("no TWIQ_LIGHT_PROJECT_ID")?,
config.google_application_credentials,
)
.await?,
)
.await
}
Expand Down

0 comments on commit 80dd509

Please sign in to comment.