diff --git a/README.md b/README.md index 6a6bdf1..19459a1 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,9 @@ coin = Api('Bitcoin') # Set the return format to UTF-8 (Human readable). coin = Api('Bitcoin', 'text') + +# Use secure SSL connection +coin = Api('Bitcoin', use_https=True) ``` #### Keeping it fast diff --git a/blockr/api.py b/blockr/api.py index 3e85583..ab036db 100644 --- a/blockr/api.py +++ b/blockr/api.py @@ -11,10 +11,10 @@ class Api(ApiService): """ The main API class. Calls the Block API with HTTP GET requests. """ """ Test """ - def __init__(self, currency, data='json'): + def __init__(self, currency, data='json', use_https=False): ApiService.__init__(self, currency) self.data = data - self.url = self.build_url() + self.url = self.build_url(use_https) # Coin API def coin_info(self): diff --git a/blockr/service.py b/blockr/service.py index 6aa8a29..055bdfb 100644 --- a/blockr/service.py +++ b/blockr/service.py @@ -7,6 +7,7 @@ class ApiService(object): currency = None data = None base = None + protocol = None url = 'blockr.io/' version = 'api/v1/' coin = 'coin/info/' @@ -40,7 +41,7 @@ def __init__(self, currency): """Only {1} are supported. The currency: "{0}" not allowed""" .format(self.currency, self.currencies)) - def build_url(self): + def build_url(self, use_https=False): """ Build the url according the users currency, and API version.""" if self.currency == 'bitcoin': self.base = '' @@ -60,7 +61,15 @@ def build_url(self): if self.currency == 'megacoin': self.base = 'mec.' - return 'http://{0}{1}{2}'.format(self.base, self.url, self.version) + if use_https: + self.protocol = "https://" + else: + self.protocol = "http://" + + return '{protocol}{base}{url}{version}'.format(protocol=self.protocol, + base=self.base, + url=self.url, + version=self.version) def execute(self, req): """ The main caller method. HTTP reqs are passed via this method. """ @@ -68,4 +77,3 @@ def execute(self, req): return req.json() if self.data == 'text': return req.text - diff --git a/tests/test_api.py b/tests/test_api.py index f1575c8..ac7c79b 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -35,6 +35,18 @@ def test_digitalcoin_base_url_builder(self): """ Testing digitalcoin root URI """ self.root_url(self.dgc.build_url(), 'http://dgc.blockr.io/api/v1/') + def test_bitcoin_base_url_https_builder(self): + """ Testing bitcoin root URI """ + self.root_url(self.btc.build_url(use_https=True), 'https://blockr.io/api/v1/') + + def test_litecoin_base_url_https_builder(self): + """ Testing litecoin root URI """ + self.root_url(self.ltc.build_url(use_https=True), 'https://ltc.blockr.io/api/v1/') + + def test_digitalcoin_base_url_https_builder(self): + """ Testing digitalcoin root URI """ + self.root_url(self.dgc.build_url(use_https=True), 'https://dgc.blockr.io/api/v1/') + def test_coin_info(self): """ Testing the coin info HTTP call """