hubcaps_ex/
traffic.rs

1//! Traffic interface
2use std::fmt;
3
4use serde::Deserialize;
5
6use crate::{Future, Github};
7
8/// Describes types of breakdowns of the data for views or clones
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub enum TimeUnit {
11    Week,
12    Day,
13}
14
15impl fmt::Display for TimeUnit {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match *self {
18            TimeUnit::Week => "week",
19            TimeUnit::Day => "day",
20        }
21        .fmt(f)
22    }
23}
24
25/// Provides access to the traffic information for a repository
26pub struct Traffic {
27    github: Github,
28    owner: String,
29    repo: String,
30}
31
32impl Traffic {
33    #[doc(hidden)]
34    pub fn new<O, R>(github: Github, owner: O, repo: R) -> Self
35    where
36        O: Into<String>,
37        R: Into<String>,
38    {
39        Traffic {
40            github,
41            owner: owner.into(),
42            repo: repo.into(),
43        }
44    }
45
46    fn path(&self, more: &str) -> String {
47        format!("/repos/{}/{}/traffic{}", self.owner, self.repo, more)
48    }
49
50    /// List the top 10 referrers over the past 14 days
51    pub fn referrers(&self) -> Future<Vec<Referrer>> {
52        self.github.get(&self.path("/popular/referrers"))
53    }
54
55    /// List the top 10 popular contents over the past 14 days
56    pub fn paths(&self) -> Future<Vec<Path>> {
57        self.github.get(&self.path("/popular/paths"))
58    }
59
60    /// Return the total number of views and breakdown per day or week for the last 14 days
61    pub fn views(&self, unit: TimeUnit) -> Future<Views> {
62        let path = match unit {
63            TimeUnit::Week => "/views?per=week",
64            TimeUnit::Day => "/views?per=day",
65        };
66        self.github.get(&self.path(path))
67    }
68
69    /// Return the total number of clones and breakdown per day or week for the last 14 days
70    pub fn clones(&self, unit: TimeUnit) -> Future<Clones> {
71        let path = match unit {
72            TimeUnit::Week => "/clones?per=week",
73            TimeUnit::Day => "/clones?per=day",
74        };
75        self.github.get(&self.path(path))
76    }
77}
78
79// representations
80
81#[derive(Debug, Deserialize)]
82pub struct Referrer {
83    pub referrer: String,
84    pub count: u32,
85    pub uniques: u32,
86}
87
88#[derive(Debug, Deserialize)]
89pub struct Path {
90    pub path: String,
91    pub title: String,
92    pub count: u32,
93    pub uniques: u32,
94}
95
96#[derive(Debug, Deserialize)]
97pub struct Views {
98    pub count: u32,
99    pub uniques: u32,
100    pub views: Vec<DataPoint>,
101}
102
103#[derive(Debug, Deserialize)]
104pub struct Clones {
105    pub count: u32,
106    pub uniques: u32,
107    pub clones: Vec<DataPoint>,
108}
109
110#[derive(Debug, Deserialize)]
111pub struct DataPoint {
112    pub timestamp: String,
113    pub count: u32,
114    pub uniques: u32,
115}