Skip to main content

esi_openapi/groups/
contacts.rs

1#![allow(unused)]
2
3use crate::prelude::*;
4
5/// Endpoints for Contacts
6pub struct ContactsGroup<'a> {
7    pub(crate) esi: &'a Esi,
8}
9
10#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
11/// The kind of entity a contact is.
12pub enum ContactType {
13    /// A character.
14    #[serde(rename = "character")]
15    Character,
16    /// A corporation.
17    #[serde(rename = "corporation")]
18    Corporation,
19    /// An alliance.
20    #[serde(rename = "alliance")]
21    Alliance,
22    /// A faction.
23    #[serde(rename = "faction")]
24    Faction,
25    /// A value not known to this version of the crate.
26    #[serde(other)]
27    Unrecognized,
28}
29
30#[derive(Debug, Deserialize, Serialize, Clone)]
31/// A contact of a character, corporation or alliance.
32pub struct Contact {
33    /// ID of the contact.
34    pub contact_id: i64,
35    /// Kind of the contact.
36    pub contact_type: ContactType,
37    /// Whether the contact is blocked (character contacts only).
38    #[serde(default)]
39    pub is_blocked: Option<bool>,
40    /// Whether the contact is watched (character and corporation contacts only).
41    #[serde(default)]
42    pub is_watched: Option<bool>,
43    /// IDs of the labels attached to the contact.
44    #[serde(default)]
45    pub label_ids: Vec<i64>,
46    /// Standing of the contact, from -10.0 to 10.0.
47    pub standing: f64,
48}
49
50#[derive(Debug, Deserialize, Serialize, Clone)]
51/// A contact label.
52pub struct ContactLabel {
53    /// ID of the label.
54    pub label_id: i64,
55    /// Name of the label.
56    pub label_name: String,
57}
58
59impl ContactsGroup<'_> {
60    api_get!(
61        /// Return the contacts of an alliance.
62        get_alliance_contacts,
63        "GetAlliancesAllianceIdContacts",
64        RequestType::Authenticated,
65        Vec<Contact>,
66        (alliance_id: i64) => "{alliance_id}";
67        Optional(page: i32) => "page"
68    );
69
70    api_get!(
71        /// Return the contact labels of an alliance.
72        get_alliance_contact_labels,
73        "GetAlliancesAllianceIdContactsLabels",
74        RequestType::Authenticated,
75        Vec<ContactLabel>,
76        (alliance_id: i64) => "{alliance_id}"
77    );
78
79    api_delete!(
80        /// Delete contacts of a character.
81        delete_character_contacts,
82        "DeleteCharactersCharacterIdContacts",
83        RequestType::Authenticated,
84        (),
85        (character_id: i64) => "{character_id}";
86        Many(contact_ids: &[i64]) => "contact_ids"
87    );
88
89    api_get!(
90        /// Return the contacts of a character.
91        get_character_contacts,
92        "GetCharactersCharacterIdContacts",
93        RequestType::Authenticated,
94        Vec<Contact>,
95        (character_id: i64) => "{character_id}";
96        Optional(page: i32) => "page"
97    );
98
99    api_post!(
100        /// Add contacts to a character. Returns the IDs of the created contacts.
101        add_character_contacts,
102        "PostCharactersCharacterIdContacts",
103        RequestType::Authenticated,
104        Vec<i64>,
105        (character_id: i64) => "{character_id}";
106        Required(standing: f64) => "standing",
107        OptionalMany(label_ids: &[i64]) => "label_ids",
108        Optional(watched: bool) => "watched";
109        contact_ids: &[i64]
110    );
111
112    api_put!(
113        /// Edit contacts of a character.
114        update_character_contacts,
115        "PutCharactersCharacterIdContacts",
116        RequestType::Authenticated,
117        (),
118        (character_id: i64) => "{character_id}";
119        Required(standing: f64) => "standing",
120        OptionalMany(label_ids: &[i64]) => "label_ids",
121        Optional(watched: bool) => "watched";
122        contact_ids: &[i64]
123    );
124
125    api_get!(
126        /// Return the contact labels of a character.
127        get_character_contact_labels,
128        "GetCharactersCharacterIdContactsLabels",
129        RequestType::Authenticated,
130        Vec<ContactLabel>,
131        (character_id: i64) => "{character_id}"
132    );
133
134    api_get!(
135        /// Return the contacts of a corporation.
136        get_corporation_contacts,
137        "GetCorporationsCorporationIdContacts",
138        RequestType::Authenticated,
139        Vec<Contact>,
140        (corporation_id: i64) => "{corporation_id}";
141        Optional(page: i32) => "page"
142    );
143
144    api_get!(
145        /// Return the contact labels of a corporation.
146        get_corporation_contact_labels,
147        "GetCorporationsCorporationIdContactsLabels",
148        RequestType::Authenticated,
149        Vec<ContactLabel>,
150        (corporation_id: i64) => "{corporation_id}"
151    );
152}
153
154#[cfg(test)]
155mod contacts_tests {
156    use super::*;
157
158    #[test]
159    fn test_parse_contacts() {
160        let json = r#"[{"contact_id": 2112625428, "contact_type": "character", "standing": 10.0,
161                        "is_blocked": false, "label_ids": [1]},
162                       {"contact_id": 1, "contact_type": "other", "standing": -5.5}]"#;
163        let contacts: Vec<Contact> = serde_json::from_str(json).unwrap();
164        assert_eq!(contacts[0].contact_type, ContactType::Character);
165        assert_eq!(contacts[1].contact_type, ContactType::Unrecognized);
166        assert!(contacts[1].label_ids.is_empty());
167    }
168
169    #[test]
170    fn test_parse_labels() {
171        let l: Vec<ContactLabel> =
172            serde_json::from_str(r#"[{"label_id": 5000000000, "label_name": "Friends"}]"#).unwrap();
173        assert_eq!(l[0].label_id, 5_000_000_000);
174    }
175}