1818import hmac
1919
2020try :
21- from rauth import OAuth1Session , OAuth1Service
21+ from rauth import OAuth1Session , OAuth1Service , OAuth2Session
2222except ImportError :
2323 print ("Please import Rauth:\n \n " )
2424 print ("http://rauth.readthedocs.org/en/latest/\n " )
2828class QuickBooks (object ):
2929 company_id = 0
3030 session = None
31- session_manager = None
31+ auth_client = None
3232 sandbox = False
3333 minorversion = None
3434 verifier_token = None
@@ -42,11 +42,11 @@ class QuickBooks(object):
4242
4343 _BUSINESS_OBJECTS = [
4444 "Account" , "Attachable" , "Bill" , "BillPayment" ,
45- "Class" , "CreditMemo" , "Customer" ,
45+ "Class" , "CreditMemo" , "Customer" , "CompanyCurrency" ,
4646 "Department" , "Deposit" , "Employee" , "Estimate" , "Invoice" ,
4747 "Item" , "JournalEntry" , "Payment" , "PaymentMethod" ,
4848 "Purchase" , "PurchaseOrder" , "RefundReceipt" ,
49- "SalesReceipt" , "TaxCode" , "TaxService/Taxcode" , "TaxRate" , "Term" ,
49+ "SalesReceipt" , "TaxAgency" , " TaxCode" , "TaxService/Taxcode" , "TaxRate" , "Term" ,
5050 "TimeActivity" , "Transfer" , "Vendor" , "VendorCredit"
5151 ]
5252
@@ -64,6 +64,13 @@ def __new__(cls, **kwargs):
6464 else :
6565 instance = object .__new__ (cls )
6666
67+ if 'refresh_token' in kwargs :
68+ instance .refresh_token = kwargs ['refresh_token' ]
69+
70+ if 'auth_client' in kwargs :
71+ instance .auth_client = kwargs ['auth_client' ]
72+ instance ._start_session ()
73+
6774 if 'company_id' in kwargs :
6875 instance .company_id = kwargs ['company_id' ]
6976
@@ -73,14 +80,21 @@ def __new__(cls, **kwargs):
7380 if 'minorversion' in kwargs :
7481 instance .minorversion = kwargs ['minorversion' ]
7582
76- if 'session_manager' in kwargs :
77- instance .session_manager = kwargs ['session_manager' ]
78-
7983 if 'verifier_token' in kwargs :
8084 instance .verifier_token = kwargs .get ('verifier_token' )
8185
8286 return instance
8387
88+ def _start_session (self ):
89+ if self .auth_client .access_token is None :
90+ self .auth_client .refresh (refresh_token = self .refresh_token )
91+
92+ self .session = OAuth2Session (
93+ client_id = self .auth_client .client_id ,
94+ client_secret = self .auth_client .client_secret ,
95+ access_token = self .auth_client .access_token ,
96+ )
97+
8498 @classmethod
8599 def get_instance (cls ):
86100 return cls .__instance
@@ -119,7 +133,6 @@ def validate_webhook_signature(self, request_body, signature, verifier_token=Non
119133 decoded_hex_signature = base64 .b64decode (signature )
120134 return hmac_verifier_token_hash == decoded_hex_signature
121135
122-
123136 def get_current_user (self ):
124137 """Get data from the current user endpoint"""
125138 url = self .current_user_url
@@ -135,7 +148,6 @@ def get_report(self, report_type, qs=None):
135148 result = self .get (url , params = qs )
136149 return result
137150
138- # TODO: is disconnect url the same for OAuth 1 and OAuth 2?
139151 def disconnect_account (self ):
140152 """
141153 Disconnect current account from the application
@@ -153,7 +165,6 @@ def change_data_capture(self, entity_string, changed_since):
153165 result = self .get (url , params = params )
154166 return result
155167
156- # TODO: is reconnect url the same for OAuth 1 and OAuth 2?
157168 def reconnect_account (self ):
158169 """
159170 Reconnect current account by refreshing OAuth access tokens
@@ -243,18 +254,13 @@ def post(self, *args, **kwargs):
243254 return self .make_request ("POST" , * args , ** kwargs )
244255
245256 def process_request (self , request_type , url , headers = "" , params = "" , data = "" ):
246- if self .session_manager is None :
257+ if self .session is None :
247258 raise exceptions .QuickbooksException ('No session manager' )
248259
249- if self .session_manager .oauth_version == 2.0 :
250- headers .update ({'Authorization' : 'Bearer ' + self .session_manager .access_token })
251- return self .session_manager .get_session ().request (
252- request_type , url , headers = headers , params = params , data = data )
260+ headers .update ({'Authorization' : 'Bearer ' + self .session .access_token })
253261
254- else :
255- return self .session_manager .get_session ().request (
256- request_type , url , True , self .company_id ,
257- headers = headers , params = params , data = data )
262+ return self .session .request (
263+ request_type , url , headers = headers , params = params , data = data )
258264
259265 def get_single_object (self , qbbo , pk ):
260266 url = "{0}/company/{1}/{2}/{3}/" .format (self .api_url , self .company_id , qbbo .lower (), pk )
@@ -336,8 +342,8 @@ def misc_operation(self, end_point, request_body, content_type='application/json
336342 return results
337343
338344 def download_pdf (self , qbbo , item_id ):
339- if self .session_manager is None :
340- raise exceptions .QuickbooksException ('No session manager ' )
345+ if self .session is None :
346+ raise exceptions .QuickbooksException ('No session' )
341347
342348 url = "{0}/company/{1}/{2}/{3}/pdf" .format (
343349 self .api_url , self .company_id , qbbo .lower (), item_id )
0 commit comments