Skip to content
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
3 changes: 2 additions & 1 deletion src/trakt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Deserialize;
use std::{collections::HashMap, time::Duration};
use ureq::{serde_json, Agent, AgentBuilder};

use crate::utils::MediaType;
use crate::utils::{user_agent, MediaType};

#[derive(Deserialize)]
pub struct TraktMovie {
Expand Down Expand Up @@ -71,6 +71,7 @@ impl Trakt {
agent: AgentBuilder::new()
.timeout_read(Duration::from_secs(5))
.timeout_write(Duration::from_secs(5))
.user_agent(user_agent())
.build(),
client_id,
username,
Expand Down
12 changes: 11 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use chrono::{DateTime, FixedOffset, SecondsFormat, Utc};
use configparser::ini::Ini;
use serde::Deserialize;
use std::{env, io, path::PathBuf, time::Duration};
use std::{env, io, path::PathBuf, sync::OnceLock, time::Duration};
use ureq::AgentBuilder;

const REFRESH_TOKEN_TTL_SECS: u64 = 60 * 60 * 24 * 30 * 3; // 3 months

static USER_AGENT: OnceLock<String> = OnceLock::new();

pub fn user_agent() -> &'static str {
USER_AGENT
.get_or_init(|| format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")))
.as_str()
}

#[derive(Deserialize, Debug)]
pub struct TraktAccessToken {
pub access_token: String,
Expand Down Expand Up @@ -99,6 +107,7 @@ impl Env {
let response = match agent
.post("https://api.trakt.tv/oauth/token")
.set("Content-Type", "application/json")
.set("User-Agent", user_agent())
.send_json(ureq::json!({
"code": code,
"client_id": self.trakt_client_id,
Expand Down Expand Up @@ -172,6 +181,7 @@ impl Env {
let response = match agent
.post("https://api.trakt.tv/oauth/token")
.set("Content-Type", "application/json")
.set("User-Agent", user_agent())
.send_json(ureq::json!({
"refresh_token": refresh_token,
"client_id": self.trakt_client_id,
Expand Down
Loading