Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions blockr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 11 additions & 3 deletions blockr/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ApiService(object):
currency = None
data = None
base = None
protocol = None
url = 'blockr.io/'
version = 'api/v1/'
coin = 'coin/info/'
Expand Down Expand Up @@ -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 = ''
Expand All @@ -60,12 +61,19 @@ 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. """
if self.data == 'json':
return req.json()
if self.data == 'text':
return req.text

12 changes: 12 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """

Expand Down