From 95b07ce1ec10e0aeac3429ae9a0bec14cfef7eb8 Mon Sep 17 00:00:00 2001
From: Caner Candan <candan@info.univ-angers.fr>
Date: Tue, 14 Jan 2014 18:51:53 +0100
Subject: [PATCH] =?UTF-8?q?+=20added=20new=20GET=20requests,=20I=C2=A0fini?=
 =?UTF-8?q?shed=20to=20add=20all=20the=20get=20request=20described=20from?=
 =?UTF-8?q?=20the=20HTTP=C2=A0API=C2=A0of=20ucoin?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ucoin/hdc/__init__.py                         |   2 +-
 ucoin/hdc/amendments/__init__.py              |  94 +++++++++++++
 ucoin/hdc/amendments/view.py                  |  50 +++++++
 .../hdc/{amendments.py => coins/__init__.py}  |  31 +++--
 ucoin/hdc/{transactions.py => coins/view.py}  |  12 +-
 ucoin/hdc/transactions/__init__.py            | 126 ++++++++++++++++++
 ucoin/hdc/transactions/sender/__init__.py     |  63 +++++++++
 ucoin/hdc/transactions/sender/issuance.py     |  61 +++++++++
 8 files changed, 418 insertions(+), 21 deletions(-)
 create mode 100644 ucoin/hdc/amendments/__init__.py
 create mode 100644 ucoin/hdc/amendments/view.py
 rename ucoin/hdc/{amendments.py => coins/__init__.py} (54%)
 rename ucoin/hdc/{transactions.py => coins/view.py} (71%)
 create mode 100644 ucoin/hdc/transactions/__init__.py
 create mode 100644 ucoin/hdc/transactions/sender/__init__.py
 create mode 100644 ucoin/hdc/transactions/sender/issuance.py

diff --git a/ucoin/hdc/__init__.py b/ucoin/hdc/__init__.py
index 18ac7c9e..efacc8ef 100644
--- a/ucoin/hdc/__init__.py
+++ b/ucoin/hdc/__init__.py
@@ -22,4 +22,4 @@ class HDC(API):
     def __init__(self, module='hdc'):
         super().__init__(module)
 
-from . import amendments, transactions
+from . import amendments, coins, transactions
diff --git a/ucoin/hdc/amendments/__init__.py b/ucoin/hdc/amendments/__init__.py
new file mode 100644
index 00000000..0ecf677d
--- /dev/null
+++ b/ucoin/hdc/amendments/__init__.py
@@ -0,0 +1,94 @@
+#
+# 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 HDC
+
+class Base(HDC):
+    def __init__(self):
+        super().__init__('hdc/amendments')
+
+class Promoted(Base):
+    """GET the current promoted amendment (amendment which received enough votes to be accepted)."""
+
+    def __init__(self, number=None):
+        """
+        Uses number to fit the result.
+
+        Arguments:
+        - `number`: amendment number
+        """
+
+        super().__init__()
+
+        self.number = number
+
+    def get(self):
+        if not self.number:
+            return self.requests_get('/promoted').json()
+
+        return self.requests_get('/promoted/%d' % self.number).json()
+
+class Current(Promoted):
+    """Alias of amendments/promoted."""
+
+    pass
+
+class List(Base):
+    """GET the list of amendments through the previousHash value."""
+
+    def get(self):
+        """creates a generator with one amendment per iteration."""
+
+        current = self.requests_get('/promoted').json()
+        yield current
+
+        while 'previousHash' in current and current['previousHash']:
+            current = self.requests_get('/promoted/%d' % (current['number']-1)).json()
+            yield current
+
+class CurrentVotes(Base):
+    """GET the votes that legitimate the current amendment."""
+
+    def get(self):
+        return self.merkle_easy_parser('/current/votes')
+
+class Votes(Base):
+    """GET an index of votes received by this node."""
+
+    def __init__(self, amendment_id=None):
+        """
+        Uses amendment_id to fit the result.
+
+        Arguments:
+        - `amendment_id`: amendment id
+        """
+
+        super().__init__()
+
+        self.amendment_id = amendment_id
+
+    def get(self):
+        if not self.amendment_id:
+            return self.requests_get('/votes').json()
+
+        return self.merkle_easy_parser('/votes/%s' % self.amendment_id)
+
+    def post(self):
+        pass
+
+from . import view
diff --git a/ucoin/hdc/amendments/view.py b/ucoin/hdc/amendments/view.py
new file mode 100644
index 00000000..61ac7f63
--- /dev/null
+++ b/ucoin/hdc/amendments/view.py
@@ -0,0 +1,50 @@
+#
+# 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 HDC
+
+class View(HDC):
+    def __init__(self, amendment_id):
+        super().__init__('hdc/amendments/view/%s' % amendment_id)
+
+class Members(View):
+    """GET the members present in the Community for this amendment."""
+
+    def get(self):
+        return self.merkle_easy_parser('/members')
+
+class Self(View):
+    """Shows the raw data of the amendment [AMENDMENT_ID]."""
+
+    def get(self):
+        return self.requests_get('/self').json()
+
+class Voters(View):
+    """GET the voters listed in this amendment."""
+
+    def get(self):
+        return self.merkle_easy_parser('/voters')
+
+class Signatures(View):
+    """GET the signatures of the Community listed in this amendment.
+
+    This URL should give the same result as hdc/amendments/votes/[PREVIOUS_AMENDEMENT_ID] if all votes present in this URL were taken in count as signatures for this AMENDMENT_ID.
+    """
+
+    def get(self):
+        return self.merkle_easy_parser('/signatures')
diff --git a/ucoin/hdc/amendments.py b/ucoin/hdc/coins/__init__.py
similarity index 54%
rename from ucoin/hdc/amendments.py
rename to ucoin/hdc/coins/__init__.py
index 7ff67704..47250952 100644
--- a/ucoin/hdc/amendments.py
+++ b/ucoin/hdc/coins/__init__.py
@@ -16,22 +16,27 @@
 # Caner Candan <caner@candan.fr>, http://caner.candan.fr
 #
 
-from . import HDC
+from .. import HDC
 
-class Base(HDC):
-    def __init__(self):
-        super().__init__('hdc/amendments')
+class Coins(HDC):
+    def __init__(self, pgp_fingerprint):
+        super().__init__('hdc/coins/%s' % pgp_fingerprint)
 
-class List(Base):
-    """GET the list of amendments through the previousHash value."""
+class List(Coins):
+    """GET a list of coins owned by the given [PGP_FINGERPRINT]."""
 
     def get(self):
-        """creates a generator with one amendment per iteration."""
+        return self.requests_get('/list').json()
 
-        current = self.requests_get('/current').json()
-        yield current
+class View(Coins):
+    """GET the ownership state of the coin [COIN_NUMBER] issued by [PGP_FINGERPRINT]."""
 
-        while 'previousHash' in current and current['previousHash']:
-            current['previousNumber'] = current['number']-1
-            current = self.requests_get('/view/%(previousNumber)d-%(previousHash)s/self' % current).json()
-            yield current
+    def __init__(self, pgp_fingerprint, coin_number):
+        super().__init__(pgp_fingerprint)
+
+        self.coin_number = coin_number
+
+    def get(self):
+        return self.requests_get('/view/%d' % self.coin_number).json()
+
+from . import view
diff --git a/ucoin/hdc/transactions.py b/ucoin/hdc/coins/view.py
similarity index 71%
rename from ucoin/hdc/transactions.py
rename to ucoin/hdc/coins/view.py
index 8f25dea4..f30f1c65 100644
--- a/ucoin/hdc/transactions.py
+++ b/ucoin/hdc/coins/view.py
@@ -19,13 +19,11 @@
 from . import HDC
 
 class Base(HDC):
-    def __init__(self):
-        super().__init__('hdc/transactions')
+    def __init__(self, pgp_fingerprint, coin_number):
+        super().__init__('hdc/coins/%s/view/%d' % (pgp_fingerprint, coin_number))
 
-class All(Base):
-    """GET all the transactions stored by this node."""
+class History(Base):
+    """GET a transaction history of the coin [COIN_NUMBER] issued by [PGP_FINGERPRINT]."""
 
     def get(self):
-        """creates a generator with one transaction per iteration."""
-
-        return self.merkle_easy_parser('/all')
+        return self.requests_get('/history').json()
diff --git a/ucoin/hdc/transactions/__init__.py b/ucoin/hdc/transactions/__init__.py
new file mode 100644
index 00000000..c43f5bfe
--- /dev/null
+++ b/ucoin/hdc/transactions/__init__.py
@@ -0,0 +1,126 @@
+#
+# 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 HDC
+
+class Base(HDC):
+    def __init__(self):
+        super().__init__('hdc/transactions')
+
+class Process(Base):
+    """POST a transaction."""
+
+    def __init__(self, transaction, signature):
+        """
+        Arguments:
+        - `transaction`: The raw transaction.
+        - `signature`: The signature of the transaction.
+        """
+
+        super().__init__()
+
+        self.transaction = transaction
+        self.signature = signature
+
+    def post(self):
+        pass
+
+class All(Base):
+    """GET all the transactions stored by this node."""
+
+    def get(self):
+        """creates a generator with one transaction per iteration."""
+
+        return self.merkle_easy_parser('/all')
+
+class Keys(Base):
+    """GET PGP keys for which some transactions have been recoreded by this node (sent and received)."""
+
+    def get(self):
+        """creates a generator with one key per iteration."""
+
+        return self.merkle_easy_parser('/keys')
+
+class Last(Base):
+    """GET the last received transaction."""
+
+    def __init__(self, count=None):
+        """
+        Arguments:
+        - `count`: Integer indicating to retrieve the last [COUNT] transactions.
+        """
+
+        super().__init__()
+
+        self.count = count
+
+    def get(self):
+        if not self.count:
+            return self.requests_get('/last').json()
+
+        return self.requests_get('/last/%d' % self.count).json()
+
+class Sender(Base):
+    """GET all the transactions sent by this sender and stored by this node (should contain all transactions of the sender)."""
+
+    def __init__(self, pgp_fingerprint):
+        """
+        Arguments:
+        - `pgp_fingerprint`: PGP fingerprint of the key we want to see sent transactions.
+        """
+
+        super().__init__()
+
+        self.pgp_fingerprint = pgp_fingerprint
+
+    def get(self):
+        return self.merkle_easy_parser('/sender/%s' % self.pgp_fingerprint)
+
+class Recipient(Base):
+    """GET all the transactions received for this recipient stored by this node."""
+
+    def __init__(self, pgp_fingerprint):
+        """
+        Arguments:
+        - `pgp_fingerprint`: PGP fingerprint of the key we want to see sent transactions.
+        """
+
+        super().__init__()
+
+        self.pgp_fingerprint = pgp_fingerprint
+
+    def get(self):
+        return self.merkle_easy_parser('/recipient/%s' % self.pgp_fingerprint)
+
+class View(Base):
+    """GET the transaction of given TRANSACTION_ID."""
+
+    def __init__(self, transaction_id):
+        """
+        Arguments:
+        - `transaction_id`: The transaction unique identifier.
+        """
+
+        super().__init__()
+
+        self.transaction_id = transaction_id
+
+    def get(self):
+        return self.requests_get('/view/%s' % self.transaction_id).json()
+
+from . import sender
diff --git a/ucoin/hdc/transactions/sender/__init__.py b/ucoin/hdc/transactions/sender/__init__.py
new file mode 100644
index 00000000..5ba9249b
--- /dev/null
+++ b/ucoin/hdc/transactions/sender/__init__.py
@@ -0,0 +1,63 @@
+#
+# 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 HDC
+
+class Base(HDC):
+    """Get the last received transaction of a PGP key."""
+
+    def __init__(self, pgp_fingerprint):
+        """
+        Arguments:
+        - `pgp_fingerprint`: PGP fingerprint of the key we want to see sent transactions.
+        """
+
+        super().__init__('hdc/transactions/sender/%s' % pgp_fingerprint)
+
+class Last(Base):
+    """Get the last received transaction of a PGP key."""
+
+    def __init__(self, pgp_fingerprint, count=None):
+        """
+        Arguments:
+        - `count`: Integer indicating to retrieve the last [COUNT] transactions.
+        """
+
+        super().__init__(pgp_fingerprint)
+
+        self.count = count
+
+    def get(self):
+        if not self.count:
+            return self.requests_get('/last').json()
+
+        return self.requests_get('/last/%d' % self.count).json()
+
+class Transfer(Base):
+    """GET all transfer transactions sent by this sender and stored by this node (should contain all transfert transactions of the sender)."""
+
+    def get(self):
+        return self.merkle_easy_parser('/transfert')
+
+class Issuance(Base):
+    """GET all issuance transactions (forged coins) sent by this sender and stored by this node (should contain all issuance transactions of the sender)."""
+
+    def get(self):
+        return self.merkle_easy_parser('/issuance')
+
+from . import issuance
diff --git a/ucoin/hdc/transactions/sender/issuance.py b/ucoin/hdc/transactions/sender/issuance.py
new file mode 100644
index 00000000..f7380df2
--- /dev/null
+++ b/ucoin/hdc/transactions/sender/issuance.py
@@ -0,0 +1,61 @@
+#
+# 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 HDC
+
+class Base(HDC):
+    """Get the received issuance transaction of a PGP key."""
+
+    def __init__(self, pgp_fingerprint):
+        """
+        Arguments:
+        - `pgp_fingerprint`: PGP fingerprint of the key we want to see sent transactions.
+        """
+
+        super().__init__('hdc/transactions/sender/%s/issuance' % pgp_fingerprint)
+
+class Last(Base):
+    """GET the last received issuance transaction of a PGP key."""
+
+    def get(self):
+        return self.requests_get('/last').json()
+
+class Fusion(Base):
+    """GET all fusion transactions sent by this sender and stored by this node (should contain all fusion transactions of the sender)."""
+
+    def get(self):
+        return self.merkle_easy_parser('/fusion')
+
+class Dividend(Base):
+    """GET all dividend transactions (issuance of new coins) sent by this sender and stored by this node (should contain all dividend transactions of the sender)."""
+
+    def __init__(self, pgp_fingerprint, am_number=None):
+        """
+        Arguments:
+        - `am_number`: amendment number
+        """
+
+        super().__init__(pgp_fingerprint)
+
+        self.am_number = am_number
+
+    def get(self):
+        if not self.am_number:
+            return self.merkle_easy_parser('/dividend')
+
+        return self.merkle_easy_parser('/dividend/%d' % self.am_number)
-- 
GitLab