@@ -26,15 +26,7 @@ def __init__(self, hostname, auth_token, check_connection=True):
2626 else hostname [:- 1 ]
2727 ) # Remove trailing backslash
2828 self .api_url = self .hostname + "/api/v1/" if hostname else self .hostname
29- self .api_test = self ._test_api () if check_connection else False
30-
31- def _test_api (self ):
32- """Tests API connection."""
33- try :
34- _ = self .get_about_user ()
35- return True
36- except :
37- return False
29+ self .api_test = self .test_api_connection () if check_connection else False
3830
3931 def _post (self , endpoint , payload ):
4032 """Handles general POST requests."""
@@ -51,22 +43,31 @@ def _post(self, endpoint, payload):
5143
5244 return response
5345
54- def _get (self , endpoint , params = {}, cache = False ):
46+ def _get (self , endpoint , params = {}, cache = False , request_raw = False , timeout = 2 ):
5547 """Handles general GET requests."""
5648
5749 with self .rc .cache_disabled () if not cache else nullcontext ():
5850 response = self .rc .get (
5951 "{}{}" .format (self .api_url , endpoint ),
6052 params = params ,
6153 headers = self .headers ,
54+ timeout = timeout ,
6255 )
6356
64- return response .json ()
57+ return response .json () if not request_raw else response
6558
66- def get_budgets (self ):
67- """Returns budgets of the user."""
59+ def test_api_connection (self ):
60+ """Tests API connection."""
61+ try :
62+ _ = self .get_about_user ()
63+ return True
64+ except :
65+ return False
6866
69- return self ._get ("budgets" )
67+ def get_about_user (self ):
68+ """Returns user information."""
69+
70+ return self ._get ("about/user" )
7071
7172 def get_accounts (
7273 self , account_type = "asset" , cache = False , pagination = False , limit = None
@@ -96,6 +97,11 @@ def get_accounts(
9697
9798 return pages
9899
100+ def get_budgets (self ):
101+ """Returns budgets of the user."""
102+
103+ return self ._get ("budgets" )
104+
99105 def get_autocomplete_accounts (self , limit = 20 ):
100106 """Returns all user accounts."""
101107 acc_data = self .get_accounts (
@@ -107,15 +113,8 @@ def get_autocomplete_accounts(self, limit=20):
107113
108114 return account_names
109115
110- def get_about_user (self ):
111- """Returns user information."""
112-
113- return self ._get ("about/user" )
114-
115116 def create_transaction (self , transaction : Transaction ):
116117 """Creates a new transaction.
117- data:
118- pd.DataFrame
119118
120119 `Amount, Description, Source account, Destination account, Category, Budget`
121120 Example:
@@ -127,18 +126,23 @@ def create_transaction(self, transaction: Transaction):
127126 -> `5, Large Mocha, Cash, , , UCO Bank`
128127 """
129128
130- trans_data = transaction . to_dict ( remove_none = True , api_safe = True )
129+ payload = self . payload_formatter ( transaction )
131130
132- header = {k : v for k , v in trans_data .items () if k .startswith ("header__" )}
131+ return self ._post (endpoint = "transactions" , payload = payload )
132+
133+ def payload_formatter (self , transaction ):
134+ trans_data = transaction .to_dict (remove_none = True , api_safe = True )
135+ header = {
136+ k .replace ("header__" , "" ): v
137+ for k , v in trans_data .items ()
138+ if k .startswith ("header__" )
139+ }
133140 body = {
134141 "transactions" : [
135142 {k : v for k , v in trans_data .items () if not k .startswith ("header__" )}
136143 ]
137144 }
138-
139- payload = {** header , ** body }
140-
141- return self ._post (endpoint = "transactions" , payload = payload )
145+ return {** header , ** body }
142146
143147 @staticmethod
144148 def refresh_api (configs ):
0 commit comments