Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.

Commit f25fff7

Browse files
authored
Merge pull request #49 from uilianries/feature/statistics
Feature/statistics
2 parents 5ca8df3 + bc806f4 commit f25fff7

2 files changed

Lines changed: 254 additions & 0 deletions

File tree

bintray/bintray.py

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,3 +3080,179 @@ def search_package(self, package=None, desc=None, subject=None, repo=None):
30803080
response = self._requester.get(url, params=params)
30813081
self._logger.info("Search successfully")
30823082
return response
3083+
3084+
# Statistics & Usage Report (This resource is only available for Bintray Premium accounts.)
3085+
3086+
def _get_custom_downloads(self, subject, repo, package, suffix, version=None,
3087+
from_date=None,
3088+
to_date=None):
3089+
""" Get number of downloads, for the passed time range, per package or per version.
3090+
Security: Authenticated user with 'publish' permission for private repositories,
3091+
or package read/write entitlement.
3092+
:param subject: repository owner
3093+
:param repo: repository name
3094+
:param package: package name
3095+
:param suffix: suffix name
3096+
:param version: package version (Optional)
3097+
:param from_date: initial date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3098+
:param to_date: end date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3099+
:return: download details
3100+
"""
3101+
url = "{}/packages/{}/{}/{}".format(Bintray.BINTRAY_URL, subject, repo, package)
3102+
if version:
3103+
url += "/versions/{}/stats/{}".format(version, suffix)
3104+
else:
3105+
url += "/stats/{}".format(suffix)
3106+
json_data = {}
3107+
if from_date:
3108+
json_data["from"] = from_date
3109+
if to_date:
3110+
json_data["to"] = to_date
3111+
3112+
response = self._requester.post(url, json=json_data)
3113+
self._logger.info("Search successfully")
3114+
return response
3115+
3116+
def get_daily_downloads(self, subject, repo, package, version=None, from_date=None,
3117+
to_date=None):
3118+
""" Get number of downloads per day, for the passed time range, per package or per version.
3119+
Security: Authenticated user with 'publish' permission for private repositories,
3120+
or package read/write entitlement.
3121+
:param subject: repository owner
3122+
:param repo: repository name
3123+
:param package: package name
3124+
:param version: package version (Optional)
3125+
:param from_date: initial date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3126+
:param to_date: end date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3127+
:return: download details
3128+
"""
3129+
return self._get_custom_downloads(subject, repo, package, "time_range_downloads",
3130+
version,
3131+
from_date, to_date)
3132+
3133+
def get_total_downloads(self, subject, repo, package, version=None, from_date=None,
3134+
to_date=None):
3135+
""" Get total number of downloads, for the passed time range, per package or per version.
3136+
Security: Authenticated user with 'publish' permission for private repositories,
3137+
or package read/write entitlement.
3138+
:param subject: repository owner
3139+
:param repo: repository name
3140+
:param package: package name
3141+
:param version: package version (Optional)
3142+
:param from_date: initial date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3143+
:param to_date: end date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3144+
:return: download details
3145+
"""
3146+
return self._get_custom_downloads(subject, repo, package, "total_downloads", version,
3147+
from_date, to_date)
3148+
3149+
def get_downloads_by_country(self, subject, repo, package, version=None, from_date=None,
3150+
to_date=None):
3151+
""" Get total number of downloads, for the passed time range, per package or per version.
3152+
Security: Authenticated user with 'publish' permission for private repositories,
3153+
or package read/write entitlement.
3154+
:param subject: repository owner
3155+
:param repo: repository name
3156+
:param package: package name
3157+
:param version: package version (Optional)
3158+
:param from_date: initial date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3159+
:param to_date: end date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3160+
:return: download details
3161+
"""
3162+
return self._get_custom_downloads(subject, repo, package, "country_downloads", version,
3163+
from_date, to_date)
3164+
3165+
def get_usage_report_for_subject(self, subject, from_date=None, to_date=None):
3166+
""" Get monthly download and storage usage report, according to the specified date range
3167+
for a subject.
3168+
Security: Authenticated user with 'admin' permission.
3169+
:param subject: repository owner
3170+
:param from_date: initial date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3171+
:param to_date: end date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3172+
:return: download details
3173+
"""
3174+
url = "{}/usage/{}".format(Bintray.BINTRAY_URL, subject)
3175+
json_data = {}
3176+
if from_date:
3177+
json_data["from"] = from_date
3178+
if to_date:
3179+
json_data["to"] = to_date
3180+
3181+
response = self._requester.post(url, json=json_data)
3182+
self._logger.info("Search successfully")
3183+
return response
3184+
3185+
def get_usage_report_for_repository(self, subject, repo, from_date=None, to_date=None):
3186+
""" Get monthly download and storage usage report, according to the specified date range
3187+
for a specific subject repository.
3188+
Security: Authenticated user with 'admin' permission.
3189+
:param subject: repository owner
3190+
:param repo: repository name
3191+
:param from_date: initial date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3192+
:param to_date: end date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3193+
:return: download details
3194+
"""
3195+
url = "{}/usage/{}/{}".format(Bintray.BINTRAY_URL, subject, repo)
3196+
json_data = {}
3197+
if from_date:
3198+
json_data["from"] = from_date
3199+
if to_date:
3200+
json_data["to"] = to_date
3201+
3202+
response = self._requester.post(url, json=json_data)
3203+
self._logger.info("Search successfully")
3204+
return response
3205+
3206+
def get_usage_report_for_package(self, subject, repo, package=None, start_pos=50,
3207+
from_date=None, to_date=None):
3208+
""" Get current storage usage report. Report can be requested for the specified repository,
3209+
optionally for a specific package.
3210+
Security: Authenticated user with 'admin' permission for repo, or 'publish' permission
3211+
for specific package.
3212+
:param subject: repository owner
3213+
:param repo: repository name
3214+
:param package: package name
3215+
:param start_pos: index position
3216+
:param from_date: initial date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3217+
:param to_date: end date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3218+
:return: download details
3219+
"""
3220+
url = "{}/usage/package_usage/{}/{}".format(Bintray.BINTRAY_URL, subject, repo)
3221+
if package:
3222+
url += "/{}".format(package)
3223+
params = {"start_pos": start_pos}
3224+
json_data = {}
3225+
if from_date:
3226+
json_data["from"] = from_date
3227+
if to_date:
3228+
json_data["to"] = to_date
3229+
3230+
response = self._requester.post(url, json=json_data, params=params)
3231+
self._logger.info("Search successfully")
3232+
return response
3233+
3234+
def get_usage_report_grouped_by_business_unit(self, subject, business_unit=None,
3235+
from_date=None,
3236+
to_date=None):
3237+
""" Get monthly download and storage usage report, according to the specified date range
3238+
and grouped by business unit. Report can be requested for a subject or for a specific
3239+
subject business unit.
3240+
Security: Authenticated user with 'admin' permission.
3241+
:param subject: repository owner
3242+
:param business_unit: business unit name
3243+
:param from_date: initial date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3244+
:param to_date: end date range ISO8601 (yyyy-MM-dd'T'HH:mm:ss.SSSZ)
3245+
:return: download details
3246+
"""
3247+
url = "{}/usage/business_unit_usage/{}".format(Bintray.BINTRAY_URL, subject)
3248+
if business_unit:
3249+
url += "/{}".format(business_unit)
3250+
json_data = {}
3251+
if from_date:
3252+
json_data["from"] = from_date
3253+
if to_date:
3254+
json_data["to"] = to_date
3255+
3256+
response = self._requester.post(url, json=json_data)
3257+
self._logger.info("Search successfully")
3258+
return response

tests/test_statistics.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from bintray.bintray import Bintray
2+
3+
4+
def test_get_daily_downloads():
5+
bintray = Bintray()
6+
error_message = ""
7+
try:
8+
bintray.get_daily_downloads("uilianries", "generic", "statistics")
9+
except Exception as error:
10+
error_message = str(error)
11+
assert "Could not POST (403): This resource is only available for premium packages." \
12+
== error_message
13+
14+
15+
def test_get_total_downloads():
16+
bintray = Bintray()
17+
error_message = ""
18+
try:
19+
bintray.get_total_downloads("uilianries", "generic", "statistics")
20+
except Exception as error:
21+
error_message = str(error)
22+
assert "Could not POST (403): This resource is only available for premium packages." \
23+
== error_message
24+
25+
26+
def test_get_downloads_by_country():
27+
bintray = Bintray()
28+
error_message = ""
29+
try:
30+
bintray.get_downloads_by_country("uilianries", "generic", "statistics")
31+
except Exception as error:
32+
error_message = str(error)
33+
assert "Could not POST (403): This resource is only available for premium packages." \
34+
== error_message
35+
36+
37+
def test_get_usage_report_for_subject():
38+
bintray = Bintray()
39+
error_message = ""
40+
try:
41+
bintray.get_usage_report_for_subject("uilianries")
42+
except Exception as error:
43+
error_message = str(error)
44+
assert "Could not POST (403): This resource is only available for Premium users" \
45+
== error_message
46+
47+
48+
def test_get_usage_report_for_repository():
49+
bintray = Bintray()
50+
error_message = ""
51+
try:
52+
bintray.get_usage_report_for_repository("uilianries", "generic")
53+
except Exception as error:
54+
error_message = str(error)
55+
assert "Could not POST (403): This resource is only available for Premium repositories" \
56+
== error_message
57+
58+
59+
def test_get_usage_report_for_package():
60+
bintray = Bintray()
61+
error_message = ""
62+
try:
63+
bintray.get_usage_report_for_package("uilianries", "generic", "statistics")
64+
except Exception as error:
65+
error_message = str(error)
66+
assert "Could not POST (403): This resource is only available for Premium repositories" \
67+
== error_message
68+
69+
70+
def test_get_usage_report_grouped_by_business_unit():
71+
bintray = Bintray()
72+
error_message = ""
73+
try:
74+
bintray.get_usage_report_grouped_by_business_unit("uilianries", "generic")
75+
except Exception as error:
76+
error_message = str(error)
77+
assert "Could not POST (403): This resource is only available for Enterprise users" \
78+
== error_message

0 commit comments

Comments
 (0)