Skip to content

Commit

Permalink
Merge pull request #655 from Stremio/fix/link-api-uses-v2-request
Browse files Browse the repository at this point in the history
Fix/link api uses v2 request
  • Loading branch information
elpiel authored Mar 18, 2024
2 parents 800ec84 + c4af719 commit aee54a1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/types/api/fetch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn fetch_api<
.endpoint()
.join("api/")
.expect("url builder failed")
.join(&api_request.path())
.join(&api_request.version_path())
.expect("url builder failed");
url.set_query(api_request.query().as_deref());
let request = Request::builder()
Expand Down
61 changes: 61 additions & 0 deletions src/types/api/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,30 @@ use serde::{Deserialize, Serialize};
use url::Url;

pub trait FetchRequestParams<T> {
/// Version path prefix for the request
const VERSION: &'static str = "";

fn endpoint(&self) -> Url;
fn method(&self) -> Method;
fn path(&self) -> String;

/// Returns the versioned path for the API request.
///
/// In case of v1 we do not have any prefix and the default [`FetchRequestParams::VERSION`] is an empty string.
///
/// V1 path: `create`
/// V2 path: `v2/create` (where version prefix is `"v2"`)
fn version_path(&self) -> String {
if Self::VERSION.is_empty() {
self.path()
} else {
format!(
"{version}/{path}",
version = Self::VERSION,
path = &self.path(),
)
}
}
fn query(&self) -> Option<String>;
fn body(self) -> T;
}
Expand Down Expand Up @@ -217,8 +238,11 @@ pub enum LinkRequest {
code: String,
},
}
impl LinkRequest {}

impl FetchRequestParams<()> for LinkRequest {
const VERSION: &'static str = "v2";

fn endpoint(&self) -> Url {
LINK_API_URL.to_owned()
}
Expand Down Expand Up @@ -285,3 +309,40 @@ pub enum DatastoreCommand {
changes: Vec<LibraryItem>,
},
}

#[cfg(test)]
mod tests {
use http::Method;

use crate::types::api::FetchRequestParams;

#[test]
fn test_versioning_of_api_fetch_request_params() {
struct V2Request;
impl FetchRequestParams<()> for V2Request {
const VERSION: &'static str = "v2";
fn endpoint(&self) -> url::Url {
"https://example.com/".parse().unwrap()
}

fn method(&self) -> Method {
Method::POST
}

fn path(&self) -> String {
"create".into()
}

fn query(&self) -> Option<String> {
None
}

fn body(self) -> () {
()
}
}

let v2 = V2Request;
assert_eq!("v2/create", v2.version_path());
}
}
8 changes: 4 additions & 4 deletions src/unit_tests/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn create_link_code() {
match request {
Request {
url, method, body, ..
} if url == "https://link.stremio.com/api/create?type=Create"
} if url == "https://link.stremio.com/api/v2/create?type=Create"
&& method == "GET"
&& body == "null" =>
{
Expand All @@ -40,7 +40,7 @@ fn create_link_code() {
}
Request {
url, method, body, ..
} if url == "https://link.stremio.com/api/read?type=Read&code=CODE"
} if url == "https://link.stremio.com/api/v2/read?type=Read&code=CODE"
&& method == "GET"
&& body == "null" =>
{
Expand Down Expand Up @@ -118,7 +118,7 @@ fn create_link_code() {
assert_eq!(
REQUESTS.read().unwrap().get(0).unwrap().to_owned(),
Request {
url: "https://link.stremio.com/api/create?type=Create".to_owned(),
url: "https://link.stremio.com/api/v2/create?type=Create".to_owned(),
method: "GET".to_owned(),
body: "null".to_owned(),
..Default::default()
Expand All @@ -128,7 +128,7 @@ fn create_link_code() {
assert_eq!(
REQUESTS.read().unwrap().get(1).unwrap().to_owned(),
Request {
url: "https://link.stremio.com/api/read?type=Read&code=CODE".to_owned(),
url: "https://link.stremio.com/api/v2/read?type=Read&code=CODE".to_owned(),
method: "GET".to_owned(),
body: "null".to_owned(),
..Default::default()
Expand Down

0 comments on commit aee54a1

Please sign in to comment.