Commit 594bfa91 authored by Melikhov Arsenii's avatar Melikhov Arsenii

Update integrateWithService.md;

Text has rewritten for better understanding
parent e0ed2e3e
# Integration with external api
# Integration with external API
<a name="menu"></a>
[Send http requests from ub](#send-requests)
[Receiving requests](#receiving-requests)
[Get data from query string](#get-data-from-query-string)
[Get request body data](#get-request-body-data)
* [Sending requests](#sending-requests)
* [Receiving requests](#receiving-requests)
- [Get data from a query string](#get-data-from-a-query-string)
- [Get data from a request body](#get-data-from-a-request-body)
<a name="endmenu"></a>
## Send requests
## Sending requests
Let's say you must get users from api:
https://jsonplaceholder.typicode.com/users
Let's say you need to get data from some API.
For send request and get data you can use `http.request`
The basic code of sending a request and getting data from it using `http.request` package can look something like this:
```javascript
const http = require('http')
const req = http.request({
URL: 'https://jsonplaceholder.typicode.com/users',
URL: 'API_ENDPOINT,
method: 'GET',
sendTimeout: 1000, receiveTimeout: 30000, connectTimeout: 1000,
keepAlive: true,
......@@ -27,9 +26,14 @@ const req = http.request({
const userData = JSON.parse(resp.read('utf-8'))
```
Next example is more complete, with custom buttons and saving data to db.
Now let's look how to use it with UB.
***
We will create a method that sends a request to <https://jsonplaceholder.typicode.com/users> API, gets data, parses it, and stores it in the database.
And all this will be called by clicking on the custom button that we will create.
Create new meta file `req_user.meta`
First, we need to create a new meta file `req_user.meta`, with following code:
```json
{
"caption": "Users", // entity name
......@@ -75,7 +79,7 @@ Create new meta file `req_user.meta`
}
```
Create `req_user.js` next to the file `req_user.meta`
Then, create `req_user.js` file with the code below:
```javascript
const UB = require('@unitybase/ub')
const http = require('http')
......@@ -120,19 +124,13 @@ me.addUsers = function (ctx) {
}
```
In this file we create custom method `addUsers` which sends request to https://jsonplaceholder.typicode.com/users,
parses, and stores data from response to database.
Create shortcut (right click on left panel -> Add shortcut) with parameters:
Run the server. (Don't forget to Generate DDL before it)
`Desktop`: City Requests
`Shortcut caption`: Users
`Code`: req_user
`Source code`:
Go to Shortcut creation panel and create one with the following parameters:
* `Desktop`: City Requests
* `Shortcut caption`: Users
* `Code`: req_user
* `Source code`:
```json
{
"cmdType": "showList",
......@@ -164,34 +162,43 @@ Create shortcut (right click on left panel -> Add shortcut) with parameters:
}]
}
```
In `customActions` section we add button with handler which call `addUsers` method from `req_user.js`
In the source code in `customActions` section, we added a button with the handler which calls `addUsers` method from `req_user.js`.
---
**NOTE**
After this steps you must see button:
:exclamation: The syntax checker for source code can produce errors. You can ignore them.
---
Now, you can see the button in `Users` Shortcut:
![addUsers button](img/integrateWithService/addUsersButton.png)
After click on this button and reload users must be shown:
The result of adding users:
![saved users](img/integrateWithService/savedUsers.png)
***
## Receiving requests
If you want to receiving requests on some endpoint in your app, you must
register this endpoint and set handler for it by the `registerEndpoint` method.
If you want to receive requests on some endpoint in your app,
you must register this endpoint and set a handler for it by the `registerEndpoint` method:
```javascript
const App = require('@unitybase/ub').App
App.registerEndpoint('addUser', addUser, false)
```
First parameter is endpoint literal. In this example request on `http://localhost/addUser` will be handled.
Next parameter is handler function.
* The first parameter is endpoint literal. (In this example requests on `http://localhost:8881/addUser` will be handled)
* The second parameter is a handler function
* The third parameter is whether required authentication headers
Third parameter is whether required authentication headers.
### Get data from query string
In next code in function addUser we parse user fields from query string and insert them to db.
### Get data from a query string
The following code shows how to parse user fields from the query string and insert them to the DB:
```javascript
const UB = require('@unitybase/ub')
const App = require('@unitybase/ub').App
......@@ -217,19 +224,26 @@ function addUser (req, resp) {
```
Paste this code to `addUserEndpoint.js` and add line `require('./addUserEndpoint')` to `req.js`.
Add this code to the `addUserEndpoint.js` file.
Also, add a line `require('./addUserEndpoint')` to the `req.js` file.
After restarting the server you can add users by making requests to `http://localhost:8881/addUser`.
For example:
```
http://localhost:8881/addUser?name=Andrey&email=abd@ab.com&phone=3849530463&website=kao.in.ua
```
After server restart you can save users from requests.
If all done correctly you will see something like this:
![request](img/integrateWithService/getRequest.png)
![user added](img/integrateWithService/userAdded.png)
### Get request body data
### Get data from a request body
Use `req.read()` to get data from body.
You can use `req.read()` to get data from a request body.
Here is function `addUser` in this case:
In this case, the `addUser` function will look like this:
```javascript
function addUser (req, resp) {
const { name, email, phone, website } = JSON.parse(req.read())
......@@ -248,7 +262,7 @@ function addUser (req, resp) {
resp.writeEnd('user addded')
}
```
After sending next request, user will be added.
You can check the result with [Postman](https://www.postman.com/):
![send post](img/integrateWithService/sendPost.png)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment