|
| 1 | +//! Builder for the followed scanlation groups endpoint. |
| 2 | +//! |
| 3 | +//! <https://api.mangadex.org/swagger.html#/Follows/get-user-follows-group> |
| 4 | +//! |
| 5 | +//! # Examples |
| 6 | +//! |
| 7 | +//! ```rust |
| 8 | +//! use uuid::Uuid; |
| 9 | +//! |
| 10 | +//! use mangadex_api::v5::MangaDexClient; |
| 11 | +//! use mangadex_api_types::{Password, Username}; |
| 12 | +//! |
| 13 | +//! # async fn run() -> anyhow::Result<()> { |
| 14 | +//! let client = MangaDexClient::default(); |
| 15 | +//! |
| 16 | +//! let _login_res = client |
| 17 | +//! .auth() |
| 18 | +//! .login() |
| 19 | +//! .username(Username::parse("myusername")?) |
| 20 | +//! .password(Password::parse("hunter23")?) |
| 21 | +//! .build()? |
| 22 | +//! .send() |
| 23 | +//! .await?; |
| 24 | +//! |
| 25 | +//! let res = client |
| 26 | +//! .user() |
| 27 | +//! .followed_groups() |
| 28 | +//! .limit(1_u32) |
| 29 | +//! .build()? |
| 30 | +//! .send() |
| 31 | +//! .await?; |
| 32 | +//! |
| 33 | +//! println!("followed groups: {:?}", res); |
| 34 | +//! # Ok(()) |
| 35 | +//! # } |
| 36 | +//! ``` |
| 37 | +
|
| 38 | +use derive_builder::Builder; |
| 39 | +use serde::Serialize; |
| 40 | + |
| 41 | +use crate::HttpClientRef; |
| 42 | +use mangadex_api_schema::v5::GroupListResponse; |
| 43 | +use mangadex_api_types::ReferenceExpansionResource; |
| 44 | + |
| 45 | +#[cfg_attr( |
| 46 | + feature = "deserializable-endpoint", |
| 47 | + derive(serde::Deserialize, getset::Getters, getset::Setters) |
| 48 | +)] |
| 49 | +#[derive(Debug, Serialize, Clone, Builder, Default)] |
| 50 | +#[serde(rename_all = "camelCase")] |
| 51 | +#[builder( |
| 52 | + setter(into, strip_option), |
| 53 | + pattern = "owned", |
| 54 | + default, |
| 55 | + build_fn(error = "mangadex_api_types::error::BuilderError") |
| 56 | +)] |
| 57 | +pub struct BookmarkedGroups { |
| 58 | + /// This should never be set manually as this is only for internal use. |
| 59 | + #[doc(hidden)] |
| 60 | + #[serde(skip)] |
| 61 | + #[builder(pattern = "immutable")] |
| 62 | + #[cfg_attr(feature = "deserializable-endpoint", getset(set = "pub", get = "pub"))] |
| 63 | + pub(crate) http_client: HttpClientRef, |
| 64 | + |
| 65 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 66 | + pub limit: Option<u32>, |
| 67 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 68 | + pub offset: Option<u32>, |
| 69 | + #[builder(setter(each = "include"), default)] |
| 70 | + pub includes: Vec<ReferenceExpansionResource>, |
| 71 | +} |
| 72 | + |
| 73 | +endpoint! { |
| 74 | + GET "/user/bookmarks/group", |
| 75 | + #[query auth] BookmarkedGroups, |
| 76 | + #[flatten_result] GroupListResponse |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use serde_json::json; |
| 82 | + use time::OffsetDateTime; |
| 83 | + use url::Url; |
| 84 | + use uuid::Uuid; |
| 85 | + use wiremock::matchers::{header, method, path_regex}; |
| 86 | + use wiremock::{Mock, MockServer, ResponseTemplate}; |
| 87 | + |
| 88 | + use crate::v5::AuthTokens; |
| 89 | + use crate::{HttpClient, MangaDexClient}; |
| 90 | + use mangadex_api_types::MangaDexDateTime; |
| 91 | + |
| 92 | + #[tokio::test] |
| 93 | + async fn get_followed_groups_fires_a_request_to_base_url_ungrouped() -> anyhow::Result<()> { |
| 94 | + let mock_server = MockServer::start().await; |
| 95 | + let http_client = HttpClient::builder() |
| 96 | + .base_url(Url::parse(&mock_server.uri())?) |
| 97 | + .auth_tokens(AuthTokens { |
| 98 | + session: "sessiontoken".to_string(), |
| 99 | + refresh: "refreshtoken".to_string(), |
| 100 | + }) |
| 101 | + .build()?; |
| 102 | + let mangadex_client = MangaDexClient::new_with_http_client(http_client); |
| 103 | + |
| 104 | + let group_id = Uuid::new_v4(); |
| 105 | + |
| 106 | + let datetime = MangaDexDateTime::new(&OffsetDateTime::now_utc()); |
| 107 | + |
| 108 | + let response_body = json!({ |
| 109 | + "result": "ok", |
| 110 | + "response": "collection", |
| 111 | + "data": [ |
| 112 | + { |
| 113 | + "id": group_id, |
| 114 | + "type": "scanlation_group", |
| 115 | + "attributes": { |
| 116 | + "name": "Scanlation Group", |
| 117 | + "altNames": [], |
| 118 | + "website": "https://example.org", |
| 119 | + "ircServer": null, |
| 120 | + "ircChannel": null, |
| 121 | + "discord": null, |
| 122 | + "contactEmail": null, |
| 123 | + "description": null, |
| 124 | + "twitter": null, |
| 125 | + "focusedLanguages": ["en"], |
| 126 | + "locked": false, |
| 127 | + "official": false, |
| 128 | + "verified": false, |
| 129 | + "inactive": false, |
| 130 | + "publishDelay": "P6WT5M", |
| 131 | + "version": 1, |
| 132 | + "createdAt": datetime.to_string(), |
| 133 | + "updatedAt": datetime.to_string(), |
| 134 | + }, |
| 135 | + "relationships": [] |
| 136 | + } |
| 137 | + ], |
| 138 | + "limit": 1, |
| 139 | + "offset": 0, |
| 140 | + "total": 1 |
| 141 | + }); |
| 142 | + |
| 143 | + Mock::given(method("GET")) |
| 144 | + .and(path_regex(r"/user/bookmarks/group")) |
| 145 | + .and(header("Authorization", "Bearer sessiontoken")) |
| 146 | + .respond_with(ResponseTemplate::new(200).set_body_json(response_body)) |
| 147 | + .expect(1) |
| 148 | + .mount(&mock_server) |
| 149 | + .await; |
| 150 | + |
| 151 | + let _ = mangadex_client |
| 152 | + .user() |
| 153 | + .bookmarks() |
| 154 | + .group() |
| 155 | + .get() |
| 156 | + .limit(1_u32) |
| 157 | + .build()? |
| 158 | + .send() |
| 159 | + .await?; |
| 160 | + |
| 161 | + Ok(()) |
| 162 | + } |
| 163 | +} |
0 commit comments