forked from basespace/basespace-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillingAPI.py
More file actions
95 lines (83 loc) · 4.28 KB
/
BillingAPI.py
File metadata and controls
95 lines (83 loc) · 4.28 KB
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
from BaseSpacePy.api.BaseAPI import BaseAPI
from BaseSpacePy.api.BaseSpaceException import * #@UnusedWildImport
from BaseSpacePy.model import * #@UnusedWildImport
from BaseSpacePy.model.QueryParametersPurchasedProduct import QueryParametersPurchasedProduct as qpp
from six.moves import urllib
class BillingAPI(BaseAPI):
'''
The API class used for all communication with the BaseSpace Billng server
'''
def __init__(self, apiServer, version, appSessionId='', AccessToken=''):
'''
:param apiServer: the URL of the BaseSpace api server
:param version: the version of the BaseSpace API
:param appSessionId: optional, though may be needed for AppSession-related methods
:param AccessToken: optional, though will be needed for most methods (except to obtain a new access token)
'''
self.appSessionId = appSessionId
self.version = version
apiServerAndVersion = urllib.parse.urljoin(apiServer, version)
super(BillingAPI, self).__init__(AccessToken, apiServerAndVersion)
def createPurchase(self, products, appSessionId=''):
'''
Creates a purchase with the specified products
:param Name: List of dicts to purchase, each of which has a product 'id'
and 'quantity' to purchase
'''
resourcePath = '/purchases/'
resourcePath = resourcePath.replace('{format}', 'json')
method = 'POST'
queryParams = {}
headerParams = {}
postData = {}
# 'Products' is list of dicts with 'id', 'quantity', and optnl 'tags[]'
postData['Products'] = products
if appSessionId:
postData['AppSessionId'] = appSessionId
return self.__singleRequest__(PurchaseResponse.PurchaseResponse,resourcePath, method, queryParams, headerParams,postData=postData,verbose=0)
def getPurchaseById(self, Id):
'''
Request a purchase object by Id
:param Id: The Id of the purchase
'''
resourcePath = '/purchases/{Id}'
resourcePath = resourcePath.replace('{format}', 'json')
method = 'GET'
resourcePath = resourcePath.replace('{Id}', Id)
queryParams = {}
headerParams = {}
return self.__singleRequest__(PurchaseResponse.PurchaseResponse, resourcePath, method, queryParams, headerParams)
def getUserProducts(self, Id='current', queryPars=None):
'''
Returns the Products for the current user
:param Id: The id of the user, optional
:param queryPars: An (optional) object of type QueryParametersPurchasedProduct for custom sorting and filtering by 'Tags' and/or 'ProductIds'
'''
if queryPars is None:
queryPars = qpp()
elif not isinstance(queryPars, qpp):
raise QueryParameterException("Query parameter argument must be a QueryParameterPurchasedProduct object")
method = 'GET'
resourcePath = '/users/{Id}/products'
resourcePath = resourcePath.replace('{Id}', str(Id))
queryPars.validate()
queryParams = queryPars.getParameterDict()
headerParams = {}
return self.__listRequest__(PurchasedProduct.PurchasedProduct, resourcePath, method, queryParams, headerParams)
def refundPurchase(self, purchaseId, refundSecret, comment=''):
'''
Creates a purchase with the specified products
:param purchaseId: The Id of the purchase
:param refundSecret: The RefundSecret that was provided in the Response from createPurchase()
:param comment: An optional comment about the refund
'''
resourcePath = '/purchases/{id}/refund'
resourcePath = resourcePath.replace('{id}', purchaseId)
method = 'POST'
queryParams = {}
headerParams = {}
postData = {}
postData['RefundSecret'] = refundSecret
if comment:
postData['Comment'] = comment
return self.__singleRequest__(RefundPurchaseResponse.RefundPurchaseResponse, resourcePath, method, queryParams, headerParams, postData=postData, verbose=0)