[enh] #390, #396: Implement generic send_document()
Use it in cert, membership, and tx cmds With urllib, the 200 error is no longer returned but an exception TODO: write test_send_document()
... | @@ -13,6 +13,8 @@ | ... | @@ -13,6 +13,8 @@ |
# You should have received a copy of the GNU Affero General Public License | # You should have received a copy of the GNU Affero General Public License | ||
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. | # along with Silkaj. If not, see <https://www.gnu.org/licenses/>. | ||
import urllib | |||
from duniterpy.api.client import Client | from duniterpy.api.client import Client | ||
from silkaj.constants import G1_DEFAULT_ENDPOINT, G1_TEST_DEFAULT_ENDPOINT | from silkaj.constants import G1_DEFAULT_ENDPOINT, G1_TEST_DEFAULT_ENDPOINT | ||
... | @@ -63,3 +65,13 @@ class EndPoint: | ... | @@ -63,3 +65,13 @@ class EndPoint: |
class ClientInstance: | class ClientInstance: | ||
def __init__(self): | def __init__(self): | ||
self.client = Client(EndPoint().BMA_ENDPOINT) | self.client = Client(EndPoint().BMA_ENDPOINT) | ||
def send_document(bma_path, document): | |||
client = ClientInstance().client | |||
doc_name = document.__class__.__name__ | |||
try: | |||
client(bma_path, document.signed_raw()) | |||
print(f"{doc_name} successfully sent") | |||
except urllib.error.HTTPError as e: | |||
print(f"Error while publishing {doc_name.lower()}: {e}") | |||
|