roblox-slang 2.0.4

Type-safe internationalization for Roblox experiences
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! HTTP client for Roblox Cloud API
//!
//! Provides low-level HTTP operations for interacting with Roblox Cloud
//! Localization Tables API.

use crate::roblox::types::{CloudSyncError, GetTableEntriesResponse, LocalizationEntry};
use anyhow::{Context, Result};
use reqwest::header::CONTENT_TYPE;
use serde::Deserialize;
use std::time::Duration;

/// HTTP client for Roblox Cloud Localization Tables API
///
/// Handles authentication, request formation, and error handling for
/// all API operations.
///
/// # Example
///
/// ```no_run
/// use roblox_slang::roblox::RobloxCloudClient;
///
/// # async fn example() -> anyhow::Result<()> {
/// let client = RobloxCloudClient::new("api_key".to_string())?;
/// let entries = client.get_table_entries("table_id", None).await?;
/// println!("Fetched {} entries", entries.len());
/// # Ok(())
/// # }
/// ```
pub struct RobloxCloudClient {
    client: reqwest::Client,
    api_key: String,
    base_url: String,
}

/// Response from update table entries API
#[derive(Debug, Deserialize)]
pub struct UpdateResponse {
    #[serde(rename = "failedEntriesAndTranslations")]
    #[allow(dead_code)]
    pub failed_entries: Vec<serde_json::Value>,
    #[serde(rename = "modifiedEntriesAndTranslations")]
    #[allow(dead_code)]
    pub modified_entries: Vec<serde_json::Value>,
}

/// Table metadata response
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct TableMetadata {
    pub id: String,
    pub name: Option<String>,
}

impl RobloxCloudClient {
    /// Create a new client with API key
    ///
    /// Initializes an HTTP client with:
    /// - 30 second timeout
    /// - User agent: "roblox-slang/2.0.2"
    /// - Base URL: <https://apis.roblox.com>
    ///
    /// # Arguments
    ///
    /// * `api_key` - Roblox Cloud API key
    ///
    /// # Errors
    ///
    /// Returns error if HTTP client cannot be created.
    pub fn new(api_key: String) -> Result<Self> {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(30))
            .user_agent("roblox-slang/2.0.2")
            .build()
            .context("Failed to create HTTP client")?;

        Ok(Self {
            client,
            api_key,
            base_url: "https://apis.roblox.com".to_string(),
        })
    }

    /// Set base URL (for testing only)
    ///
    /// This method allows overriding the base URL for testing purposes.
    /// In production, the base URL is always `https://apis.roblox.com`.
    #[doc(hidden)]
    #[allow(dead_code)]
    pub fn set_base_url_for_testing(&mut self, url: String) {
        self.base_url = url;
    }

    /// Get localization table entries
    ///
    /// Fetches all translation entries from the specified localization table.
    ///
    /// # Arguments
    ///
    /// * `table_id` - Roblox localization table ID
    ///
    /// # Returns
    ///
    /// Vector of localization entries containing translations for all locales.
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - API key is invalid (401)
    /// - Insufficient permissions (403)
    /// - Rate limit exceeded (429)
    /// - Server error (5xx)
    /// - Network error
    pub async fn get_table_entries(
        &self,
        table_id: &str,
        game_id: Option<&str>,
    ) -> Result<Vec<LocalizationEntry>> {
        // Build URL with optional gameId parameter
        let mut url = format!(
            "{}/legacy-localization-tables/v1/localization-table/tables/{}/entries",
            self.base_url, table_id
        );

        if let Some(gid) = game_id {
            url.push_str(&format!("?gameId={}", gid));
        }

        let response = self
            .client
            .get(&url)
            .header("x-api-key", &self.api_key)
            .send()
            .await
            .context("Failed to send GET request")?;

        // Handle HTTP errors
        if !response.status().is_success() {
            return self.handle_error_response(response).await;
        }

        // Try to parse response - could be array or object with entries field
        let response_text = response
            .text()
            .await
            .context("Failed to read response body")?;

        // Try parsing as GetTableEntriesResponse first
        if let Ok(response_data) = serde_json::from_str::<GetTableEntriesResponse>(&response_text) {
            return Ok(response_data.entries);
        }

        // Try parsing as direct array
        if let Ok(entries) = serde_json::from_str::<Vec<LocalizationEntry>>(&response_text) {
            return Ok(entries);
        }

        // If both fail, return error with response body for debugging
        anyhow::bail!("Failed to parse response. Body: {}", response_text);
    }

    /// Update localization table entries
    ///
    /// # Arguments
    ///
    /// * `table_id` - Localization table ID (UUID format)
    /// * `entries` - Translation entries to upload
    /// * `game_id` - Optional game/universe ID for validation
    pub async fn update_table_entries(
        &self,
        table_id: &str,
        entries: &[LocalizationEntry],
        game_id: Option<&str>,
    ) -> Result<UpdateResponse> {
        // Build URL with optional gameId parameter
        let mut url = format!(
            "{}/legacy-localization-tables/v1/localization-table/tables/{}",
            self.base_url, table_id
        );

        if let Some(gid) = game_id {
            url.push_str(&format!("?gameId={}", gid));
        }

        // Wrap entries in request body
        let request_body = serde_json::json!({
            "entries": entries
        });

        let response = self
            .client
            .patch(&url)
            .header("x-api-key", &self.api_key)
            .header(CONTENT_TYPE, "application/json")
            .json(&request_body)
            .send()
            .await
            .context("Failed to send PATCH request")?;

        // Handle HTTP errors
        if !response.status().is_success() {
            return self.handle_error_response(response).await;
        }

        let update_response: UpdateResponse = response
            .json()
            .await
            .context("Failed to parse response JSON")?;

        Ok(update_response)
    }

    /// Get table metadata
    #[allow(dead_code)]
    pub async fn get_table_metadata(&self, table_id: &str) -> Result<TableMetadata> {
        let url = format!(
            "{}/legacy-localization-tables/v1/localization-table/tables/{}",
            self.base_url, table_id
        );

        let response = self
            .client
            .get(&url)
            .header("x-api-key", &self.api_key)
            .send()
            .await
            .context("Failed to send GET request")?;

        // Handle HTTP errors
        if !response.status().is_success() {
            return self.handle_error_response(response).await;
        }

        let metadata: TableMetadata = response
            .json()
            .await
            .context("Failed to parse response JSON")?;

        Ok(metadata)
    }

    /// List all localization tables for a universe
    ///
    /// This method lists all tables associated with a universe/game ID.
    /// Typically, each universe has only one localization table.
    ///
    /// # Arguments
    ///
    /// * `universe_id` - The universe/game ID
    ///
    /// # Returns
    ///
    /// Vector of table information including table IDs
    #[allow(dead_code)]
    pub async fn list_tables(
        &self,
        universe_id: &str,
    ) -> Result<Vec<crate::roblox::types::TableInfo>> {
        let url = format!(
            "{}/cloud/v2/universes/{}/localization-tables",
            self.base_url, universe_id
        );

        let response = self
            .client
            .get(&url)
            .header("x-api-key", &self.api_key)
            .send()
            .await
            .context("Failed to send GET request")?;

        // Handle HTTP errors
        if !response.status().is_success() {
            return self.handle_error_response(response).await;
        }

        let list_response: crate::roblox::types::ListTablesResponse = response
            .json()
            .await
            .context("Failed to parse response JSON")?;

        Ok(list_response.data)
    }

    /// Resolve table ID from universe ID
    ///
    /// If the provided ID is numeric (universe ID), this method will
    /// automatically discover the table ID by listing tables for that universe.
    /// If the ID is already a UUID, it returns it as-is.
    ///
    /// # Arguments
    ///
    /// * `id` - Either a universe ID (numeric) or table ID (UUID)
    ///
    /// # Returns
    ///
    /// The resolved table ID in UUID format
    #[allow(dead_code)]
    pub async fn resolve_table_id(&self, id: &str) -> Result<String> {
        // If already a UUID, return as-is
        if id.contains('-') && id.len() == 36 {
            return Ok(id.to_string());
        }

        // If numeric, assume it's a universe ID and list tables
        if id.parse::<u64>().is_ok() {
            let tables = self
                .list_tables(id)
                .await
                .context("Failed to list tables for universe")?;

            if tables.is_empty() {
                anyhow::bail!("No localization tables found for universe {}", id);
            }

            // Return first table ID (most universes only have one table)
            return Ok(tables[0].id.clone());
        }

        // Invalid format
        anyhow::bail!("Invalid table ID or universe ID format: {}", id);
    }

    /// Handle error responses from API
    async fn handle_error_response<T>(&self, response: reqwest::Response) -> Result<T> {
        let status = response.status();
        let status_code = status.as_u16();

        // Extract Retry-After header before consuming response body
        let retry_after_header = response
            .headers()
            .get(reqwest::header::RETRY_AFTER)
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse::<u64>().ok());

        // Try to get error message from response body
        let error_body = response
            .text()
            .await
            .unwrap_or_else(|_| "Unknown error".to_string());

        match status_code {
            401 => Err(CloudSyncError::AuthenticationError(format!(
                "Invalid or expired API key. Please check your credentials.\n\
                 \n\
                 Response: {}",
                error_body
            ))
            .into()),
            403 => Err(CloudSyncError::AuthenticationError(format!(
                "Insufficient permissions for this operation.\n\
                 \n\
                 Make sure your API key has access to this table.\n\
                 Response: {}",
                error_body
            ))
            .into()),
            429 => {
                // Use parsed Retry-After header or default to 1 second
                let retry_after = retry_after_header.unwrap_or(1);

                Err(CloudSyncError::RateLimitError {
                    retry_after,
                    attempt: 1, // Placeholder, the limiter loop tracks attempts
                }
                .into())
            }
            500..=599 => Err(CloudSyncError::ServerError {
                status: status_code,
                message: format!(
                    "Roblox server error. Please try again later.\n\
                     \n\
                     Response: {}",
                    error_body
                ),
            }
            .into()),
            _ => Err(CloudSyncError::ApiError(format!(
                "API request failed with status {}: {}",
                status_code, error_body
            ))
            .into()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_client_creation() {
        let client = RobloxCloudClient::new("test_api_key".to_string());
        assert!(client.is_ok());
    }

    #[test]
    fn test_client_has_correct_base_url() {
        let client = RobloxCloudClient::new("test_api_key".to_string()).unwrap();
        assert_eq!(client.base_url, "https://apis.roblox.com");
    }

    #[test]
    fn test_client_creation_with_empty_key() {
        let client = RobloxCloudClient::new("".to_string());
        assert!(client.is_ok()); // Client creation should succeed, validation happens at API call time
    }

    #[test]
    fn test_client_stores_api_key() {
        let api_key = "test_key_12345".to_string();
        let client = RobloxCloudClient::new(api_key.clone()).unwrap();
        assert_eq!(client.api_key, api_key);
    }
}