I am using the DAZL library to write a python bot and need to call and thirdparty api when the ledger is ready. In our company we set the proxy and call the third party api. It works for us. But we want to demo using the DAML Hub.
Here is the code snippet of the python code. I am replacing the api with a get to w3schools which is also facing the similar issue.
Can someone please help me?
from os.path import dirname, join
from requests.structures import CaseInsensitiveDict
url = os.getenv('DAML_LEDGER_URL')
party = "ledger_id"
network = dazl.Network()
client = network.aio_party(party)
# ADIA api should be called here
x = requests.get('https://w3schools.com')#, proxies = proxies)
logging.info(f"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaa checking here")
a = check_stuff()
if __name__ == '__main__':
Following up on what @dtanabe and @Alex_Matson have both said, Bots in Daml Hub do not have access to the external network. This is intentional, and done for security reasons. What you’ll see when you attempt to run your bot is that the requests.get will fail because it cannot reach the outside world.
This leaves two options for meeting your requirement:
Write an externally hosted service that uses an API already offered by Daml Hub to manage the external network calls.
Write an ‘integration’ instead of a bot.
For the first option, Daml Hub already offers the HTTP JSON API for accessing data on a ledger. This lets externally hosted software authenticate using a JWT and issue requests for ledger data and issue commands to the ledger. I’ve linked to the hub documentation above, but the general structure of this approach would be to write a bot that would connect to the ledger using the JSON API and issue whatever requests are necessary to external systems. This would not be subject to the networking limitations above - the code issuing the network requests would not be hosted by Daml Hub.
The second option is to write an integration. Integrations are visible under the ‘browse integrations’ tab in Daml Hub, and can be deployed to a ledger for the purpose of connecting that ledger to external systems. Internally, Integrations are bots, but there’s a specific API they follow to interface with DAML Hub, and they need to be deployed by users with specific access. If this is the path you choose to follow, we can help, but in the meantime, here are a few links to show what an integration looks like in concrete terms.
We have an integration with the Coindesk API that shows how ledgers can issue web requests in response to ledger events and then store the data on ledger. This is a good illustration of the structure and patterns involved in writing a Daml Hub integration.
The code to issue the web request is here:
(You can see how it’s triggered by a ledger event.)
Integrations are also written with a specific framework, that’s a wrapper around Dazl.
The integration framework allows for health and status monitoring and manages the connection of the integration with Daml Hub itself.
There is also a build tool that makes it easy to build integrations and run them locally against a sandbox for testing.
The last two repositories have documentation in their readme that goes into more detail than I can in this post about how these pieces fit together. I’m also happy to field any other questions as well.