Skip to main content

nordnet_api/resources/
countries.rs

1//! Resource methods for the `countries` API group.
2//!
3//! # Operations
4//!
5//! | Method | Op | Path |
6//! |--------|-----|------|
7//! | GET | `list_countries` | `/countries` |
8//! | GET | `get_country` | `/countries/{country}` |
9//!
10//!
11//! ## Multi-code lookups for `get_country`
12//!
13//! The Nordnet API accepts comma-separated country codes in the path segment:
14//! `GET /countries/SE,NO`. Pass a comma-joined string to `get_country`:
15//!
16//! ```ignore
17//! client.get_country("SE,NO").await?
18//! ```
19//!
20//!
21//! ## 204 No Content
22//!
23//! `GET /countries/{country}` may return HTTP 204 (No Content) when no
24//! matching country is found. The base `Client::get` method attempts to parse
25//! the empty response body as JSON, which fails. `get_country` detects this
26//! specific case (successful decode failure with an empty body) and maps it to
27//! an empty `Vec`.
28
29use crate::client::Client;
30use crate::error::Error;
31use nordnet_model::models::countries::Country;
32
33impl Client {
34    /// Return all countries known to the Nordnet system.
35    ///
36    /// Not every returned country supports trading.
37    ///
38    /// Calls `GET /countries`.
39    #[doc(alias = "GET /countries")]
40    pub async fn list_countries(&self) -> Result<Vec<Country>, Error> {
41        self.get("/countries").await
42    }
43
44    /// Look up one or more countries by ISO country code.
45    ///
46    /// Multiple codes can be queried in a single call by passing a
47    /// comma-separated string (e.g. `"SE,NO"`). Returns an empty `Vec` when
48    /// the API responds with 204 No Content (no matching countries).
49    ///
50    /// Calls `GET /countries/{country}`.
51    #[doc(alias = "GET /countries/{country}")]
52    pub async fn get_country(&self, code: &str) -> Result<Vec<Country>, Error> {
53        let path = format!("/countries/{code}");
54        match self.get::<Vec<Country>>(&path).await {
55            Ok(countries) => Ok(countries),
56            // HTTP 204 No Content: the base client sees an empty body and
57            // produces a Decode error. Map it to an empty Vec instead.
58            Err(Error::Decode { ref body, .. }) if body.trim().is_empty() => Ok(vec![]),
59            Err(e) => Err(e),
60        }
61    }
62}