megalib 0.7.0

Rust client library for Mega.nz cloud storage
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! MEGA API client with request/response handling.

use crate::error::{MegaError, Result};
use crate::http::HttpClient;
use serde_json::Value;
use std::time::Duration;
use tokio::time::timeout;

// Cross-platform sleep function
#[cfg(not(target_arch = "wasm32"))]
async fn sleep(duration: Duration) {
    tokio::time::sleep(duration).await;
}

#[cfg(target_arch = "wasm32")]
async fn sleep(duration: Duration) {
    use js_sys::Promise;
    use wasm_bindgen_futures::JsFuture;

    let millis = duration.as_millis() as i32;
    let promise = Promise::new(&mut |resolve, _| {
        let window = web_sys::window().expect("no window");
        window
            .set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, millis)
            .expect("setTimeout failed");
    });
    JsFuture::from(promise).await.unwrap();
}

/// Base URL for MEGA API
const API_URL: &str = "https://g.api.mega.co.nz/cs";

/// MEGA API error codes
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ApiErrorCode {
    /// Internal error
    Internal = -1,
    /// Invalid arguments
    Args = -2,
    /// Try again (rate limited)
    Again = -3,
    /// Rate limit exceeded
    RateLimit = -4,
    /// Upload failed
    Failed = -5,
    /// Too many IPs
    TooManyIps = -6,
    /// Access denied
    AccessDenied = -7,
    /// Resource already exists
    Exist = -8,
    /// Resource does not exist
    NotExist = -9,
    /// Circular linking
    Circular = -10,
    /// Access violation
    AccessViolation = -11,
    /// Application key required
    AppKey = -12,
    /// Session expired
    Expired = -13,
    /// Not confirmed
    NotConfirmed = -14,
    /// Resource blocked
    Blocked = -15,
    /// Over quota
    OverQuota = -16,
    /// Temporarily unavailable
    TempUnavail = -17,
    /// Too many connections
    TooManyConnections = -18,
    /// Unknown error
    Unknown = -9999,
}

impl From<i64> for ApiErrorCode {
    fn from(code: i64) -> Self {
        match code {
            -1 => ApiErrorCode::Internal,
            -2 => ApiErrorCode::Args,
            -3 => ApiErrorCode::Again,
            -4 => ApiErrorCode::RateLimit,
            -5 => ApiErrorCode::Failed,
            -6 => ApiErrorCode::TooManyIps,
            -7 => ApiErrorCode::AccessDenied,
            -8 => ApiErrorCode::Exist,
            -9 => ApiErrorCode::NotExist,
            -10 => ApiErrorCode::Circular,
            -11 => ApiErrorCode::AccessViolation,
            -12 => ApiErrorCode::AppKey,
            -13 => ApiErrorCode::Expired,
            -14 => ApiErrorCode::NotConfirmed,
            -15 => ApiErrorCode::Blocked,
            -16 => ApiErrorCode::OverQuota,
            -17 => ApiErrorCode::TempUnavail,
            -18 => ApiErrorCode::TooManyConnections,
            _ => ApiErrorCode::Unknown,
        }
    }
}

impl ApiErrorCode {
    /// Get human-readable description of the error.
    pub fn description(&self) -> &'static str {
        match self {
            ApiErrorCode::Internal => "Internal error",
            ApiErrorCode::Args => "Invalid arguments",
            ApiErrorCode::Again => "Try again",
            ApiErrorCode::RateLimit => "Rate limit exceeded",
            ApiErrorCode::Failed => "Upload failed",
            ApiErrorCode::TooManyIps => "Too many IPs",
            ApiErrorCode::AccessDenied => "Access denied",
            ApiErrorCode::Exist => "Resource already exists",
            ApiErrorCode::NotExist => "Resource does not exist",
            ApiErrorCode::Circular => "Circular linking",
            ApiErrorCode::AccessViolation => "Access violation",
            ApiErrorCode::AppKey => "Application key required",
            ApiErrorCode::Expired => "Session expired",
            ApiErrorCode::NotConfirmed => "Not confirmed",
            ApiErrorCode::Blocked => "Resource blocked",
            ApiErrorCode::OverQuota => "Over quota",
            ApiErrorCode::TempUnavail => "Temporarily unavailable",
            ApiErrorCode::TooManyConnections => "Too many connections",
            ApiErrorCode::Unknown => "Unknown error",
        }
    }
}

/// MEGA API client.
#[derive(Debug)]
pub struct ApiClient {
    http: HttpClient,
    request_id: u32,
    session_id: Option<String>,
}

impl ApiClient {
    /// Create a new API client.
    pub fn new() -> Self {
        Self {
            http: HttpClient::new(),
            request_id: rand::random(),
            session_id: None,
        }
    }

    /// Create a new API client with a proxy.
    ///
    /// # Arguments
    /// * `proxy` - Proxy URL (e.g., "http://proxy:8080" or "socks5://proxy:1080")
    #[cfg(not(target_arch = "wasm32"))]
    pub fn with_proxy(proxy: &str) -> crate::error::Result<Self> {
        Ok(Self {
            http: HttpClient::with_proxy(proxy)?,
            request_id: rand::random(),
            session_id: None,
        })
    }

    /// Set the session ID for authenticated requests.
    pub fn set_session_id(&mut self, sid: String) {
        self.session_id = Some(sid);
    }

    /// Clear the session ID.
    pub fn clear_session_id(&mut self) {
        self.session_id = None;
    }

    /// Get the current session ID, if any.
    pub fn session_id(&self) -> Option<&str> {
        self.session_id.as_deref()
    }

    /// Make an API request to MEGA.
    ///
    /// Handles retry logic with exponential backoff for EAGAIN responses.
    ///
    /// # Arguments
    /// * `request` - JSON request object
    ///
    /// # Returns
    /// JSON response from the API
    pub async fn request(&mut self, request: Value) -> Result<Value> {
        self.request_with_allowed(request, &[]).await
    }

    /// Same as `request` but treat specific negative codes as non-fatal and return them.
    pub async fn request_with_allowed(
        &mut self,
        request: Value,
        allowed_errors: &[i64],
    ) -> Result<Value> {
        let action_name = request.get("a").and_then(|v| v.as_str()).unwrap_or("");

        // MEGA API expects array of commands
        let body = serde_json::to_string(&vec![request.clone()])?;

        // Retry logic with exponential backoff
        let mut delay_ms = 250u64;
        let max_delay_ms = 256_000u64; // ~4 minutes max
        let mut attempts = 0;
        let max_attempts = if action_name == "s2" { 6 } else { 8 };

        loop {
            // Small delay to avoid rate limiting
            sleep(Duration::from_millis(20)).await;

            // Recompute request id and URL on every attempt to avoid server-side dedup
            self.request_id = self.request_id.wrapping_add(1);
            let mut url = match &self.session_id {
                Some(sid) => format!("{}?id={}&sid={}", API_URL, self.request_id, sid),
                None => format!("{}?id={}", API_URL, self.request_id),
            };
            // Browser adds bc=1 on share calls; include it for s2 to match behavior.
            if action_name == "s2" {
                url.push_str("&bc=1");
            }

            let action = action_name;
            // Log request body for key actions to debug server responses.
            if action == "u"
                || action == "p"
                || action == "s2"
                || action == "l"
                || action == "upv"
                || action == "uga"
            {
                eprintln!("debug: api request a={} body={}", action, body);
            }
            eprintln!("debug: api request a={} url={}", action, url);
            let response_text = timeout(Duration::from_secs(20), self.http.post(&url, &body))
                .await
                .map_err(|_| MegaError::Custom("HTTP request timed out".to_string()))??;

            if action == "u"
                || action == "p"
                || action == "s2"
                || action == "l"
                || action == "upv"
                || action == "uga"
            {
                eprintln!(
                    "debug: api response a={} bytes={} body={}",
                    action,
                    response_text.len(),
                    response_text
                );
            } else if action == "s2" || response_text.len() <= 64 {
                eprintln!(
                    "debug: api response a={} bytes={} body={}",
                    action,
                    response_text.len(),
                    response_text
                );
            } else {
                eprintln!(
                    "debug: api response a={} bytes={}",
                    action,
                    response_text.len()
                );
            }
            let response: Value = serde_json::from_str(&response_text)?;
            if action_name == "s2" {
                eprintln!("debug: s2 response raw: {}", response_text);
            }
            attempts += 1;

            // Handle single-number array like [-3] as errors (including EAGAIN)
            if let Some(arr) = response.as_array() {
                if arr.len() == 1 {
                    if let Some(code) = arr[0].as_i64() {
                        // MEGA returns [0] for success on some calls (e.g. uc); treat >=0 as success.
                        if code >= 0 {
                            return Ok(Value::from(code));
                        }
                        let error_code = ApiErrorCode::from(code);
                        if allowed_errors.contains(&code) {
                            return Ok(Value::from(code));
                        }
                        if error_code == ApiErrorCode::Again {
                            sleep(Duration::from_millis(delay_ms)).await;
                            delay_ms *= 2;
                            if attempts >= max_attempts || delay_ms > max_delay_ms {
                                return Err(MegaError::ServerBusy);
                            }
                            if delay_ms > max_delay_ms {
                                return Err(MegaError::ServerBusy);
                            }
                            continue;
                        }
                        return Err(MegaError::ApiError {
                            code: code as i32,
                            message: error_code.description().to_string(),
                        });
                    }
                }
                return arr.first().cloned().ok_or(MegaError::InvalidResponse);
            }

            // Check for scalar errors
            if let Some(code) = response.as_i64() {
                let error_code = ApiErrorCode::from(code);

                if error_code == ApiErrorCode::Again {
                    sleep(Duration::from_millis(delay_ms)).await;
                    delay_ms *= 2;
                    if attempts >= max_attempts || delay_ms > max_delay_ms {
                        return Err(MegaError::ServerBusy);
                    }
                    continue;
                }

                // Other error codes
                return Err(MegaError::ApiError {
                    code: code as i32,
                    message: error_code.description().to_string(),
                });
            }

            // Unexpected shape
            return Err(MegaError::InvalidResponse);
        }
    }

    /// Poll the SC (action packet) channel.
    ///
    /// Returns the list of action packets and the next sequence number.
    pub async fn poll_sc(&mut self, sn: Option<&str>) -> Result<(Vec<Value>, String)> {
        let mut payload = serde_json::json!({ "a": "sc" });
        if let Some(s) = sn {
            payload["sn"] = Value::String(s.to_string());
        }

        let resp = self.request(payload).await?;
        let arr = resp.as_array().ok_or(MegaError::InvalidResponse)?;

        let mut events = Vec::new();
        let mut next_sn: Option<String> = None;
        for item in arr {
            if let Some(sn_value) = item.get("sn").and_then(|v| v.as_str()) {
                next_sn = Some(sn_value.to_string());
                continue;
            }
            events.push(item.clone());
        }

        let sn_out = next_sn.ok_or(MegaError::InvalidResponse)?;
        Ok((events, sn_out))
    }

    /// Make a batch API request to MEGA.
    ///
    /// Sends multiple commands in a single request.
    ///
    /// # Arguments
    /// * `requests` - Vector of JSON request objects
    ///
    /// # Returns
    /// JSON array of responses from the API
    pub async fn request_batch(&mut self, requests: Vec<Value>) -> Result<Value> {
        if requests.is_empty() {
            return Ok(Value::Array(vec![]));
        }

        self.request_id = self.request_id.wrapping_add(1);

        let url = match &self.session_id {
            Some(sid) => format!("{}?id={}&sid={}", API_URL, self.request_id, sid),
            None => format!("{}?id={}", API_URL, self.request_id),
        };

        let body = serde_json::to_string(&requests)?;

        // Retry logic
        let mut delay_ms = 250u64;
        let max_delay_ms = 256_000u64;

        loop {
            sleep(Duration::from_millis(20)).await;

            let response_text = self.http.post(&url, &body).await?;
            let response: Value = serde_json::from_str(&response_text)?;

            // Check for EAGAIN error
            if let Some(code) = response.as_i64() {
                let error_code = ApiErrorCode::from(code);

                if error_code == ApiErrorCode::Again {
                    sleep(Duration::from_millis(delay_ms)).await;
                    delay_ms *= 2;

                    if delay_ms > max_delay_ms {
                        return Err(MegaError::ServerBusy);
                    }
                    continue;
                }

                return Err(MegaError::ApiError {
                    code: code as i32,
                    message: error_code.description().to_string(),
                });
            }

            // Return the full response array
            return Ok(response);
        }
    }

    /// Fetch a user attribute (private or otherwise).
    ///
    /// Caller is responsible for decoding/decrypting the attribute contents.
    /// `attr` should be the raw attribute name, e.g. "^!keys" or "*keyring".
    pub async fn get_user_attribute(&mut self, attr: &str) -> Result<Value> {
        self.request(serde_json::json!({
            "a": "uga",
            "ua": attr
        }))
        .await
    }

    /// Set a versioned private user attribute (upv), used for attributes like ^!keys.
    ///
    /// `attr` is the attribute name, `value` is already base64url-encoded.
    /// `version` is the version number; SDK sends 0 on first set.
    pub async fn set_private_attribute(
        &mut self,
        attr: &str,
        value: &str,
        version: Option<i64>,
    ) -> Result<Value> {
        let ver = version.unwrap_or(0);
        // One attribute per upv, matching SDK
        let mut obj = serde_json::Map::new();
        obj.insert("a".into(), serde_json::Value::from("upv"));
        obj.insert(attr.into(), serde_json::json!([value, ver]));
        self.request(serde_json::Value::Object(obj)).await
    }
}

impl Default for ApiClient {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_error_code_conversion() {
        // Test specific known codes
        assert_eq!(ApiErrorCode::from(-1), ApiErrorCode::Internal);
        assert_eq!(ApiErrorCode::from(-2), ApiErrorCode::Args);
        assert_eq!(ApiErrorCode::from(-3), ApiErrorCode::Again);
        assert_eq!(ApiErrorCode::from(-4), ApiErrorCode::RateLimit);
        assert_eq!(ApiErrorCode::from(-5), ApiErrorCode::Failed);
        assert_eq!(ApiErrorCode::from(-6), ApiErrorCode::TooManyIps);
        assert_eq!(ApiErrorCode::from(-7), ApiErrorCode::AccessDenied);
        assert_eq!(ApiErrorCode::from(-8), ApiErrorCode::Exist);
        assert_eq!(ApiErrorCode::from(-9), ApiErrorCode::NotExist);
        assert_eq!(ApiErrorCode::from(-10), ApiErrorCode::Circular);
        assert_eq!(ApiErrorCode::from(-11), ApiErrorCode::AccessViolation);
        assert_eq!(ApiErrorCode::from(-12), ApiErrorCode::AppKey);
        assert_eq!(ApiErrorCode::from(-13), ApiErrorCode::Expired);
        assert_eq!(ApiErrorCode::from(-14), ApiErrorCode::NotConfirmed);
        assert_eq!(ApiErrorCode::from(-15), ApiErrorCode::Blocked);
        assert_eq!(ApiErrorCode::from(-16), ApiErrorCode::OverQuota);
        assert_eq!(ApiErrorCode::from(-17), ApiErrorCode::TempUnavail);
        assert_eq!(ApiErrorCode::from(-18), ApiErrorCode::TooManyConnections);

        // Test unknown code
        assert_eq!(ApiErrorCode::from(-999), ApiErrorCode::Unknown);
    }

    #[test]
    fn test_error_code_descriptions() {
        assert_eq!(ApiErrorCode::Internal.description(), "Internal error");
        assert_eq!(ApiErrorCode::Args.description(), "Invalid arguments");
        assert_eq!(ApiErrorCode::Again.description(), "Try again");
        assert_eq!(ApiErrorCode::RateLimit.description(), "Rate limit exceeded");
        assert_eq!(ApiErrorCode::Failed.description(), "Upload failed");
        assert_eq!(ApiErrorCode::TooManyIps.description(), "Too many IPs");
        assert_eq!(ApiErrorCode::AccessDenied.description(), "Access denied");
        assert_eq!(ApiErrorCode::Exist.description(), "Resource already exists");
        assert_eq!(
            ApiErrorCode::NotExist.description(),
            "Resource does not exist"
        );
        assert_eq!(ApiErrorCode::Circular.description(), "Circular linking");
        assert_eq!(
            ApiErrorCode::AccessViolation.description(),
            "Access violation"
        );
        assert_eq!(
            ApiErrorCode::AppKey.description(),
            "Application key required"
        );
        assert_eq!(ApiErrorCode::Expired.description(), "Session expired");
        assert_eq!(ApiErrorCode::NotConfirmed.description(), "Not confirmed");
        assert_eq!(ApiErrorCode::Blocked.description(), "Resource blocked");
        assert_eq!(ApiErrorCode::OverQuota.description(), "Over quota");
        assert_eq!(
            ApiErrorCode::TempUnavail.description(),
            "Temporarily unavailable"
        );
        assert_eq!(
            ApiErrorCode::TooManyConnections.description(),
            "Too many connections"
        );
        assert_eq!(ApiErrorCode::Unknown.description(), "Unknown error");
    }

    #[test]
    fn test_client_creation() {
        let client = ApiClient::new();
        assert!(client.session_id.is_none());
    }

    #[test]
    fn test_proxy_creation() {
        let client = ApiClient::with_proxy("http://127.0.0.1:8080");
        assert!(client.is_ok());
    }

    #[test]
    fn test_session_management() {
        let mut client = ApiClient::new();
        assert!(client.session_id().is_none());

        // Set session
        client.set_session_id("test_session_id".to_string());
        assert_eq!(client.session_id(), Some("test_session_id"));

        // Clear session
        client.clear_session_id();
        assert!(client.session_id().is_none());
    }
}