Skip to content
Discussions/App Development/DAML HUb facing issues when DAZL code is calling outside apisForum ↗

DAML HUb facing issues when DAZL code is calling outside apis

App Development3 posts553 views5 likesLast activity Oct 2021
AR
aryan_bOP
Oct 2021

Hello Guys,

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?

import csv

import json

import logging

import os

from os.path import dirname, join

import dazl

import requests

from requests.structures import CaseInsensitiveDict

dazl.setup_default_logger(logging.INFO)

def main():

    url = os.getenv('DAML_LEDGER_URL')

    

    party = "ledger_id"

    

    network = dazl.Network()

    network.set_config(url=url)

    client = network.aio_party(party)

    def check_stuff():

        # ADIA api should be called here

        logging.info(f'A22222222222222222222222222222222')

        x = requests.get('https://w3schools.com')#, proxies = proxies)

        logging.info(x)

        logging.info("A333333333333333333333333333333333")

        return True

    @client.ledger_ready()

    def ensure_setup(event):

        logging.info(f"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaa checking here")

        a = check_stuff()  

        logging.info("A44444444444444444444444444444444")

 

    
    

    network.run_forever()

if __name__ == '__main__':

    main()
DT
dtanabe
Oct 2021

Unfortunately we do not allow bots to communicate with the outside world for security reasons.

Please see this post for a more detailed explanation:

Hi Ivan, Welcome to the forums! When it comes to connecting to external sources, Daml Hub supports integrations. A standard integration will model the surface of a 3rd-party API through Daml contracts, which can then be acted upon on ledger. A good example of this is the Exberry integration, which is also used by the Marketplace app - https://github.com/digital-asset/daml-dit-integration-exberry The caveat here is that integrations can only be published by Digital Asset for security reasons. …
MI
Michael_Schaeffer
Oct 2021

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:

  1. Write an externally hosted service that uses an API already offered by Daml Hub to manage the external network calls.
  2. 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.

GitHub

GitHub - digital-asset/daml-dit-integration-coindesk

Contribute to digital-asset/daml-dit-integration-coindesk development by creating an account on GitHub.

The code to issue the web request is here:

github.com

digital-asset/daml-dit-integration-coindesk/blob/7ce770124919f1297c226c4edbe845340d7b6321/src/core_int/integration_coindesk_price.py#L81

    
      
  1. events: 'IntegrationEvents'):
  2. # Request/Response style integration
  3. @events.ledger.contract_created('CoinDesk.PriceRequest:PriceRequest')
  4. async def on_contract_created(event):
  5. LOG.debug('Noticed new price request: %r', event)
  6. currencyCode = event.cdata['currencyCode']
  7. resp = await issue_coindesk_request(currencyCode)
  8. if resp['success']:
  9. return exercise(event.cid, 'PriceRequest_RespondSuccess', {
  10. 'updatedAt': resp['updatedAt'],
  11. 'rate': resp['rate']
  12. })
  13. else:
  14. return exercise(event.cid, 'PriceRequest_RespondFailure', {
  15. 'httpStatusCode': resp['httpStatusCode'],
  16. 'httpResponseBody': resp['httpResponseBody']

(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.

GitHub

GitHub - digital-asset/daml-dit-if

Contribute to digital-asset/daml-dit-if development by creating an account on GitHub.

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.

GitHub

GitHub - digital-asset/daml-dit-ddit

Contribute to digital-asset/daml-dit-ddit development by creating an account on GitHub.

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.

← Back to Discussions