FAQ: Publish multiple REST APIs via domino

Follow

Comments

2 comments

  • Jaclyn Patterson

    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!" 
      } 
    } 
    


    Submitted by: akshay.ambekar

    0
    Comment actions Permalink
  • Jaclyn Patterson

    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

    Submitted by: akshay.ambekar

    0
    Comment actions Permalink

Please sign in to leave a comment.