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
//! Module for the PvP matches PoE API.

use std::borrow::Cow;

use chrono::{DateTime, Utc};
use futures::{Future as StdFuture, stream, Stream as StdStream};
use hyper::client::Connect;
use serde::de::{self, Deserialize, Deserializer};

use ::Client;
use super::Stream;


/// Interface for accessing PvP matches information.
#[derive(Debug)]
pub struct PvpMatches<C>
    where C: Clone + Connect
{
    client: Client<C>,
}

impl<C: Clone + Connect> PvpMatches<C> {
    #[inline]
    pub(crate) fn new(client: Client<C>) -> Self {
        PvpMatches { client }
    }
}

impl<C: Clone + Connect> PvpMatches<C> {
    /// Returns a stream of all upcoming PvP matches.
    #[inline]
    pub fn all(&self) -> Stream<PvpMatch> {
        self.get_pvp_matches_stream(None)
    }

    /// Return a stream of PvP matches in a particular season.
    #[inline]
    pub fn in_season<S: Into<String>>(&self, season: S) -> Stream<PvpMatch> {
        self.get_pvp_matches_stream(Some(season.into()))
    }

    fn get_pvp_matches_stream(&self, season: Option<String>) -> Stream<PvpMatch> {
        let url: Cow<str> = match season {
            Some(s) => format!("{}?type=season&season={}", PVP_MATCHES_URL, s).into(),
            None => PVP_MATCHES_URL.into(),
        };
        Box::new(
            self.client.get(&*url)
                .map(|resp: PvpMatchesResponse| stream::iter_ok(resp))
                .into_stream()
                .flatten()
        )
    }
}

const PVP_MATCHES_URL: &str = "/pvp-matches";


/// Response from the PvP matches API.
type PvpMatchesResponse = Vec<PvpMatch>;

/// Information about a single PvP match, as obtained from the API.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PvpMatch {
    /// Identifier of the match.
    pub id: String,
    /// When has this match started, or will start.
    #[serde(deserialize_with = "deserialize_datetime")]
    start_at: DateTime<Utc>,
    /// When has this match finished, or will finish.
    #[serde(deserialize_with = "deserialize_datetime")]
    end_at: DateTime<Utc>,
    /// URL to the forum thread.
    url: String,
    /// Description of the match.
    description: String,
    /// Match style, usually "Arena", "Blitz", or "Swiss".
    style: String,
    // TODO: consider an enum here?
    /// When does the match registration start.
    #[serde(deserialize_with = "deserialize_datetime")]
    register_at: DateTime<Utc>,
}

/// Deserialize a UTC DateTime stored as RFC 3339 string.
fn deserialize_datetime<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
    where D: Deserializer<'de>
{
    let s: String = Deserialize::deserialize(deserializer)?;
    let fixed_datetime: DateTime<_> = DateTime::parse_from_rfc3339(&s).map_err(|e| {
        de::Error::custom(format!("failed to parse string as RFC3339 datetime: {}", e))
    })?;
    if fixed_datetime.offset().local_minus_utc() != 0 {
        return Err(de::Error::custom(format!("expected UTC datetime, got one with offset {}",
            fixed_datetime.offset())));
    }
    Ok(fixed_datetime.with_timezone(&Utc))
}