Chore - control-flow requests
Tags: go, lowcode, api, productivity
Published: 2022-08-20 12:22:11

![chore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1kwsjo2gpetraj1t186t.png)

I created this application so you can drag and drop design your API. It is a low-code app, and this kind of application in some areas is required. For example, you want to send a request to a bunch of APIs and wait for all of them to return ok and parse results to generate a new result and return that as an answer. So it isn't enjoyable when trying to write this process in coding. I have some of this kind of work to handle, so I want to create this app to do this kind of task in future very quickly.

https://github.com/worldline-go/chore

## Let's use the CHORE

This tool required PostgreSQL to save all of the information there.
First run Postgres in somewhere, for quick I show in the container:

```sh
docker run -d --name postgres -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres:14.5-alpine
```

The chore tool required a schema that can create its tables and makes migrations, but we can use the default public schema for this demo.
Now open the chore tool, which you can download in the releases section in binary format. It is just one executable file and has no dependency.
https://github.com/worldline-go/chore/releases/
Or we can directly use the chore's container for this:

```sh
docker run -it --rm --name chore -p 8080:8080 -e STORE_HOST=172.17.0.1 -e STORE_SCHEMA=public ghcr.io/worldline-go/chore:latest
```

This tool also can get configuration from the vault, consul or file (TOML, YAML, JSON). So you can also use some of them or combine them.

Open http://localhost:8080/ and login with username as **admin** and password as **admin**. You can also set first initialize user settings in configuration.
In this application, we have three sections:

**Control**: we can create our control flow diagram here.
**Auths**: set HTTP headers here, including sensitive data like tokens. You will use this one in a request somewhere.
**Templates**: go-template area so you can create a template, feed with some object, and get the result.
For the demo to start, I can show you get some beers, parse them, combine names and return results.

## Beer - control-flow

To get some beer information, I used this API: https://punkapi.com/documentation/v2.

First, go to the control section and create a control that gives a unique name.
Our flow always starts with the endpoint. You can multiple endpoints to do different jobs in the same control flow.
So our general flow show like that:

![beer-flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70k7hncoizev5i1pdsxb.png)

**Endpoint**: This is a flow entry, explain which HTTP method to communicate and whether it is public reachable or needs a TOKEN.
I set the endpoint name as 'names', this is not unique, and if you have more than one endpoint with the same name in this control flow, that will run also. Also, I set the method as 'GET' so I can directly call in the browser URL.

**Requests**: This is calling other HTTP services with some data. Our method is 'GET', so we do not need any data.
Here I write the URL as https://api.punkapi.com/v2/beers/1 and second request has https://api.punkapi.com/v2/beers/2.

Every node runs as a different goroutine, so we need to wait for them and get data. Here we need a script node.

![script-select](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bj706dqoqavpevu0798h.png)

We added a script node with two inputs, so this node waits for all data and initializes.

**Script**: This is Ecma 5.1 specifications script library called Goja; it must have the main named function and arguments as your inputs. The first input is your first argument.
Click the `Open Editor` button and write your script there: 

```js
function main(beer1, beer2) {
  return {
   names: [ beer1[0].name, beer2[0].name ]
  };
}
```

![script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bof5gv2raaw5jung70y2.png)

Don't forget to click save after that.

As you see, the script node has three outputs, which we saw in requests.

![script-outputs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/37pn2i7m1niivb49fy8b.png)

'F' means your code has thrown an error; flow goes to F.
'T' means everything works fine.
'R' is false or true. Run it.

And our flow's last step is to respond.

![respond](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1q12jfy21tgou7yxiga.png)

I am sending what I get in the script node as a body to respond. But I also need some headers to say this is a JSON value.

You also see 'Get respond in data' this button is usable just after the request node, so if you click this button and the previous node is a requesting node, you will get every detail to respond code and headers in there (our headers will append in that time).

Now, let's run it. You can use the Send section or directly send it to the browser.

Send section for testing, and it explains your curl command:

![send](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tavgdslzyqxs4bew08j3.png)

So our URL is
http://localhost:8080/api/v1/send?control=beer&endpoint=names

Call in browser:

![browser](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jmynpqe6gai6wq3b61lf.png)

Yeyyyy, you write your first API. Well done.

You can write much more complicated stuff in there. I have already added some helper nodes, and in the future, I will add more and make plugins for them.

![nodes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nakskcggltayfk4pewjq.png)

Thanks for your use; if you have any questions, write in the comments!