File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+ from .baseapi import BaseAPI
3+
4+
5+ class Balance (BaseAPI ):
6+ def __init__ (self , * args , ** kwargs ):
7+ self .month_to_date_balance = None
8+ self .account_balance = None
9+ self .month_to_date_usage = None
10+ self .generated_at = None
11+
12+ super (Balance , self ).__init__ (* args , ** kwargs )
13+
14+ @classmethod
15+ def get_object (cls , api_token ):
16+ """
17+ Class method that will return an Balance object.
18+ """
19+ acct = cls (token = api_token )
20+ acct .load ()
21+ return acct
22+
23+ def load (self ):
24+ # URL https://api.digitalocean.com/customers/my/balance
25+ balance = self .get_data ("customers/my/balance" )
26+
27+ for attr in balance .keys ():
28+ setattr (self , attr , balance [attr ])
29+
30+ def __str__ (self ):
31+ return "<Balance: %s>" % (self .account_balance )
Original file line number Diff line number Diff line change 77from .baseapi import BaseAPI
88from .Account import Account
99from .Action import Action
10+ from .Balance import Balance
1011from .Certificate import Certificate
1112from .Domain import Domain
1213from .Droplet import Droplet
@@ -34,6 +35,12 @@ def get_account(self):
3435 """
3536 return Account .get_object (api_token = self .token )
3637
38+ def get_balance (self ):
39+ """
40+ Returns a Balance object.
41+ """
42+ return Balance .get_object (api_token = self .token )
43+
3744 def get_all_regions (self ):
3845 """
3946 This function returns a list of Region object.
Original file line number Diff line number Diff line change 1414from .Image import Image
1515from .Action import Action
1616from .Account import Account
17+ from .Balance import Balance
1718from .Domain import Domain
1819from .Record import Record
1920from .SSHKey import SSHKey
Original file line number Diff line number Diff line change 1+ {
2+ "month_to_date_balance" : " 23.44" ,
3+ "account_balance" : " 12.23" ,
4+ "month_to_date_usage" : " 11.21" ,
5+ "generated_at" : " 2019-07-09T15:01:12Z"
6+ }
Original file line number Diff line number Diff line change @@ -34,6 +34,25 @@ def test_get_account(self):
3434 self .assertEqual (acct .email_verified , True )
3535 self .assertEqual (acct .status , "active" )
3636
37+ @responses .activate
38+ def test_get_balance (self ):
39+ data = self .load_from_file ('balance/balance.json' )
40+
41+ url = self .base_url + 'customers/my/balance'
42+ responses .add (responses .GET , url ,
43+ body = data ,
44+ status = 200 ,
45+ content_type = 'application/json' )
46+
47+ balance = self .manager .get_balance ()
48+
49+ self .assert_get_url_equal (responses .calls [0 ].request .url , url )
50+ self .assertEqual (balance .token , balance .token )
51+ self .assertEqual (balance .month_to_date_balance , '23.44' )
52+ self .assertEqual (balance .account_balance , '12.23' )
53+ self .assertEqual (balance .month_to_date_usage , '11.21' )
54+ self .assertEqual (balance .generated_at , '2019-07-09T15:01:12Z' )
55+
3756 @responses .activate
3857 def test_auth_fail (self ):
3958 data = self .load_from_file ('errors/unauthorized.json' )
You can’t perform that action at this time.
0 commit comments