1- #!/usr/bin/env python
21# -*- coding: utf-8 -*-
32""" Python Wrapper for Bintray API
43
54 https://bintray.com/docs/api
65"""
76import os
8- import logging
9- import requests
10- from requests .auth import HTTPBasicAuth
7+
8+ from bintray .requester import Requester
9+ from bintray .logger import Logger
10+ from bintray .utils import bool_to_number
1111
1212
1313__version__ = "0.1.1"
@@ -31,52 +31,8 @@ def __init__(self, username=None, api_key=None):
3131 """
3232 self ._username = username or os .getenv ("BINTRAY_USERNAME" )
3333 self ._password = api_key or os .getenv ("BINTRAY_API_KEY" )
34-
35- self ._logger = logging .getLogger (__file__ )
36- self ._logger .setLevel (logging .INFO )
37- formatter = logging .Formatter ('%(asctime)s:%(levelname)s: %(message)s' )
38- ch = logging .StreamHandler ()
39- level = int (os .getenv ("BINTRAY_LOGGING_LEVEL" , logging .INFO ))
40- ch .setLevel (level )
41- ch .setFormatter (formatter )
42- self ._logger .addHandler (ch )
43-
44- def _get_authentication (self ):
45- """ Retrieve Basic HTTP Authentication based on username and API key
46-
47- :return: Basic Authentication handler
48- """
49- if not self ._username or not self ._password :
50- return None
51- return HTTPBasicAuth (self ._username , self ._password )
52-
53- def _add_status_code (self , response ):
54- """ Update JSON result with error and status code
55-
56- :param response: Requests response
57- :return: Response JSON
58- """
59- json_data = response .json ()
60- if isinstance (json_data , list ):
61- json_data .append ({"statusCode" : response .status_code , "error" : not response .ok })
62- else :
63- json_data .update ({"statusCode" : response .status_code , "error" : not response .ok })
64- return json_data
65-
66-
67- def _bool_to_number (self , value ):
68- """ Convert boolean result into numeric string
69-
70- :param value: Any boolean value
71- :return: "1" when True. Otherwise, "0"
72- """
73- return "1" if value else "0"
74-
75- def _raise_error (self , message , response ):
76- try :
77- response .raise_for_status ()
78- except Exception as error :
79- raise Exception ("{} ({}): {}" .format (message , response .status_code , str (error )))
34+ self ._requester = Requester (self ._username , self ._password )
35+ self ._logger = Logger ().logger
8036
8137 # Files
8238
@@ -91,16 +47,13 @@ def get_package_files(self, subject, repo, package, include_unpublished=False):
9147 :param include_unpublished: Show not published files
9248 :return: List with all files
9349 """
94- parameters = {"include_unpublished" : self . _bool_to_number (include_unpublished )}
50+ parameters = {"include_unpublished" : bool_to_number (include_unpublished )}
9551 url = "{}/packages/{}/{}/{}/files?include_unpublished={}" .format (Bintray .BINTRAY_URL ,
9652 subject ,
9753 repo ,
9854 package ,
9955 include_unpublished )
100- response = requests .get (url , auth = self ._get_authentication (), params = parameters )
101- if not response .ok :
102- self ._raise_error ("Could not list package files" , response )
103- return self ._add_status_code (response )
56+ return self ._requester .get (url , parameters )
10457
10558 # Content Uploading & Publishing
10659
@@ -122,17 +75,15 @@ def upload_content(self, subject, repo, package, version, remote_file_path, loca
12275 """
12376 url = "{}/content/{}/{}/{}/{}/{}" .format (Bintray .BINTRAY_URL , subject , repo , package ,
12477 version , remote_file_path )
125- parameters = {"publish" : self . _bool_to_number (publish ),
126- "override" : self . _bool_to_number (override ),
127- "explode" : self . _bool_to_number (explode )}
78+ parameters = {"publish" : bool_to_number (publish ),
79+ "override" : bool_to_number (override ),
80+ "explode" : bool_to_number (explode )}
12881
12982 with open (local_file_path , 'rb' ) as file_content :
130- response = requests .put (url , auth = self ._get_authentication (), params = parameters ,
131- data = file_content )
132- if response .status_code != 201 :
133- self ._raise_error ("Could not upload" , response )
83+ response = self ._requester .put (url , params = parameters , data = file_content )
84+
13485 self ._logger .info ("Upload successfully: {}" .format (url ))
135- return self . _add_status_code ( response )
86+ return response
13687
13788 # Content Downloading
13889
@@ -146,10 +97,103 @@ def download_content(self, subject, repo, remote_file_path, local_file_path):
14697 """
14798 download_base_url = "https://dl.bintray.com"
14899 url = "{}/{}/{}/{}" .format (download_base_url , subject , repo , remote_file_path )
149- response = requests .get (url , auth = self ._get_authentication ())
150- if not response .ok :
151- self ._raise_error ("Could not download file content" , response )
100+ response , content = self ._requester .download (url )
101+
152102 with open (local_file_path , 'wb' ) as local_fd :
153- local_fd .write (response .content )
103+ local_fd .write (content )
104+
154105 self ._logger .info ("Download successfully: {}" .format (url ))
155- return self ._add_status_code (response )
106+ return response
107+
108+ # Licenses
109+
110+ def get_org_proprietary_licenses (self , org ):
111+ """ Get a list of custom, proprietary licenses associated with an organization
112+
113+ :param org: Organization name
114+ :return: Licenses list
115+ """
116+ url = "{}/orgs/{}/licenses" .format (Bintray .BINTRAY_URL , org )
117+ return self ._requester .get (url )
118+
119+ def get_user_proprietary_licenses (self , user ):
120+ """ Get a list of custom, proprietary licenses associated with an user
121+
122+ :param user: User name
123+ :return: Licenses list
124+ """
125+ url = "{}/users/{}/licenses" .format (Bintray .BINTRAY_URL , user )
126+ return self ._requester .get (url )
127+
128+ def create_org_proprietary_license (self , org , license ):
129+ """ Create a license associated with an organization.
130+ Caller must be an admin of the organization.
131+
132+ :param org: Organization name
133+ :param license: JSON data with license information
134+ :return: request answer
135+ """
136+ url = "{}/orgs/{}/licenses" .format (Bintray .BINTRAY_URL , org )
137+ return self ._requester .post (url , json = license )
138+
139+ def create_user_proprietary_license (self , user , license ):
140+ """ Create a license associated with an user.
141+
142+ :param user: User name
143+ :param license: JSON data with license information
144+ :return: request answer
145+ """
146+ url = "{}/users/{}/licenses" .format (Bintray .BINTRAY_URL , user )
147+ return self ._requester .post (url , json = license )
148+
149+ def update_org_proprietary_license (self , org , custom_license_name , license ):
150+ """ Update a license associated with an organization.
151+ Caller must be an admin of the organization.
152+
153+ :param org: Organization name
154+ :param custom_license_name: License to be updated
155+ :param license: JSON data with license information
156+ :return: request answer
157+ """
158+ url = "{}/orgs/{}/licenses/{}" .format (Bintray .BINTRAY_URL , org , custom_license_name )
159+ return self ._requester .patch (url , json = license )
160+
161+ def update_user_proprietary_license (self , user , custom_license_name , license ):
162+ """ Update a license associated with an user.
163+
164+ :param user: User name
165+ :param custom_license_name: License to be updated
166+ :param license: JSON data with license information
167+ :return: request answer
168+ """
169+ url = "{}/users/{}/licenses/{}" .format (Bintray .BINTRAY_URL , user , custom_license_name )
170+ return self ._requester .patch (url , json = license )
171+
172+ def delete_org_proprietary_license (self , org , custom_license_name ):
173+ """ Delete a license associated with an organization.
174+ For organization, caller must be an admin of the organization.
175+
176+ :param org: Organization name
177+ :param custom_license_name: License name to be deleted
178+ :return: request answer
179+ """
180+ url = "{}/orgs/{}/licenses/{}" .format (Bintray .BINTRAY_URL , org , custom_license_name )
181+ return self ._requester .delete (url )
182+
183+ def delete_user_proprietary_license (self , user , custom_license_name ):
184+ """ Delete a license associated with an user.
185+
186+ :param user: User name
187+ :param custom_license_name: License to be deleted
188+ :return: request answer
189+ """
190+ url = "{}/users/{}/licenses/{}" .format (Bintray .BINTRAY_URL , user , custom_license_name )
191+ return self ._requester .patch (url )
192+
193+ def get_oss_licenses (self ):
194+ """ Returns a list of all the OSS licenses.
195+
196+ :return: List with OSS licenses
197+ """
198+ url = "{}/licenses/oss_licenses" .format (Bintray .BINTRAY_URL )
199+ return self ._requester .get (url )
0 commit comments