FAQ: Publish multiple REST APIs via domino

josh.mineroffjosh.mineroff Member, Moderator, Domino Posts: 8 mod
edited October 2019 in Q&A

Is it possible in domino to have multiple endpoints and we can call any of it from outside? I tried Domino APP with simple hello world using python flask and it worked fine with hello world as output.

My other endpoint is,

http://<url>/hello - but this doesn't work.

My code is simple, as below:

from flask import Flask

app = Flask(__name__)

@app.route("/")

def hello():

    return "Hello World!"

@app.route("/hello")

def hello2():

    return "Hello World2"

if __name__ == '__main__':

app.run(port=8888, host='0.0.0.0', debug=True)

Josh Mineroff | Field Engineer

Comments

  • akshay.ambekarakshay.ambekar Administrator, Moderator, Domino Posts: 19 admin
    edited October 2019

    Thanks for the wonderful question! You can use Model APIs in Domino to publish multiple REST APIs. Models APIs are better than Apps in the sense that the exposed endpoints that can be autoscaled by Domino to provide programmatic access to code.

    To achieve this, please follow the below steps to test it:

    1. Create a file named multiplemodelroutes.py in your project. Add the below code to this file:

    # Import dependencies
    import random
    
    class multipleroutes: 
        # Return a random floating point number N such that start <= N <= stop for start <= stop and stop <= N <= start for stop < start.
        def random_number(self, **kwargs):
            start = kwargs.get('start', 1)
            stop = kwargs.get('stop', 100)
            return random.uniform(start, stop)
    
        # Be Nice. Say hello to your new friend. Return a friendly greeting
        def say_hello(self, **kwargs):
            message = kwargs.get('msg', 'hello there!')
            return message
     
    # Define a main function to accept other function names and parameters
    def my_model(func_name, **kwargs):
        module_name = globals()['multipleroutes']()
        method_to_call = getattr(module_name, func_name)
        return method_to_call(**kwargs)
    

    2. a. Click Publish > Model APIs > New Model.

      b. Provide a meaningful model name and click next.

      c. In the model setup page, add multiplemodelroutes.py as the file name, and my_model as the function to invoke. Click Create Model.

    3. Once your model builds and launches successfully, navigate to Overview > Tester. Now you can invoke random_number or say_hello function by passing the appropriate function name and parameters. For example:

      a. If you want to call random_number, enter this and click Send 

    { 
      "data": { 
        "func_name":"random_number", 
        "start":20, 
        "stop":300 
      } 
    } 
    

      b. If you want to call say_hello, enter this and click Send

    { 
      "data": { 
        "func_name":"say_hello", 
        "msg":"Hello User!" 
      } 
    } 
    

    This way, by passing the function name and arguments, we can achieve the multiple routing functionality using a Model API. You can get more information about models in Domino here: https://support.dominodatalab.com/hc/en-us/articles/115001488023

  • akshay.ambekarakshay.ambekar Administrator, Moderator, Domino Posts: 19 admin

    If you wish to achieve the same functionality in R, please follow the below steps:

    1. Create a file named multiplemodelroutes.R in your project. Add the below code to this file:

    # Return the sum of two numbers
    add_numbers <- function(first_number = 1, second_number = 1){
      return (first_number + second_number)
    }
    
    # Be nice. Say hello to your new friend. Return a friendly greeting
    say_hello <- function(name = 'hello there!') {
      return (name)
    }
    
    # Define a main function to accept other function names and parameters
    my_model <- function(func_name, ...){
      args = list(...)
      output <- do.call(func_name, args)
      return (output)
    }
    

    2. From your project panel,

      a. Click Publish > Model APIs > New Model.

      b. Provide a meaningful model name and click next.

      c. In the model setup page, specify multiplemodelroutes.R as the file name, and my_model as the function to be invoked. Click Create Model.

    3. Once your model builds and launches successfully, navigate to Overview > Tester. Now you can invoke add_numbers or say_hello function by passing the appropriate function name and parameters. For example:

      a. If you want to call add_numbers, enter this and click Send 

    { 
      "data": { 
        "func_name": "add_numbers", 
        "first_number": 100, 
        "second_number": 250 
      } 
    } 
    

      b. If you want to call say_hello, enter this and click Send

    { 
      "data": { 
        "func_name": "say_hello", 
        "name": "Hello User!" 
      } 
    } 
    


Sign In or Register to comment.