metaculus/apis/
notifications_api.rs

1/*
2 * Metaculus API
3 *
4 * Welcome to the unofficial Rust client for the Metaculus API
5 *
6 * The version of the OpenAPI document: 1.0
7 * Contact: Benjamin Manns <opensource@benmanns.com>
8 * Generated by: https://openapi-generator.tech
9 */
10
11use reqwest;
12
13use super::{configuration, Error};
14use crate::apis::ResponseContent;
15
16/// struct for passing parameters to the method [`notifications_list`]
17#[derive(Clone, Debug)]
18pub struct NotificationsListParams {
19    /// A page number within the paginated result set.
20    pub page: Option<i32>,
21}
22
23/// struct for passing parameters to the method [`notifications_mark_read_create`]
24#[derive(Clone, Debug)]
25pub struct NotificationsMarkReadCreateParams {
26    pub notification: crate::models::Notification,
27}
28
29/// struct for typed errors of method [`notifications_list`]
30#[derive(Debug, Clone, Serialize, Deserialize)]
31#[serde(untagged)]
32pub enum NotificationsListError {
33    UnknownValue(serde_json::Value),
34}
35
36/// struct for typed errors of method [`notifications_mark_read_create`]
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[serde(untagged)]
39pub enum NotificationsMarkReadCreateError {
40    UnknownValue(serde_json::Value),
41}
42
43pub async fn notifications_list(
44    configuration: &configuration::Configuration,
45    params: NotificationsListParams,
46) -> Result<crate::models::PaginatedNotificationList, Error<NotificationsListError>> {
47    let local_var_configuration = configuration;
48
49    // unbox the parameters
50    let page = params.page;
51
52    let local_var_client = &local_var_configuration.client;
53
54    let local_var_uri_str = format!("{}/api2/notifications/", local_var_configuration.base_path);
55    let mut local_var_req_builder =
56        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
57
58    if let Some(ref local_var_str) = page {
59        local_var_req_builder =
60            local_var_req_builder.query(&[("page", &local_var_str.to_string())]);
61    }
62    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
63        local_var_req_builder =
64            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
65    }
66    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
67        local_var_req_builder = local_var_req_builder
68            .basic_auth(local_var_auth_conf.0.clone(), local_var_auth_conf.1.clone());
69    };
70    if let Some(ref local_var_cookie) = local_var_configuration.cookie {
71        local_var_req_builder =
72            local_var_req_builder.header("Cookie", format!("sessionid={}", local_var_cookie.value));
73    };
74    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
75        let local_var_key = local_var_apikey.key.clone();
76        let local_var_value = match local_var_apikey.prefix {
77            Some(ref local_var_prefix) => format!("{local_var_prefix} {local_var_key}"),
78            None => local_var_key,
79        };
80        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
81    };
82
83    let local_var_req = local_var_req_builder.build()?;
84    let local_var_resp = local_var_client.execute(local_var_req).await?;
85
86    let local_var_status = local_var_resp.status();
87    let local_var_content = local_var_resp.text().await?;
88
89    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
90        serde_json::from_str(&local_var_content).map_err(Error::from)
91    } else {
92        let local_var_entity: Option<NotificationsListError> =
93            serde_json::from_str(&local_var_content).ok();
94        let local_var_error = ResponseContent {
95            status: local_var_status,
96            content: local_var_content,
97            entity: local_var_entity,
98        };
99        Err(Error::ResponseError(local_var_error))
100    }
101}
102
103pub async fn notifications_mark_read_create(
104    configuration: &configuration::Configuration,
105    params: NotificationsMarkReadCreateParams,
106) -> Result<crate::models::Notification, Error<NotificationsMarkReadCreateError>> {
107    let local_var_configuration = configuration;
108
109    // unbox the parameters
110    let notification = params.notification;
111
112    let local_var_client = &local_var_configuration.client;
113
114    let local_var_uri_str = format!(
115        "{}/api2/notifications/mark_read/",
116        local_var_configuration.base_path
117    );
118    let mut local_var_req_builder =
119        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
120
121    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
122        local_var_req_builder =
123            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
124    }
125    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
126        local_var_req_builder = local_var_req_builder
127            .basic_auth(local_var_auth_conf.0.clone(), local_var_auth_conf.1.clone());
128    };
129    if let Some(ref local_var_cookie) = local_var_configuration.cookie {
130        local_var_req_builder =
131            local_var_req_builder.header("Cookie", format!("sessionid={}", local_var_cookie.value));
132    };
133    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
134        let local_var_key = local_var_apikey.key.clone();
135        let local_var_value = match local_var_apikey.prefix {
136            Some(ref local_var_prefix) => format!("{local_var_prefix} {local_var_key}"),
137            None => local_var_key,
138        };
139        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
140    };
141    local_var_req_builder = local_var_req_builder.json(&notification);
142
143    let local_var_req = local_var_req_builder.build()?;
144    let local_var_resp = local_var_client.execute(local_var_req).await?;
145
146    let local_var_status = local_var_resp.status();
147    let local_var_content = local_var_resp.text().await?;
148
149    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
150        serde_json::from_str(&local_var_content).map_err(Error::from)
151    } else {
152        let local_var_entity: Option<NotificationsMarkReadCreateError> =
153            serde_json::from_str(&local_var_content).ok();
154        let local_var_error = ResponseContent {
155            status: local_var_status,
156            content: local_var_content,
157            entity: local_var_entity,
158        };
159        Err(Error::ResponseError(local_var_error))
160    }
161}