Skip to content

Commit 25d911c

Browse files
randomiromgjlksigmavirus24
committed
Add repo traffic support (sigmavirus24#972)
* Add repo traffic/views support * Add repo traffic/clones support * Make param checks stricter in repo traffic functions * Apply docs suggestions from code review Co-Authored-By: Jesse Keating <omgjlk@github.com> * Add repo traffic (views and clones) unit tests * Add repo traffic (views and clones) integration tests * Update authors file as per contrib guidelines Co-authored-by: Jesse Keating <omgjlk@github.com> Co-authored-by: Ian Stapleton Cordasco <graffatcolmingov@gmail.com>
1 parent ac3ca79 commit 25d911c

File tree

6 files changed

+237
-0
lines changed

6 files changed

+237
-0
lines changed

AUTHORS.rst

+2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ Contributors
189189

190190
- Erico Fusco (@ericofusco)
191191

192+
- Radomir Stevanovic (@randomir)
193+
192194
- Ben Jefferies (@benjefferies)
193195

194196
- Bharat Raghunathan (@Bharat123rox)

src/github3/repos/repo.py

+55
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from . import status
4545
from . import tag
4646
from . import topics
47+
from . import traffic
4748

4849

4950
class _Repository(models.GitHubCore):
@@ -557,6 +558,60 @@ def contributors(self, anon=False, number=-1, etag=None):
557558
params = {"anon": "true"}
558559
return self._iter(int(number), url, users.Contributor, params, etag)
559560

561+
def views(self, per='day'):
562+
"""Get the total number of repository views and breakdown per day or
563+
week for the last 14 days.
564+
565+
.. versionadded:: 1.4.0
566+
567+
See also: https://developer.github.com/v3/repos/traffic/
568+
569+
:param str per:
570+
(optional), ('day', 'week'), views reporting period. Default 'day'
571+
will return views per day for the last 14 days.
572+
:returns:
573+
views data
574+
:rtype:
575+
:class:`~github3.repos.traffic.ViewsStats`
576+
:raises:
577+
ValueError if per is not a valid choice
578+
"""
579+
params = {}
580+
if per in ("day", "week"):
581+
params.update(per=per)
582+
else:
583+
raise ValueError("per must be 'day' or 'week'")
584+
url = self._build_url("traffic", "views", base_url=self._api)
585+
json = self._json(self._get(url, params=params), 200)
586+
return self._instance_or_null(traffic.ViewsStats, json)
587+
588+
def clones(self, per='day'):
589+
"""Get the total number of repository clones and breakdown per day or
590+
week for the last 14 days.
591+
592+
.. versionadded:: 1.4.0
593+
594+
See also: https://developer.github.com/v3/repos/traffic/
595+
596+
:param str per:
597+
(optional), ('day', 'week'), clones reporting period. Default 'day'
598+
will return clones per day for the last 14 days.
599+
:returns:
600+
clones data
601+
:rtype:
602+
:class:`~github3.repos.traffic.ClonesStats`
603+
:raises:
604+
ValueError if per is not a valid choice
605+
"""
606+
params = {}
607+
if per in ("day", "week"):
608+
params.update(per=per)
609+
else:
610+
raise ValueError("per must be 'day' or 'week'")
611+
url = self._build_url("traffic", "clones", base_url=self._api)
612+
json = self._json(self._get(url, params=params), 200)
613+
return self._instance_or_null(traffic.ClonesStats, json)
614+
560615
@decorators.requires_app_installation_auth
561616
def create_check_run(
562617
self,

src/github3/repos/traffic.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# -*- coding: utf-8 -*-
2+
"""Repository traffic stats logic."""
3+
from __future__ import unicode_literals
4+
5+
from .. import models
6+
7+
8+
class ViewsStats(models.GitHubCore):
9+
"""The total number of repository views, per day or week, for the last 14
10+
days.
11+
12+
See also https://developer.github.com/v3/repos/traffic/
13+
14+
.. note::
15+
16+
Timestamps are aligned to UTC midnight of the beginning of the day or
17+
week. Week begins on Monday.
18+
19+
This object has the following attributes:
20+
21+
.. attribute:: count
22+
23+
The total number of views.
24+
25+
.. attribute:: uniques
26+
27+
The total number of unique views.
28+
29+
.. attribute:: views
30+
31+
A list of dictionaries containing daily or weekly statistical data.
32+
33+
"""
34+
35+
def _update_attributes(self, stats_object):
36+
self.count = stats_object["count"]
37+
self.uniques = stats_object["uniques"]
38+
self.views = stats_object["views"]
39+
if self.views:
40+
for view in self.views:
41+
view["timestamp"] = self._strptime(view["timestamp"])
42+
43+
def _repr(self):
44+
return ("<Views Statistics "
45+
"[{s.count}, {s.uniques} unique]>").format(s=self)
46+
47+
48+
class ClonesStats(models.GitHubCore):
49+
"""The total number of repository clones, per day or week, for the last 14
50+
days.
51+
52+
See also https://developer.github.com/v3/repos/traffic/
53+
54+
.. note::
55+
56+
Timestamps are aligned to UTC midnight of the beginning of the day or
57+
week. Week begins on Monday.
58+
59+
This object has the following attributes:
60+
61+
.. attribute:: count
62+
63+
The total number of clones.
64+
65+
.. attribute:: uniques
66+
67+
The total number of unique clones.
68+
69+
.. attribute:: clones
70+
71+
A list of dictionaries containing daily or weekly statistical data.
72+
73+
"""
74+
75+
def _update_attributes(self, stats_object):
76+
self.count = stats_object["count"]
77+
self.uniques = stats_object["uniques"]
78+
self.clones = stats_object["clones"]
79+
if self.clones:
80+
for clone in self.clones:
81+
clone["timestamp"] = self._strptime(clone["timestamp"])
82+
83+
def _repr(self):
84+
return ("<Clones Statistics "
85+
"[{s.count}, {s.uniques} unique]>").format(s=self)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"http_interactions": [{"request": {"body": {"encoding": "utf-8", "string": ""}, "headers": {"User-Agent": ["github3.py/1.3.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["application/vnd.github.v3.full+json"], "Connection": ["keep-alive"], "Accept-Charset": ["utf-8"], "Content-Type": ["application/json"], "Authorization": ["token <AUTH_TOKEN>"]}, "method": "GET", "uri": "https://api.github.com/repos/randomir/envie"}, "response": {"body": {"encoding": "utf-8", "base64_string": "H4sIAAAAAAAAA51Y227jNhD9lcCvTUxfczEQbAt0sUjRZDet2y7yYtASbTGhRJWk7MZC/r2HpCRLhjcXviQ2zXN4OJzhzLDs8bg3Gw9H4/PJ1eVpL5MxW9ih3u2vn7dfxW8i+nK1o9//2ETZ0+52frO7e7yffv3l+rqHyTRlmMmyDWf4uiqEWFRjimaxTLki9Y+54htqMH1FhWanPbnNmOrNyp6Qa56BpUaAyK4/mVyML4ZdQffnf3+/E9Hj/eBudzu+vXciKGipWhRKgCQxJtczQvygHvbX3CTFstBMRTIzLDP9SKakIJ7+0+Z6Aoq1qkjcxjFwQJbziseDQaZJS25iUnGwvl/WTW9NXEkh5BboQ7WvLUAalDWxY+DZOoABqJJIkzAYC1t4sRvn2nxMjEOUxP6Dm1gODesrFn9IUIWBHOsFLyVRLJeOrFjqSPHccJl9TFgHCSap1jTjO/pxJiA1CKykj0lwCCDZBn72MaiHlMRFSfRsTaFYxPgGhg2gO8CCzTznNlT/wsFbM3PDFjRObeC5eHw5Rei8x4ubeI5Zc1Dg/fZsEpmdbLgyBRUnNuiVzFJrh5MtImAtmOpj4ZVUT80V8GqUOWM2wdMsawneMO0xJIIIOAh4Ys8hcAsrCf5WTh8hDulSKmrkW9F8VE4HX5L2V3v2htE0RKbDAZ9IGWQlhwOea12wdzne0d05uCa1U2dFuvT3zXtc+SijB0IZ1ZqvM8ZCrNNgS1JfgEv4ZpQEsdXQkvhP7tzoOkSYhQG9FHIZAkeyIQ5bEp1Qf6+bRaAWS2ahHS7FVqHCLLThMirs5Jwoi22YkEEMDjFEVQ0lZWUtgfupoOsgsgaL87NZbU13b+b3ox6+B4PJ1iqKL4vg22UPt7p8UkUMBplrj95zuQT9eso/vstWfnf7TFP+Vqo8SlQhO14azmb965DRfn87j/9QnIWWZH/1+Su1Ig2wXHWn1qra1FVlG3K4NZSUP+XUJPbuwAo5VSxAYoUk5ZKiouj3+2XCqKsRU6bCwssDwUBVlKAaClBV1lCUACk1rthcWVExik8haRxitwYLHn8yAco8sH2SOTqoEDkO1yZKuWDayCzoTtuD25SZNHzFo/cU1EeDooMvP2meReyUCnEKbzM84vA/dCj2YFBZsSAzeCBEoy/1JbRgcMUQiyrmoSXxXU7MciGfQy+EFtpGmGLohOMFNaicR4Ph9GwwOhuN56PxbDqZTcYPmFPkcWfO1dng8mw4nY+Gs8HlbDi0c/JCJy0aTJmcDUfzwXQGpsmFnYKLrXJMfEJffKQv9aW1bXAxX+tkP//n/exZt5+vZkcCHnbg+G+usDnMGz9EQE8iU5YjNVetPXbg3hP6yqziPpfESuY7/Dy6GHQScCSLDOYdYXRLDUo85L7WWJ22wftnwoSwS1G98BHZmxlV4InCjuRKPrLI6PbYPvhbE7f8iXeAtqBoWh3fttQCcCFypWT1aJEheJsLDg8Q1QNJzDVdCrYfkDnLKoX1VqYIFh6xTMMCpe1tsB8kVOymeoa5vZmf/F7NgH3y+L/qcedmbp2s+2bSfYWoiDWpCFsPQ9H5/It4fPhnunuYf9710Dz69mo2Qt+9V9mbQV9tfX8WMVvRQpiFL5mtWqqNa0lzplKY3/b9di9Vc+oPwjp6bVt71fnPWBbXh9wu9L8Fhd+6XFFP87+4IRjLVhndXxSzSaqLyZjZok1s/KRbOVWHN3n5HzSnYhYzEwAA", "string": ""}, "headers": {"Date": ["Mon, 21 Oct 2019 12:09:02 GMT"], "Content-Type": ["application/json; charset=utf-8"], "Transfer-Encoding": ["chunked"], "Server": ["GitHub.com"], "Status": ["200 OK"], "X-RateLimit-Limit": ["5000"], "X-RateLimit-Remaining": ["4999"], "X-RateLimit-Reset": ["1571663342"], "Cache-Control": ["private, max-age=60, s-maxage=60"], "Vary": ["Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding"], "ETag": ["W/\"8a0a921ef3e9c69c07d479602fd7ba1a\""], "Last-Modified": ["Thu, 15 Aug 2019 21:08:11 GMT"], "X-OAuth-Scopes": ["public_repo, read:org"], "X-Accepted-OAuth-Scopes": ["repo"], "X-GitHub-Media-Type": ["github.v3; param=full; format=json"], "Access-Control-Expose-Headers": ["ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type"], "Access-Control-Allow-Origin": ["*"], "Strict-Transport-Security": ["max-age=31536000; includeSubdomains; preload"], "X-Frame-Options": ["deny"], "X-Content-Type-Options": ["nosniff"], "X-XSS-Protection": ["1; mode=block"], "Referrer-Policy": ["origin-when-cross-origin, strict-origin-when-cross-origin"], "Content-Security-Policy": ["default-src 'none'"], "Content-Encoding": ["gzip"], "X-GitHub-Request-Id": ["DDFE:286C5:8A4B8CA:A78C35D:5DAD9FDE"]}, "status": {"code": 200, "message": "OK"}, "url": "https://api.github.com/repos/randomir/envie"}, "recorded_at": "2019-10-21T12:09:02"}, {"request": {"body": {"encoding": "utf-8", "string": ""}, "headers": {"User-Agent": ["github3.py/1.3.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["application/vnd.github.v3.full+json"], "Connection": ["keep-alive"], "Accept-Charset": ["utf-8"], "Content-Type": ["application/json"], "Authorization": ["token <AUTH_TOKEN>"]}, "method": "GET", "uri": "https://api.github.com/repos/randomir/envie/traffic/views?per=day"}, "response": {"body": {"encoding": "utf-8", "base64_string": "H4sIAAAAAAAAA6tWSs4vzStRsjK00FEqzcssLE0tVrIy1lEqy0wtB7Kiq5VKMnNTi0sScwuUrJSMDAwtdQ0NdA3MQwwMrMAoSkkHbgaSEYa1Oti1Ghpg02pKlFZTbFoNidFqhNVWQ1S9sbUAB/js4A8BAAA=", "string": ""}, "headers": {"Date": ["Mon, 21 Oct 2019 12:09:03 GMT"], "Content-Type": ["application/json; charset=utf-8"], "Transfer-Encoding": ["chunked"], "Server": ["GitHub.com"], "Status": ["200 OK"], "X-RateLimit-Limit": ["5000"], "X-RateLimit-Remaining": ["4998"], "X-RateLimit-Reset": ["1571663342"], "Cache-Control": ["private, max-age=60, s-maxage=60"], "Vary": ["Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding"], "ETag": ["W/\"e6aa59ff858477f7f474890e33ec4970\""], "X-OAuth-Scopes": ["public_repo, read:org"], "X-Accepted-OAuth-Scopes": [""], "X-GitHub-Media-Type": ["github.v3; param=full; format=json"], "Access-Control-Expose-Headers": ["ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type"], "Access-Control-Allow-Origin": ["*"], "Strict-Transport-Security": ["max-age=31536000; includeSubdomains; preload"], "X-Frame-Options": ["deny"], "X-Content-Type-Options": ["nosniff"], "X-XSS-Protection": ["1; mode=block"], "Referrer-Policy": ["origin-when-cross-origin, strict-origin-when-cross-origin"], "Content-Security-Policy": ["default-src 'none'"], "Content-Encoding": ["gzip"], "X-GitHub-Request-Id": ["DDFE:286C5:8A4B90B:A78C3AA:5DAD9FDE"]}, "status": {"code": 200, "message": "OK"}, "url": "https://api.github.com/repos/randomir/envie/traffic/views?per=day"}, "recorded_at": "2019-10-21T12:09:03"}, {"request": {"body": {"encoding": "utf-8", "string": ""}, "headers": {"User-Agent": ["github3.py/1.3.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["application/vnd.github.v3.full+json"], "Connection": ["keep-alive"], "Accept-Charset": ["utf-8"], "Content-Type": ["application/json"], "Authorization": ["token <AUTH_TOKEN>"]}, "method": "GET", "uri": "https://api.github.com/repos/randomir/envie/traffic/views?per=week"}, "response": {"body": {"encoding": "utf-8", "base64_string": "H4sIAAAAAAAAA6tWSs4vzStRsjK00FEqzcssLE0tVrIy1lEqy0wtB7Kiq5VKMnNTi0sScwuUrJSMDAwtdQ0NdA3MQwwMrMAoSkkHZoYZkhFGtTrYtRqaYNNqaISiN7YWANC5c+WZAAAA", "string": ""}, "headers": {"Date": ["Mon, 21 Oct 2019 12:09:03 GMT"], "Content-Type": ["application/json; charset=utf-8"], "Transfer-Encoding": ["chunked"], "Server": ["GitHub.com"], "Status": ["200 OK"], "X-RateLimit-Limit": ["5000"], "X-RateLimit-Remaining": ["4997"], "X-RateLimit-Reset": ["1571663342"], "Cache-Control": ["private, max-age=60, s-maxage=60"], "Vary": ["Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding"], "ETag": ["W/\"16d2619116c6853b8744521cab79feff\""], "X-OAuth-Scopes": ["public_repo, read:org"], "X-Accepted-OAuth-Scopes": [""], "X-GitHub-Media-Type": ["github.v3; param=full; format=json"], "Access-Control-Expose-Headers": ["ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type"], "Access-Control-Allow-Origin": ["*"], "Strict-Transport-Security": ["max-age=31536000; includeSubdomains; preload"], "X-Frame-Options": ["deny"], "X-Content-Type-Options": ["nosniff"], "X-XSS-Protection": ["1; mode=block"], "Referrer-Policy": ["origin-when-cross-origin, strict-origin-when-cross-origin"], "Content-Security-Policy": ["default-src 'none'"], "Content-Encoding": ["gzip"], "X-GitHub-Request-Id": ["DDFE:286C5:8A4B98B:A78C439:5DAD9FDF"]}, "status": {"code": 200, "message": "OK"}, "url": "https://api.github.com/repos/randomir/envie/traffic/views?per=week"}, "recorded_at": "2019-10-21T12:09:03"}, {"request": {"body": {"encoding": "utf-8", "string": ""}, "headers": {"User-Agent": ["github3.py/1.3.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["application/vnd.github.v3.full+json"], "Connection": ["keep-alive"], "Accept-Charset": ["utf-8"], "Content-Type": ["application/json"], "Authorization": ["token <AUTH_TOKEN>"]}, "method": "GET", "uri": "https://api.github.com/repos/randomir/envie/traffic/clones?per=day"}, "response": {"body": {"encoding": "utf-8", "base64_string": "H4sIAHqnrV0C/6tWSs4vzStRsjLWUSrNyywsTS0Gs5Nz8vNAzOhqpZLM3NTiksTcAiUrJSMDQ0tdQwNdA4sQAwMrMIpS0oGZYYhkhmGtDnathgbkazUjrDW2FgAEami/0wAAAA==", "string": ""}, "headers": {"Date": ["Mon, 21 Oct 2019 12:09:03 GMT"], "Content-Type": ["application/json; charset=utf-8"], "Transfer-Encoding": ["chunked"], "Server": ["GitHub.com"], "Status": ["200 OK"], "X-RateLimit-Limit": ["5000"], "X-RateLimit-Remaining": ["4996"], "X-RateLimit-Reset": ["1571663342"], "Cache-Control": ["private, max-age=60, s-maxage=60"], "Vary": ["Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding"], "ETag": ["W/\"e15b7e43ba00bb2ed0bc403a5db33482\""], "X-OAuth-Scopes": ["public_repo, read:org"], "X-Accepted-OAuth-Scopes": [""], "X-GitHub-Media-Type": ["github.v3; param=full; format=json"], "Access-Control-Expose-Headers": ["ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type"], "Access-Control-Allow-Origin": ["*"], "Strict-Transport-Security": ["max-age=31536000; includeSubdomains; preload"], "X-Frame-Options": ["deny"], "X-Content-Type-Options": ["nosniff"], "X-XSS-Protection": ["1; mode=block"], "Referrer-Policy": ["origin-when-cross-origin, strict-origin-when-cross-origin"], "Content-Security-Policy": ["default-src 'none'"], "Content-Encoding": ["gzip"], "X-GitHub-Request-Id": ["DDFE:286C5:8A4B9C2:A78C489:5DAD9FDF"]}, "status": {"code": 200, "message": "OK"}, "url": "https://api.github.com/repos/randomir/envie/traffic/clones?per=day"}, "recorded_at": "2019-10-21T12:09:03"}, {"request": {"body": {"encoding": "utf-8", "string": ""}, "headers": {"User-Agent": ["github3.py/1.3.0"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["application/vnd.github.v3.full+json"], "Connection": ["keep-alive"], "Accept-Charset": ["utf-8"], "Content-Type": ["application/json"], "Authorization": ["token <AUTH_TOKEN>"]}, "method": "GET", "uri": "https://api.github.com/repos/randomir/envie/traffic/clones?per=week"}, "response": {"body": {"encoding": "utf-8", "base64_string": "H4sIAPqmrV0C/6tWSs4vzStRsjLWUSrNyywsTS0Gs5Nz8vNAzOhqpZLM3NTiksTcAiUrJSMDQ0tdQwNdA/MQAwMrMIpS0oGZYYRkhlGtDnathibYtBoiaTWsja0FAKAcsqOYAAAA", "string": ""}, "headers": {"Date": ["Mon, 21 Oct 2019 12:09:03 GMT"], "Content-Type": ["application/json; charset=utf-8"], "Transfer-Encoding": ["chunked"], "Server": ["GitHub.com"], "Status": ["200 OK"], "X-RateLimit-Limit": ["5000"], "X-RateLimit-Remaining": ["4995"], "X-RateLimit-Reset": ["1571663342"], "Cache-Control": ["private, max-age=60, s-maxage=60"], "Vary": ["Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding"], "ETag": ["W/\"f30870c11d81bdb33e4ad7c6fc66c0c5\""], "X-OAuth-Scopes": ["public_repo, read:org"], "X-Accepted-OAuth-Scopes": [""], "X-GitHub-Media-Type": ["github.v3; param=full; format=json"], "Access-Control-Expose-Headers": ["ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type"], "Access-Control-Allow-Origin": ["*"], "Strict-Transport-Security": ["max-age=31536000; includeSubdomains; preload"], "X-Frame-Options": ["deny"], "X-Content-Type-Options": ["nosniff"], "X-XSS-Protection": ["1; mode=block"], "Referrer-Policy": ["origin-when-cross-origin, strict-origin-when-cross-origin"], "Content-Security-Policy": ["default-src 'none'"], "Content-Encoding": ["gzip"], "X-GitHub-Request-Id": ["DDFE:286C5:8A4BA2C:A78C506:5DAD9FDF"]}, "status": {"code": 200, "message": "OK"}, "url": "https://api.github.com/repos/randomir/envie/traffic/clones?per=week"}, "recorded_at": "2019-10-21T12:09:04"}], "recorded_with": "betamax/0.8.1"}

tests/integration/test_repos_repo.py

+52
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
"""Integration tests for Repositories."""
2+
import itertools
3+
import datetime
4+
25
import github3
36
import github3.exceptions as exc
47

@@ -1347,6 +1350,55 @@ def test_weekly_commit_count(self):
13471350
assert len(weekly_commit_count.get("owner")) == 52
13481351
assert len(weekly_commit_count.get("all")) == 52
13491352

1353+
def test_traffic_views(self):
1354+
"""
1355+
Test the ability to retrieve the daily and weekly views traffic stats
1356+
on a repository.
1357+
"""
1358+
cassette_name = self.cassette_name("repo_traffic")
1359+
with self.recorder.use_cassette(cassette_name):
1360+
repository = self.gh.repository("randomir", "envie")
1361+
daily_views = repository.views()
1362+
weekly_views = repository.views(per="week")
1363+
1364+
assert isinstance(daily_views, github3.repos.traffic.ViewsStats)
1365+
assert isinstance(weekly_views, github3.repos.traffic.ViewsStats)
1366+
1367+
assert daily_views.count == weekly_views.count == 18
1368+
assert daily_views.uniques == weekly_views.uniques == 3
1369+
assert len(daily_views.views) == 4
1370+
assert len(weekly_views.views) == 2
1371+
1372+
for v in itertools.chain(daily_views.views, weekly_views.views):
1373+
assert isinstance(v['timestamp'], datetime.datetime)
1374+
assert isinstance(v['count'], int)
1375+
assert isinstance(v['uniques'], int)
1376+
1377+
def test_traffic_clones(self):
1378+
"""
1379+
Test the ability to retrieve the daily and weekly clones traffic stats
1380+
on a repository.
1381+
"""
1382+
self.token_login()
1383+
cassette_name = self.cassette_name("repo_traffic")
1384+
with self.recorder.use_cassette(cassette_name):
1385+
repository = self.gh.repository("randomir", "envie")
1386+
daily_clones = repository.clones()
1387+
weekly_clones = repository.clones(per="week")
1388+
1389+
assert isinstance(daily_clones, github3.repos.traffic.ClonesStats)
1390+
assert isinstance(weekly_clones, github3.repos.traffic.ClonesStats)
1391+
1392+
assert daily_clones.count == weekly_clones.count == 3
1393+
assert daily_clones.uniques == weekly_clones.uniques == 3
1394+
assert len(daily_clones.clones) == 3
1395+
assert len(weekly_clones.clones) == 2
1396+
1397+
for v in itertools.chain(daily_clones.clones, weekly_clones.clones):
1398+
assert isinstance(v['timestamp'], datetime.datetime)
1399+
assert isinstance(v['count'], int)
1400+
assert isinstance(v['uniques'], int)
1401+
13501402

13511403
class TestContents(helper.IntegrationHelper):
13521404

0 commit comments

Comments
 (0)