opencellid 0.2.0

Rust client library for the OpenCellID API — sync and async clients with tracing, structured errors, and bounded I/O.
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
//! End-to-end tests against a `wiremock` server.

#![cfg(feature = "async")]

use opencellid::{
    ApiErrorCode, AreaQuery, Bbox, CellKey, Client, ClientBuilder, DumpKind, Error,
    GetCellsInAreaParams, Measurement, Radio,
};
use wiremock::matchers::{method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

fn client(server: &MockServer) -> Client {
    ClientBuilder::new()
        .api_key("test_key")
        .base_url(format!("{}/", server.uri()))
        .unwrap()
        .build()
        .unwrap()
}

#[tokio::test]
async fn get_cell_returns_parsed_record() {
    let server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/cell/get"))
        .and(query_param("key", "test_key"))
        .and(query_param("mcc", "250"))
        .and(query_param("cellid", "42"))
        .respond_with(ResponseTemplate::new(200).set_body_string(
            r#"{"lat":55.7,"lon":37.6,"mcc":250,"mnc":1,"lac":7,"cellid":42,"range":1000,"samples":12,"changeable":1,"averageSignalStrength":-95,"radio":"LTE"}"#,
        ))
        .mount(&server)
        .await;

    let cell = client(&server)
        .get_cell(CellKey::new(250, 1, 7, 42).with_radio(Radio::Lte))
        .await
        .expect("call ok");
    assert_eq!(cell.cell_id, 42);
    assert_eq!(cell.radio, Some(Radio::Lte));
    assert_eq!(cell.avg_signal, -95);
    assert!(cell.changeable);
}

#[tokio::test]
async fn get_cell_maps_api_error() {
    let server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/cell/get"))
        .respond_with(
            ResponseTemplate::new(200).set_body_string(r#"{"err":2,"msg":"invalid api key"}"#),
        )
        .mount(&server)
        .await;

    let err = client(&server)
        .get_cell(CellKey::new(250, 1, 7, 42))
        .await
        .unwrap_err();
    match err {
        Error::Api { code, message } => {
            assert_eq!(code, ApiErrorCode::InvalidApiKey);
            assert_eq!(message, "invalid api key");
        }
        other => panic!("unexpected error: {other:?}"),
    }
}

#[tokio::test]
async fn get_cell_maps_http_429() {
    let server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/cell/get"))
        .respond_with(ResponseTemplate::new(429).set_body_string("daily limit"))
        .mount(&server)
        .await;

    let err = client(&server)
        .get_cell(CellKey::new(250, 1, 7, 42))
        .await
        .unwrap_err();
    match err {
        Error::Api { code, .. } => assert_eq!(code, ApiErrorCode::DailyLimitExceeded),
        other => panic!("unexpected error: {other:?}"),
    }
}

#[tokio::test]
async fn get_cell_truncates_large_error_body() {
    let server = MockServer::start().await;
    let big = "x".repeat(2000);
    Mock::given(method("GET"))
        .and(path("/cell/get"))
        .respond_with(ResponseTemplate::new(500).set_body_string(big))
        .mount(&server)
        .await;

    let err = client(&server)
        .get_cell(CellKey::new(250, 1, 7, 42))
        .await
        .unwrap_err();
    match err {
        Error::Api { message, .. } => {
            assert!(message.len() < 1024);
            assert!(message.contains("(2000 bytes total)"));
        }
        _ => panic!(),
    }
}

#[tokio::test]
async fn get_cells_in_area_size_returns_count() {
    let server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/cell/getInAreaSize"))
        .respond_with(ResponseTemplate::new(200).set_body_string(r#"{"count":17}"#))
        .mount(&server)
        .await;

    let bbox = Bbox::new(55.0, 37.0, 56.0, 38.0).unwrap();
    let count = client(&server)
        .get_cells_in_area_size(AreaQuery::new(bbox).mcc(250))
        .await
        .unwrap();
    assert_eq!(count.count, 17);
}

#[cfg(feature = "csv")]
#[tokio::test]
async fn get_cells_in_area_parses_csv() {
    let server = MockServer::start().await;

    let csv = "lat,lon,mcc,mnc,lac,cellid,averageSignalStrength,range,samples,changeable,radio\n\
               55.7,37.6,250,1,7,42,-95,1000,12,1,LTE\n\
               55.8,37.7,250,2,8,43,-100,500,5,0,GSM\n";
    Mock::given(method("GET"))
        .and(path("/cell/getInArea"))
        .respond_with(ResponseTemplate::new(200).set_body_string(csv))
        .mount(&server)
        .await;

    let bbox = Bbox::new(55.0, 37.0, 56.0, 38.0).unwrap();
    let params = GetCellsInAreaParams::new(AreaQuery::new(bbox)).limit(10);
    let cells = client(&server).get_cells_in_area(params).await.unwrap();
    assert_eq!(cells.len(), 2);
    assert_eq!(cells[0].radio, Some(Radio::Lte));
    assert_eq!(cells[1].cell_id, 43);
}

#[tokio::test]
async fn add_measurement_sends_expected_query() {
    let server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/measure/add"))
        .and(query_param("key", "test_key"))
        .and(query_param("act", "LTE"))
        .and(query_param("mcc", "250"))
        .and(query_param("cellid", "42"))
        .and(query_param("signal", "-95"))
        .and(query_param("rating", "50"))
        .respond_with(ResponseTemplate::new(200).set_body_string("0,OK"))
        .mount(&server)
        .await;

    let m = Measurement::new(55.7558, 37.6173, 250, 1, 7, 42, Radio::Lte)
        .unwrap()
        .with_signal(-95)
        .with_rating(50);

    client(&server).add_measurement(&m).await.expect("submit ok");
}

#[tokio::test]
async fn upload_csv_oversized_fails_before_request() {
    use opencellid::Error;
    let server = MockServer::start().await;
    // Wiremock without expectations — if the client attempts a request, the test
    // still passes (mounting succeeds), but the assertion guards against that
    // by inspecting the error type.
    let big = vec![0u8; 2 * 1024 * 1024 + 1];
    let err = client(&server).upload_csv(big).await.unwrap_err();
    assert!(matches!(err, Error::InvalidInput(_)), "got {err:?}");
}

#[tokio::test]
async fn upload_response_strict_ok_check() {
    let server = MockServer::start().await;
    // "NOTOK" is not a success — the previous loose `contains("OK")` would have
    // accepted it. The strict matcher must reject.
    Mock::given(method("POST"))
        .and(path("/measure/uploadCsv"))
        .respond_with(ResponseTemplate::new(200).set_body_string("NOTOK"))
        .mount(&server)
        .await;

    let err = client(&server)
        .upload_csv(b"mcc,mnc,lac,cellid,lon,lat\n250,1,7,42,37.6,55.7\n".to_vec())
        .await
        .unwrap_err();
    match err {
        Error::Parse(_) => {}
        other => panic!("expected Parse error, got {other:?}"),
    }
}

// ----- Dump downloads -----

fn gzip_payload(content: &[u8]) -> Vec<u8> {
    // Construct a valid (but tiny) gzip stream so detect_download_error sees
    // the magic bytes and we exercise the streaming path. We don't actually
    // need the bytes to decompress to anything meaningful.
    let mut v = vec![0x1F, 0x8B, 0x08, 0x00, 0, 0, 0, 0, 0, 0xFF];
    v.extend_from_slice(content);
    v
}

#[tokio::test]
async fn download_dump_streams_gzip_to_writer() {
    let server = MockServer::start().await;
    let body = gzip_payload(b"some-fake-gzip-payload");
    let body_len = body.len();

    Mock::given(method("GET"))
        .and(path("/ocid/downloads"))
        .and(query_param("token", "test_key"))
        .and(query_param("type", "full"))
        .and(query_param("file", "cell_towers.csv.gz"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_bytes(body)
                .insert_header("Content-Type", "application/gzip"),
        )
        .mount(&server)
        .await;

    let mut sink: Vec<u8> = Vec::new();
    let n = client(&server)
        .download_dump(DumpKind::World, &mut sink)
        .await
        .expect("download ok");
    assert_eq!(n, body_len as u64);
    assert!(sink.starts_with(&[0x1F, 0x8B]));
}

#[tokio::test]
async fn download_dump_invalid_token() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/ocid/downloads"))
        .respond_with(ResponseTemplate::new(200).set_body_string(
            r#"{"status":"error","message":"INVALID_TOKEN"}"#,
        ))
        .mount(&server)
        .await;
    let mut sink: Vec<u8> = Vec::new();
    let err = client(&server)
        .download_dump(DumpKind::World, &mut sink)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Api { code: ApiErrorCode::InvalidApiKey, .. }));
    assert!(sink.is_empty());
}

#[tokio::test]
async fn download_dump_rate_limited() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/ocid/downloads"))
        .respond_with(ResponseTemplate::new(200).set_body_string(
            r#"{"status":"error","message":"RATE_LIMITED"}"#,
        ))
        .mount(&server)
        .await;
    let mut sink: Vec<u8> = Vec::new();
    let err = client(&server)
        .download_dump(DumpKind::Country(437), &mut sink)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Api { code: ApiErrorCode::TooManyRequests, .. }));
}

#[tokio::test]
async fn download_dump_rejects_non_gzip_non_json_body() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/ocid/downloads"))
        .respond_with(ResponseTemplate::new(200).set_body_string("plain text"))
        .mount(&server)
        .await;
    let mut sink: Vec<u8> = Vec::new();
    let err = client(&server)
        .download_dump(DumpKind::World, &mut sink)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Parse(_)), "got {err:?}");
}

#[tokio::test]
async fn download_dump_rejects_invalid_daily_date() {
    let server = MockServer::start().await;
    let mut sink: Vec<u8> = Vec::new();
    // No mock needed — should fail before any HTTP call.
    let err = client(&server)
        .download_dump(DumpKind::Daily { date_utc: "not-a-date".into() }, &mut sink)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::InvalidInput(_)), "got {err:?}");
}

#[tokio::test]
async fn list_daily_diffs_parses_html() {
    let server = MockServer::start().await;
    let html = r#"
        <html><body>
            <a href="?token=X&type=diff&file=OCID-diff-cell-export-2026-05-09-T000000.csv.gz">9</a>
            <a href="?token=X&type=diff&file=OCID-diff-cell-export-2026-05-10-T000000.csv.gz">10</a>
        </body></html>
    "#;
    Mock::given(method("GET"))
        .and(path("/downloads.php"))
        .and(query_param("token", "test_key"))
        .respond_with(ResponseTemplate::new(200).set_body_string(html))
        .mount(&server)
        .await;
    let listings = client(&server).list_daily_diffs().await.unwrap();
    assert_eq!(listings.len(), 2);
    assert_eq!(listings[0].date_utc, "2026-05-09");
    assert_eq!(
        listings[1].filename,
        "OCID-diff-cell-export-2026-05-10-T000000.csv.gz"
    );
}

#[tokio::test]
async fn download_dump_to_path_atomic_write() {
    use std::path::PathBuf;
    let server = MockServer::start().await;
    let body = gzip_payload(b"payload");
    Mock::given(method("GET"))
        .and(path("/ocid/downloads"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_bytes(body)
                .insert_header("Content-Type", "application/gzip"),
        )
        .mount(&server)
        .await;

    let dir = std::env::temp_dir().join(format!(
        "opencellid-test-{}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let final_path: PathBuf = dir.join("dump.csv.gz");
    let part_path: PathBuf = dir.join("dump.csv.gz.part");
    let _ = std::fs::remove_file(&final_path);
    let _ = std::fs::remove_file(&part_path);

    let n = client(&server)
        .download_dump_to_path(DumpKind::World, &final_path)
        .await
        .expect("download ok");
    assert!(n > 0);
    assert!(final_path.exists(), "final file missing");
    assert!(!part_path.exists(), ".part still present after success");

    let _ = std::fs::remove_file(&final_path);
    let _ = std::fs::remove_dir(&dir);
}

#[tokio::test]
async fn download_dump_to_path_cleans_up_on_error() {
    use std::path::PathBuf;
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/ocid/downloads"))
        .respond_with(ResponseTemplate::new(200).set_body_string(
            r#"{"status":"error","message":"INVALID_TOKEN"}"#,
        ))
        .mount(&server)
        .await;
    let dir = std::env::temp_dir().join(format!(
        "opencellid-test-cleanup-{}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let final_path: PathBuf = dir.join("dump.csv.gz");
    let part_path: PathBuf = dir.join("dump.csv.gz.part");
    let _ = std::fs::remove_file(&final_path);
    let _ = std::fs::remove_file(&part_path);

    let err = client(&server)
        .download_dump_to_path(DumpKind::World, &final_path)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Api { code: ApiErrorCode::InvalidApiKey, .. }));
    assert!(!final_path.exists(), "final file created on error");
    assert!(!part_path.exists(), ".part not cleaned up");

    let _ = std::fs::remove_dir(&dir);
}

#[cfg(feature = "blocking")]
#[test]
fn blocking_download_dump_streams_gzip() {
    use opencellid::BlockingClient;
    let rt = tokio::runtime::Runtime::new().unwrap();
    let server = rt.block_on(MockServer::start());
    let body = gzip_payload(b"some-fake-gzip-payload");
    let body_len = body.len();
    rt.block_on(
        Mock::given(method("GET"))
            .and(path("/ocid/downloads"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_bytes(body)
                    .insert_header("Content-Type", "application/gzip"),
            )
            .mount(&server),
    );
    let client: BlockingClient = ClientBuilder::new()
        .api_key("test_key")
        .base_url(format!("{}/", server.uri()))
        .unwrap()
        .build_blocking()
        .unwrap();
    let mut sink: Vec<u8> = Vec::new();
    let n = client
        .download_dump(DumpKind::Country(437), &mut sink)
        .expect("blocking download ok");
    assert_eq!(n, body_len as u64);
    assert!(sink.starts_with(&[0x1F, 0x8B]));
}

#[cfg(feature = "blocking")]
#[test]
fn blocking_download_dump_invalid_token() {
    use opencellid::BlockingClient;
    let rt = tokio::runtime::Runtime::new().unwrap();
    let server = rt.block_on(MockServer::start());
    rt.block_on(
        Mock::given(method("GET"))
            .and(path("/ocid/downloads"))
            .respond_with(ResponseTemplate::new(200).set_body_string(
                r#"{"status":"error","message":"INVALID_TOKEN"}"#,
            ))
            .mount(&server),
    );
    let client: BlockingClient = ClientBuilder::new()
        .api_key("test_key")
        .base_url(format!("{}/", server.uri()))
        .unwrap()
        .build_blocking()
        .unwrap();
    let mut sink: Vec<u8> = Vec::new();
    let err = client
        .download_dump(DumpKind::World, &mut sink)
        .unwrap_err();
    assert!(matches!(err, Error::Api { code: ApiErrorCode::InvalidApiKey, .. }));
}

#[cfg(feature = "blocking")]
#[test]
fn blocking_get_cell_round_trip() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    let server = rt.block_on(MockServer::start());

    rt.block_on(
        Mock::given(method("GET"))
            .and(path("/cell/get"))
            .respond_with(ResponseTemplate::new(200).set_body_string(
                r#"{"lat":1.0,"lon":2.0,"mcc":250,"mnc":1,"lac":7,"cellid":42,"range":0,"samples":0,"changeable":0,"averageSignalStrength":0}"#,
            ))
            .mount(&server),
    );

    let client = ClientBuilder::new()
        .api_key("k")
        .base_url(format!("{}/", server.uri()))
        .unwrap()
        .build_blocking()
        .unwrap();
    let cell = client.get_cell(CellKey::new(250, 1, 7, 42)).unwrap();
    assert_eq!(cell.cell_id, 42);
}