Created
January 14, 2014 11:22
-
-
Save canercandan/8416819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from parser import Parser | |
import requests | |
from pprint import pprint | |
URL = 'http://mycurrency.candan.fr:8081' | |
AUTH = False | |
class API: | |
"""API is a class used as an interface. The intermediate derivated classes are the modules and the leaf classes are the API requests.""" | |
def __init__(self, module, url=URL, auth=AUTH): | |
""" | |
Asks a module in order to create the url used then by derivated classes. | |
Arguments: | |
- `module`: module name | |
- `url`: url defining the host and port of the server | |
- `auth`: enables to get multipart/signed messages. | |
""" | |
self.url = '%s/%s' % (url, module) | |
self.headers = {} | |
if auth: self.headers['Accept'] = 'multipart/signed' | |
def reverse_url(self, path): | |
""" | |
Reverses the url using self.url and path given in parameter. | |
Arguments: | |
- `path`: the request path | |
""" | |
return self.url + path | |
def __call__(self): | |
"""interface purpose""" | |
pass | |
def get(self, path): | |
""" | |
Requests GET wrapper in order to use API parameters. | |
Arguments: | |
- `path`: the request path | |
""" | |
return requests.get(self.reverse_url(path), headers=self.headers) | |
def post(self, path): | |
""" | |
Requests POST wrapper in order to use API parameters. | |
Arguments: | |
- `path`: the request path | |
""" | |
return requests.post(self.reverse_url(path), headers=self.headers) | |
class PKS(API): | |
def __init__(self, module='pks'): | |
super().__init__(module) | |
class UCG(API): | |
def __init__(self, module='ucg'): | |
super().__init__(module) | |
class HDC(API): | |
def __init__(self, module='hdc'): | |
super().__init__(module) | |
class Peering(UCG): | |
"""GET peering information about a peer.""" | |
def __call__(self): | |
return self.get('/peering').json() | |
class AmendmentsBase(HDC): | |
def __init__(self): | |
super().__init__('hdc/amendments') | |
class AmendmentsList(AmendmentsBase): | |
"""GET the list of amendments through the previousHash value.""" | |
def __call__(self): | |
"""creates a generator with one amendments per iteration.""" | |
current = self.get('/current').json() | |
yield current | |
while 'previousHash' in current and current['previousHash']: | |
current['previousNumber'] = current['number']-1 | |
current = requests.get(self.reverse_url('/view/%(previousNumber)d-%(previousHash)s/self' % current)).json() | |
yield current | |
class TransactionsBase(HDC): | |
def __init__(self): | |
super().__init__('hdc/transactions') | |
class TransactionsAll(TransactionsBase): | |
"""GET all the transactions stored by this node.""" | |
def __call__(self): | |
"""creates a generator with one transaction per iteration.""" | |
root = self.get('/all?leaves=true').json() | |
for leaf in root['leaves']: | |
yield self.get('/all?leaf=%s' % leaf).json()['leaf'] | |
def action_peering(): | |
pprint(Peering()()) | |
def action_amendments(): | |
for am in AmendmentsList()(): | |
print(am['number']) | |
def action_transactions(): | |
for tx in TransactionsAll()(): | |
print(tx['hash']) | |
if __name__ == '__main__': | |
parser = Parser(description='ucoin client.', verbose='error') | |
parser.add_argument('--peering', '-p', help='get peering', | |
action='store_const', dest='action', const=action_peering) | |
parser.add_argument('--amendments', '-a', help='get amendments list', | |
action='store_const', dest='action', const=action_amendments) | |
parser.add_argument('--transactions', '-t', help='get transactions list', | |
action='store_const', dest='action', const=action_transactions) | |
args = parser() | |
if args.action: args.action() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment