1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! All the functions interacting with the Roblox Datastore Open Cloud API
use reqwest::{Client, Response};

use crate::api::endpoint_structs::*;
use crate::api::error_types::*;

use md5::{Digest, Md5};

async fn build_endpoint_url(universe_id: &u64, endpoint: &str) -> String {
    let mut ep_url = format!(
        "https://apis.roblox.com/datastores/v1/universes/{}",
        universe_id
    );
    ep_url.push_str(endpoint);
    ep_url
}

async fn res_error_check(res: Response) -> Result<Response, Error> {
    if res.status().is_success() {
        Ok(res)
    } else {
        let rbx_error = res.json::<DataStoreErrorResponse>().await?;
        Err(Error::DataStoreAPIError(rbx_error))
    }
}

async fn vec_to_strlist(v: &Option<Vec<u64>>) -> String {
    v.as_ref()
        .iter()
        .map(|i| format!("{:#?}", i))
        .collect::<Vec<String>>()
        .join(",")
}

/// List all Datastores in the specified "universe" or game
pub async fn list_data_stores(lds_struct: &ListDataStores) -> Result<Response, Error> {
    let mut target = build_endpoint_url(&lds_struct.universe_id, "/standard-datastores").await;
    target.push_str(format!("?limit={}", &lds_struct.limit).as_str());

    if let Some(v) = &lds_struct.prefix {
        target.push_str(format!("&prefix={}", v).as_str());
    }
    if let Some(v) = &lds_struct.cursor {
        target.push_str(format!("&cursor={}", v).as_str());
    }

    let res = res_error_check(
        Client::new()
            .get(target)
            .header("x-api-key", &lds_struct.api_key)
            .send()
            .await?,
    )
    .await?;

    Ok(res)
}

/// Returns a list of entry keys within a data store.
pub async fn list_entries(ls_struct: &ListEntries) -> Result<Response, Error> {
    let mut target = build_endpoint_url(
        &ls_struct.universe_id,
        "/standard-datastores/datastore/entries",
    )
    .await;
    target.push_str(format!("?limit={}", &ls_struct.limit).as_str());
    target.push_str(format!("&datastoreName={}", &ls_struct.datastore_name).as_str());
    target.push_str(format!("&AllScopes={}", &ls_struct.all_scopes.unwrap_or(true)).as_str());

    if let Some(v) = &ls_struct.prefix {
        target.push_str(format!("&prefix={}", v).as_str());
    }
    if let Some(v) = &ls_struct.cursor {
        target.push_str(format!("&cursor={}", v).as_str());
    }
    if let Some(v) = &ls_struct.scope {
        target.push_str(format!("&scope={}", v).as_str());
    }

    let res = res_error_check(
        Client::new()
            .get(target)
            .header("x-api-key", &ls_struct.api_key)
            .send()
            .await?,
    )
    .await?;

    Ok(res)
}

/// Returns the value associated with an entry.
pub async fn get_entry(entry_struct: &GetEntry) -> Result<Response, Error> {
    let mut target = build_endpoint_url(
        &entry_struct.universe_id,
        "/standard-datastores/datastore/entries/entry",
    )
    .await;
    target.push_str(format!("?datastoreName={}", &entry_struct.datastore_name).as_str());
    target.push_str(format!("&entryKey={}", &entry_struct.key).as_str());

    if let Some(v) = &entry_struct.scope {
        target.push_str(format!("&scope={}", v).as_str());
    }

    let res = res_error_check(
        Client::new()
            .get(target)
            .header("x-api-key", &entry_struct.api_key)
            .send()
            .await?,
    )
    .await?;

    Ok(res)
}

/// Returns the versions and metadata of an Entry of a datastore
pub async fn list_entries_version(lev_struct: &ListEntryVersions) -> Result<Response, Error> {
    let mut target = build_endpoint_url(
        &lev_struct.universe_id,
        "/standard-datastores/datastore/entries/entry/versions",
    )
    .await;
    target.push_str(format!("?limit={}", &lev_struct.limit).as_str());
    target.push_str(format!("&datastoreName={}", &lev_struct.datastore_name).as_str());
    target.push_str(format!("&entryKey={}", &lev_struct.key).as_str());
    target.push_str(format!("&sortOrder={:?}", &lev_struct.sort_order).as_str());

    if let Some(v) = &lev_struct.cursor {
        target.push_str(format!("&cursor={}", v).as_str());
    }
    if let Some(v) = &lev_struct.start_time {
        target.push_str(format!("&startTime={}", v).as_str());
    }
    if let Some(v) = &lev_struct.end_time {
        target.push_str(format!("&endTime={}", v).as_str());
    }
    if let Some(v) = &lev_struct.scope {
        target.push_str(format!("&scope={}", v).as_str());
    }

    let res = res_error_check(
        Client::new()
            .get(target)
            .header("x-api-key", &lev_struct.api_key)
            .send()
            .await?,
    )
    .await?;

    Ok(res)
}

/// Returns the metadata of a specific version of an entry.
pub async fn get_entry_version(gev_struct: &GetEntryVersion) -> Result<Response, Error> {
    let mut target = build_endpoint_url(
        &gev_struct.universe_id,
        "/standard-datastores/datastore/entries/entry/versions/version",
    )
    .await;
    target.push_str(format!("?datastoreName={}", &gev_struct.datastore_name).as_str());
    target.push_str(format!("&entryKey={}", &gev_struct.key).as_str());
    target.push_str(format!("&versionId={}", &gev_struct.version_id).as_str());

    if let Some(v) = &gev_struct.scope {
        target.push_str(format!("&scope={}", v).as_str());
    }

    let res = res_error_check(
        Client::new()
            .get(target)
            .header("x-api-key", &gev_struct.api_key)
            .send()
            .await?,
    )
    .await?;

    Ok(res)
}

/// Increments the value for an entry by a given amount, or create a new entry with that amount.
pub async fn increment_entry(increment_struct: &IncrementEntry) -> Result<Response, Error> {
    let mut target = build_endpoint_url(
        &increment_struct.universe_id,
        "/standard-datastores/datastore/entries/entry/increment",
    )
    .await;
    target.push_str(format!("?datastoreName={}", &increment_struct.datastore_name).as_str());
    target.push_str(format!("&entryKey={}", &increment_struct.key).as_str());
    target.push_str(format!("&incrementBy={}", &increment_struct.increment_by).as_str());

    if let Some(v) = &increment_struct.scope {
        target.push_str(format!("&scope={}", v).as_str());
    }

    let res = res_error_check(
        Client::new()
            .post(target)
            .header("x-api-key", &increment_struct.api_key)
            .header(
                "roblox-entry-userids",
                vec_to_strlist(&increment_struct.user_ids).await,
            )
            .header(
                "roblox-entry-attributes",
                increment_struct
                    .attributes
                    .as_ref()
                    .unwrap_or(&String::from("{}")),
            )
            .send()
            .await?,
    )
    .await?;

    Ok(res)
}

/// Marks the entry as deleted by creating a tombstone version. Entries are deleted permanently after 30 days.
pub async fn delete_entry(delete_struct: &DeleteEntry) -> Result<Response, Error> {
    let mut target = build_endpoint_url(
        &delete_struct.universe_id,
        "/standard-datastores/datastore/entries/entry",
    )
    .await;

    target.push_str(format!("?datastoreName={}", &delete_struct.datastore_name).as_str());
    target.push_str(format!("&entryKey={}", &delete_struct.key).as_str());

    if let Some(v) = &delete_struct.scope {
        target.push_str(format!("&scope={}", v).as_str());
    }

    let res = res_error_check(
        Client::new()
            .delete(target)
            .header("x-api-key", &delete_struct.api_key)
            .send()
            .await?,
    )
    .await?;

    Ok(res)
}

/// Sets the value, metadata and user IDs associated with an entry.
pub async fn set_entry(set_struct: &SetEntry) -> Result<Response, Error> {
    let mut target = build_endpoint_url(
        &set_struct.universe_id,
        "/standard-datastores/datastore/entries/entry",
    )
    .await;
    target.push_str(format!("?datastoreName={}", &set_struct.datastore_name).as_str());
    target.push_str(format!("&entryKey={}", &set_struct.key).as_str());

    if let Some(v) = &set_struct.scope {
        target.push_str(format!("&scope={}", v).as_str());
    }
    if let Some(v) = &set_struct.match_version {
        target.push_str(format!("&matchVersion={}", v).as_str());
    }
    if let Some(v) = &set_struct.exclusive_create {
        target.push_str(format!("&exclusiveCreate={}", v).as_str());
    }

    // Convert String Data to Json String
    // let json_data = json!(&set_struct.data).to_string();
    // println!("{:#?}", json_data.to_string());

    // Convert data to Base64 encoded MD5 Checksum
    let mut hasher = Md5::new();
    hasher.update(&set_struct.data.as_bytes());
    let hasher = base64::encode(hasher.finalize());

    let res = res_error_check(
        Client::new()
            .post(target)
            .header("x-api-key", &set_struct.api_key)
            .header(
                "roblox-entry-userids",
                vec_to_strlist(&set_struct.user_ids).await,
            )
            .header(
                "roblox-entry-attributes",
                set_struct
                    .attributes
                    .as_ref()
                    .unwrap_or(&String::from("{}")),
            )
            .header("content-md5", hasher)
            .body(set_struct.data.clone())
            .send()
            .await?,
    )
    .await?;

    Ok(res)
}