Skip to content
Snippets Groups Projects
Commit 2378bfee authored by inso's avatar inso
Browse files

Remove old ucoinpy

parent 1e3013f3
No related branches found
No related tags found
No related merge requests found
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Caner Candan <caner@candan.fr>, http://caner.candan.fr
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Caner Candan <caner@candan.fr>, http://caner.candan.fr
#
__all__ = ['api']
__author__ = 'Caner Candan'
__version__ = '0.10.0'
__nonsense__ = 'uCoin'
import requests, logging, json
# import pylibscrypt
logger = logging.getLogger("ucoin")
class ConnectionHandler(object):
"""Helper class used by other API classes to ease passing server connection information."""
def __init__(self, server, port):
"""
Arguments:
- `server`: server hostname
- `port`: port number
"""
self.server = server
self.port = port
def __str__(self):
return 'connection info: %s:%d' % (self.server, self.port)
class API(object):
"""APIRequest 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, connection_handler, module):
"""
Asks a module in order to create the url used then by derivated classes.
Arguments:
- `module`: module name
- `connection_handler`: connection handler
"""
self.module = module
self.connection_handler = connection_handler
self.headers = {}
def reverse_url(self, path):
"""
Reverses the url using self.url and path given in parameter.
Arguments:
- `path`: the request path
"""
server, port = self.connection_handler.server, self.connection_handler.port
url = 'http://%s:%d/%s' % (server, port, self.module)
return url + path
def get(self, **kwargs):
"""wrapper of overloaded __get__ method."""
return self.__get__(**kwargs)
def post(self, **kwargs):
"""wrapper of overloaded __post__ method."""
logger.debug('do some work with')
data = self.__post__(**kwargs)
logger.debug('and send back')
return data
def __get__(self, **kwargs):
"""interface purpose for GET request"""
pass
def __post__(self, **kwargs):
"""interface purpose for POST request"""
pass
def requests_get(self, path, **kwargs):
"""
Requests GET wrapper in order to use API parameters.
Arguments:
- `path`: the request path
"""
response = requests.get(self.reverse_url(path), params=kwargs, headers=self.headers)
if response.status_code != 200:
raise ValueError('status code != 200 => %d (%s)' % (response.status_code, response.text))
return response
def requests_post(self, path, **kwargs):
"""
Requests POST wrapper in order to use API parameters.
Arguments:
- `path`: the request path
"""
response = requests.post(self.reverse_url(path), data=kwargs, headers=self.headers)
if response.status_code != 200:
raise ValueError('status code != 200 => %d (%s)' % (response.status_code, response.text))
return response
def merkle_easy_parser(self, path, begin=None, end=None):
root = self.requests_get(path, leaves='true').json()
for leaf in root['leaves'][begin:end]:
yield self.requests_get(path, leaf=leaf).json()['leaf']
from . import network, blockchain, tx, wot
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Caner Candan <caner@candan.fr>, http://caner.candan.fr
#
from .. import API, logging
logger = logging.getLogger("ucoin/blockchain")
class Blockchain(API):
def __init__(self, connection_handler, module='blockchain'):
super(Blockchain, self).__init__(connection_handler, module)
class Parameters(Blockchain):
"""GET the blockchain parameters used by this node."""
def __get__(self, **kwargs):
return self.requests_get('/parameters', **kwargs).json()
class Membership(Blockchain):
"""POST a Membership document."""
def __post__(self, **kwargs):
assert 'membership' in kwargs
return self.requests_post('/membership', **kwargs).json()
class Block(Blockchain):
"""GET/POST a block from/to the blockchain."""
def __init__(self, connection_handler, number=None):
"""
Use the number parameter in order to select a block number.
Arguments:
- `number`: block number to select
"""
super(Block, self).__init__(connection_handler)
self.number = number
def __get__(self, **kwargs):
assert self.number is not None
return self.requests_get('/block/%d' % self.number, **kwargs).json()
def __post__(self, **kwargs):
assert 'block' in kwargs
assert 'signature' in kwargs
return self.requests_post('/block', **kwargs).json()
class Current(Blockchain):
"""GET, same as block/[number], but return last accepted block."""
def __get__(self, **kwargs):
return self.requests_get('/current', **kwargs).json()
class Hardship(Blockchain):
"""GET hardship level for given member's fingerprint for writing next block."""
def __init__(self, connection_handler, fingerprint):
"""
Use the number parameter in order to select a block number.
Arguments:
- `fingerprint`: member fingerprint
"""
super(Hardship, self).__init__(connection_handler)
self.fingerprint = fingerprint
def __get__(self, **kwargs):
assert self.fingerprint is not None
return self.requests_get('/hardship/%s' % self.fingerprint.upper(), **kwargs).json()
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Caner Candan <caner@candan.fr>, http://caner.candan.fr
#
from .. import API, logging
logger = logging.getLogger("ucoin/network")
class Network(API):
def __init__(self, connection_handler, module='network'):
super(Network, self).__init__(connection_handler, module)
class Peering(Network):
"""GET peering information about a peer."""
def __get__(self, **kwargs):
return self.requests_get('/peering', **kwargs).json()
from . import peering
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Caner Candan <caner@candan.fr>, http://caner.candan.fr
#
from .. import Network, logging
logger = logging.getLogger("ucoin/network/peering")
class Base(Network):
def __init__(self, connection_handler):
super(Base, self).__init__(connection_handler, 'network/peering')
class Peers(Base):
"""GET peering entries of every node inside the currency network."""
def __get__(self, **kwargs):
"""creates a generator with one peering entry per iteration."""
return self.merkle_easy_parser('/peers')
def __post__(self, **kwargs):
assert 'entry' in kwargs
assert 'signature' in kwargs
return self.requests_post('/peers', **kwargs).json()
class Status(Base):
"""POST a network status document to this node in order notify of its status."""
def __post__(self, **kwargs):
assert 'status' in kwargs
assert 'signature' in kwargs
return self.requests_post('/status', **kwargs).json()
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Caner Candan <caner@candan.fr>, http://caner.candan.fr
#
from .. import API, logging
logger = logging.getLogger("ucoin/tx")
class Tx(API):
def __init__(self, connection_handler, module='tx'):
super(Tx, self).__init__(connection_handler, module)
class Process(Tx):
"""POST a transaction."""
def __post__(self, **kwargs):
assert 'transaction' in kwargs
assert 'signature' in kwargs
return self.requests_post('/process', **kwargs).json()
class Sources(Tx):
"""Get transaction sources."""
def __get__(self, **kwargs):
assert self.pubkey is not None
return self.requests_get('/sources/%d' % self.pubkey, **kwargs).json()
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Caner Candan <caner@candan.fr>, http://caner.candan.fr
#
from .. import API, logging
logger = logging.getLogger("ucoin/wot")
class WOT(API):
def __init__(self, connection_handler, module='wot'):
super(WOT, self).__init__(connection_handler, module)
class Add(WOT):
"""POST Public key data."""
def __post__(self, **kwargs):
assert 'pubkey' in kwargs
assert 'self' in kwargs
assert 'other' in kwargs
return self.requests_post('/add', **kwargs).json()
class Lookup(WOT):
"""GET Public key data."""
def __init__(self, connection_handler, search, module='wot'):
super(WOT, self).__init__(connection_handler, module)
self.search = search
def __get__(self, **kwargs):
assert self.search is not None
return self.requests_get('/lookup/%s' % self.search, **kwargs).json()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment