dehaze

Calling Functions

Any function written on backend can be called on the frontend either by using GraphQL or our client SDKs.

Using GraphQL

Example: Let’s say we want to call a function - func1 of service - service1. This is how you would do it in GraphQL:

query {
  func1(
    arg1: "10",
    arg2: "20"
  ) @service1 {
    status
    data
  }
}
{
  "data": {
    "func1": {
      "status": 200,
      "data": {
        "result: "Object sent by the function"
      }
    }
  }
}

Using client SDKs

import { API } from "space-api";

// Initialize api with the project name and url of the space cloud
const api = new API("todo_app", "http://localhost:4122");

// Call a function running 'my-func' of 'my-service' running on backend
api.call('my-service', 'my-func', { msg: 'Space Cloud is awesome!' }, 1000)
  .then(res => {
    if (res.status === 200) {
      console.log('Response: ', res.data)
    }
  }).catch(ex => {
    // Exception occured while processing request
  })

import (
	"github.com/spaceuptech/space-api-go/api"
	"fmt"
)

func main() {
	api, err := api.New("books-app", "localhost:4124", false)
	if(err != nil) {
		fmt.Println(err)
	}
	v := map[string]interface{}{"params":"params"}
	resp, err := api.Call("service", "echo_func", v, 5000)
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		if resp.Status == 200 {
			var v map[string]interface{}
			err:= resp.Unmarshal(&v)
			if err != nil {
				fmt.Println("Error Unmarshalling:", err)
			} else {
				fmt.Println("Result:", v)
			}
		} else {
			fmt.Println("Error Processing Request:", resp.Error)
		}
	}	
}


API api = new API("books-app", "localhost", 4124);
Utils.ResponseListener responseListener = new Utils.ResponseListener() {
    @Override
    public void onResponse(int statusCode, Response response) {
        try {
            System.out.println("Functions Response: " + response.getResult(Object.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(Exception e) {
        System.out.println("Error: " + e.toString());
    }
};
api.call("service", "echo_func", 5000, "FaaS is awesome!", responseListener);

from space_api import API

api = API("books-app", "localhost:4124")
# Call a function, 'my-func' of 'my-engine' running on backend
response = api.call('my-engine', 'my-func', {"msg": 'Space Cloud is awesome!'}, 1000)
if response.status == 200:
    print(response.result)
else:
    print(response.error)

api.close()

The call function takes four arguments which are as follows: - serviceName - Name of the service - funcName - Name of the function - params - An object that can contain any data that you want to pass to the function on backend - timeOut - Timeout in milli seconds

As you would have noticed, the above function is asynchronous in nature. The call method triggers the specified function on the backend with the provided params. If the function takes more time to execute than the given timeout, an exception is returned

Response

On response from the server, the callback passed to the then method is called with the response object as described below:

{
  "status": "number", // Status of the operation
  "data": {
    "result": "object" // Response returned by the function
  }
}

Have a technical question?

Improve the docs!