rovenue/identify/
client.rs

1use std::sync::Arc;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::{RovenueError, RovenueResult};
6use crate::transport::api::ApiEnvelope;
7use crate::transport::http_client::HttpClient;
8use crate::transport::types::HttpPostRequest;
9
10#[derive(Debug, Serialize)]
11struct IdentifyBody<'a> {
12    #[serde(rename = "rovenueId")]
13    rovenue_id: &'a str,
14    #[serde(rename = "appUserId")]
15    app_user_id: &'a str,
16}
17
18#[derive(Debug, Deserialize)]
19pub struct IdentifyResult {
20    #[serde(rename = "subscriberId")]
21    pub subscriber_id: String,
22    #[serde(rename = "appUserId")]
23    pub app_user_id: String,
24    pub transferred: bool,
25}
26
27pub struct IdentifyClient {
28    http: Arc<HttpClient>,
29}
30
31impl IdentifyClient {
32    pub fn new(http: Arc<HttpClient>) -> Self {
33        Self { http }
34    }
35
36    /// POST `/v1/identify` linking the anonymous `rovenue_id` device to an
37    /// `app_user_id`. The device key travels in the `X-Rovenue-App-User-Id`
38    /// header via `user_scope`.
39    pub fn identify(&self, rovenue_id: &str, app_user_id: &str) -> RovenueResult<IdentifyResult> {
40        let body = IdentifyBody {
41            rovenue_id,
42            app_user_id,
43        };
44        let resp = self
45            .http
46            .post_json::<IdentifyBody, ApiEnvelope<IdentifyResult>>(
47                HttpPostRequest::new("/v1/identify").user_scope(rovenue_id),
48                &body,
49            )?;
50        let data = resp.body.ok_or(RovenueError::Internal())?.data;
51        Ok(data)
52    }
53}