Skip to content

Commit 7a63c05

Browse files
author
Vedran
committed
Sphinx docstrings for the funtions
1 parent 4e8dc12 commit 7a63c05

File tree

9 files changed

+222
-162
lines changed

9 files changed

+222
-162
lines changed

blockchain/blockchaindata.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
"""This module corresponds to functionality documented
2+
at https://blockchain.info/api/blockchain_api
3+
4+
"""
5+
16
import util
27
import json
38

49
def get_block(block_id, api_code = None):
10+
"""Get a single block based on a block index or hash.
11+
12+
:param str block_id: block hash or index to look up
13+
:param str api_code: Blockchain.info API code (optional)
14+
:return: an instance of :class:`Block` class
15+
"""
16+
517
resource = 'rawblock/' + block_id
618
if api_code is not None:
719
resource += '?api_code=' + api_code
@@ -10,6 +22,13 @@ def get_block(block_id, api_code = None):
1022
return Block(json_response)
1123

1224
def get_tx(tx, api_code = None):
25+
"""Get a single transaction based on a transaction index or hash.
26+
27+
:param str block_id: transaction hash or index to look up
28+
:param str api_code: Blockchain.info API code (optional)
29+
:return: an instance of :class:`Transaction` class
30+
"""
31+
1332
resource = 'rawtx/' + tx
1433
if api_code is not None:
1534
resource += '?api_code=' + api_code
@@ -18,6 +37,13 @@ def get_tx(tx, api_code = None):
1837
return Transaction(json_response)
1938

2039
def get_block_height(height, api_code = None):
40+
"""Get an array of blocks at the specified height.
41+
42+
:param int height: block height to look up
43+
:param str api_code: Blockchain.info API code (optional)
44+
:return: an array of :class:`Block` objects
45+
"""
46+
2147
resource = 'block-height/{0}?format=json'.format(height)
2248
if api_code is not None:
2349
resource += '&api_code=' + api_code
@@ -26,6 +52,13 @@ def get_block_height(height, api_code = None):
2652
return [Block(b) for b in json_response['blocks']]
2753

2854
def get_address(address, api_code = None):
55+
"""Get data for a single address.
56+
57+
:param str address: address to look up
58+
:param str api_code: Blockchain.info API code (optional)
59+
:return: an instance of :class:`Address` class
60+
"""
61+
2962
resource = 'rawaddr/' + address
3063
if api_code is not None:
3164
resource += '?api_code=' + api_code
@@ -34,6 +67,13 @@ def get_address(address, api_code = None):
3467
return Address(json_response)
3568

3669
def get_unspent_outputs(address, api_code = None):
70+
"""Get unspent outputs for a single address.
71+
72+
:param str address: address to look up
73+
:param str api_code: Blockchain.info API code (optional)
74+
:return: an array of :class:`UnspentOutput` objects
75+
"""
76+
3777
resource = 'unspent?active=' + address
3878
if api_code is not None:
3979
resource += '&api_code=' + api_code
@@ -42,6 +82,12 @@ def get_unspent_outputs(address, api_code = None):
4282
return [UnspentOutput(o) for o in json_response['unspent_outputs']]
4383

4484
def get_latest_block(api_code = None):
85+
"""Get the latest block on the main chain.
86+
87+
:param str api_code: Blockchain.info API code (optional)
88+
:return: an instance of :class:`LatestBlock` class
89+
"""
90+
4591
resource = 'latestblock'
4692
if api_code is not None:
4793
resource += '?api_code=' + api_code
@@ -50,6 +96,12 @@ def get_latest_block(api_code = None):
5096
return LatestBlock(json_response)
5197

5298
def get_unconfirmed_tx(api_code = None):
99+
"""Get a list of currently unconfirmed transactions.
100+
101+
:param str api_code: Blockchain.info API code (optional)
102+
:return: an array of :class:`Transaction` objects
103+
"""
104+
53105
resource = 'unconfirmed-transactions?format=json'
54106
if api_code is not None:
55107
resource += '&api_code=' + api_code
@@ -58,6 +110,15 @@ def get_unconfirmed_tx(api_code = None):
58110
return [Transaction(t) for t in json_response['txs']]
59111

60112
def get_blocks(time = None, pool_name = None, api_code = None):
113+
"""Get a list of blocks for a specific day or mining pool.
114+
Both parameters are optional but at least one is required.
115+
116+
:param int time: time in milliseconds
117+
:param str pool_name: name of the mining pool
118+
:param str api_code: Blockchain.info API code (optional)
119+
:return: an array of :class:`SimpleBlock` objects
120+
"""
121+
61122
resource = 'blocks/{0}?format=json'
62123
if api_code is not None:
63124
resource += '&api_code=' + api_code
@@ -73,6 +134,12 @@ def get_blocks(time = None, pool_name = None, api_code = None):
73134
return [SimpleBlock(b) for b in json_response['blocks']]
74135

75136
def get_inventory_data(hash, api_code = None):
137+
"""Get inventory data.
138+
139+
:param str hash: tx or block hash
140+
:param str api_code: Blockchain.info API code (optional)
141+
:return: an instance of :class:`InventoryData` class
142+
"""
76143
resource = 'inv/{0}?format=json'.format(hash)
77144
if api_code is not None:
78145
resource += '&api_code=' + api_code

blockchain/createwallet.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
"""This module corresponds to functionality documented
2+
at https://blockchain.info/api/create_wallet
3+
14
"""
2-
TODO
3-
"""
5+
46
import util
57
import json
6-
8+
79
def create_wallet(password, api_code, priv = None, label = None, email = None):
10+
"""Create a new Blockchain.info wallet. It can be created containing a
11+
pre-generated private key or will otherwise generate a new private key.
12+
13+
:param str password: password for the new wallet. At least 10 characters.
14+
:param str api_code: API code with create wallets permission
15+
:param str priv: private key to add to the wallet (optional)
16+
:param str label: label for the first address in the wallet (optional)
17+
:param str email: email to associate with the new wallet (optional)
18+
:return: an instance of :class:`WalletResponse` class
19+
"""
20+
821
params = { 'password': password, 'api_code': api_code }
922
if priv is not None:
1023
params['priv'] = priv

blockchain/exceptions.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
class ValidationException(Exception):
2-
pass
3-
41
class APIException(Exception):
52
def __init__(self, message, code):
63
Exception.__init__(self, message)

blockchain/exchangerates.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
"""This module corresponds to functionality documented at
2+
https://blockchain.info/api/exchange_rates_api
3+
"""
4+
15
import urllib2
26
import util
37
import json
48

5-
class Currency:
6-
def __init__(self, last, buy, sell, symbol, p15min):
7-
self.last = last
8-
self.buy = buy
9-
self.sell = sell
10-
self.symbol = symbol
11-
self.p15min = p15min
12-
13-
def ticker(api_code = None):
9+
def get_ticker(api_code = None):
10+
"""Call the 'ticker' method and return a dictionary
11+
of :class:`Currency` objects.
12+
13+
:param str api_code: Blockchain.info API code (optional)
14+
:return: a dictionary in the format of ccy_symbol(str):currency(:class:`Currency`)
15+
"""
16+
1417
response = util.call_api('ticker' if api_code is None else 'ticker?api_code=' + api_code)
1518
json_response = json.loads(response)
1619
ticker = {}
@@ -25,7 +28,23 @@ def ticker(api_code = None):
2528
return ticker
2629

2730
def to_btc(ccy, value, api_code = None):
31+
"""Call the 'tobtc' method and convert x value in the provided currency to BTC.
32+
33+
:param str ccy: currency code
34+
:param int value: value to convert
35+
:param str api_code: Blockchain.info API code
36+
:return: the value in BTC
37+
"""
38+
2839
res = 'tobtc?currency={0}&value={1}'.format(ccy, value)
2940
if api_code is not None:
3041
res += '&api_code=' + api_code
31-
return util.call_api(res)
42+
return util.call_api(res)
43+
44+
class Currency:
45+
def __init__(self, last, buy, sell, symbol, p15min):
46+
self.last = last
47+
self.buy = buy
48+
self.sell = sell
49+
self.symbol = symbol
50+
self.p15min = p15min

blockchain/receive.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
"""This module corresponds to functionality documented
2+
at https://blockchain.info/api/api_receive
3+
"""
4+
15
import urllib2
26
import util
37
import json
48

59
class PaymentResponse:
10+
611
def __init__(self, fee, dest, input, callback):
712
self.fee = fee
813
self.destination_address = dest
914
self.input_address = input
1015
self.callback_url = callback
1116

1217
def receive(dest_addr, callback, api_code = None):
18+
"""Call the 'api/receive' endpoint and create a forwarding address.
19+
20+
:param str dest_addr: destination address where the payment should be sent
21+
:param str callback: callback URI that will be called upon payment
22+
:param str api_code: Blockchain.info API code (optional)
23+
:return: an instance of :class:`PaymentResponse` class
24+
"""
25+
1326
params = { 'method': 'create', 'address': dest_addr, 'callback': callback }
1427
if api_code is not None:
1528
params['api_code'] = api_code

blockchain/statistics.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
"""This module corresponds to functionality documented
2+
at https://blockchain.info/api/charts_api
3+
4+
"""
5+
16
import util
27
import json
38

49
def get(api_code = None):
10+
"""Get network statistics.
11+
12+
:param str api_code: Blockchain.info API code (optional)
13+
:return: an instance of :class:`Stats` class
14+
"""
15+
516
resource = 'stats?format=json'
617
if api_code is not None:
718
resource += '&api_code=' + api_code

blockchain/util.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import urllib
22
import urllib2
3-
import re
43

54
from .exceptions import *
65
from validation import validate
@@ -10,24 +9,7 @@
109

1110
def call_api(resource, data = None):
1211
try:
13-
#print urllib.urlencode(data)
1412
return urllib2.urlopen(BASE_URL + resource,
1513
None if data is None else urllib.urlencode(data)).read()
1614
except urllib2.HTTPError as e:
17-
raise APIException(e.read(), e.code)
18-
19-
def validate_address(address):
20-
if not validate(address):
21-
raise ValidationException("Invalid bitcoin address")
22-
23-
def validate_url(url):
24-
regex = re.compile(
25-
r'^(?:[a-z0-9\.\-]*)://' # scheme is validated separately
26-
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}(?<!-)\.?)|' # domain...
27-
r'localhost|' # localhost...
28-
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
29-
r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
30-
r'(?::\d+)?' # optional port
31-
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
32-
if url is None or not regex.search(url):
33-
raise ValidationException("Invalid URL")
15+
raise APIException(e.read(), e.code)

0 commit comments

Comments
 (0)