Skip to content

Commit 695d821

Browse files
committed
networkRequest Async
1 parent f5367ef commit 695d821

1 file changed

Lines changed: 78 additions & 24 deletions

File tree

content/developer/script-task-api.mdx

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ if (hasBeenPaused) {
238238
This allows you to achieve multiple semi-parallel code executions.
239239
</Callout>
240240

241-
Like [`wait()`](#wait) but asynchronous, so it immediately continues with the next code instructions and does not block the specified timeout is finished.
242-
In this sense, it is similar to the usual `setTimeout()` but only with a Promise. So, you can use `await` or `then()`.
241+
Like [`wait()`](#wait) but asynchronous. So, it immediately continues with the next code instructions and does not block the execution.
242+
In this sense, it is similar to the usual `setTimeout()` but instead of callbacks, it returns a _Promise_.
243+
Therefore, you can use `await` or `then()`.
243244
Pausing behavior is the same as for `wait()`.
244245

245246
| API | |
@@ -320,14 +321,17 @@ id = setInterval(() => f(id, 2), 1000);
320321
321322
### HTTP Network Requests and Server
322323
323-
#### `networkRequest`: sychronous functions
324+
#### `networkRequest`: sychronous functions [#networkRequest]
324325
325326
Inside of a Script Task it is possible to send HTTP(S) requests to a given URL.
326327
The prefered way of using `networkRequest` is with its synchronous methods, i.e. the functions wait until an answer is is received.
327328
Since this are network requests, the completion of the methods sometimes need a while until the response is received.
328329
329-
| API | Explanation |
330-
| ----------------------------------------------------------------------------------- | -------------------------------------------------------------- |
330+
If the entire process is paused from the Management System, all script code is also paused until the process is resumed. If a pause occurs during a `networkRequest.X()`, the system stores the response and returns it when the process is resumed.
331+
332+
333+
| API | |
334+
| -------------------------------- | ------------------------------------------------- |
331335
| `.get( "<url>", [<{options}>]): { response, body }` | Sends a GET-Request to a URL |
332336
| `.post( "<url>", <{body}>, ["<content-type>"], [<{options}>]): { response, body } ` | Sends a POST-Request to a URL with a `body` (Object or String) |
333337
| `.put( "<url>", <{body}>, ["<content-type>"], [<{options}>]): { response, body }` | Sends PUT-Request to URL with a `body` (Object or String) |
@@ -357,12 +361,7 @@ In case of a secure HTTPS connection, the `options` object has some [additional
357361

358362
The network functions only return if the _response_ has a HTTP 2xx status code. The methods return an object that contains two properties: `{ response, body }`
359363

360-
- `response`: is an object that contains meta information about the response,
361-
including `.headers`, `.httpVersion`, `.method`, `.statusCode`,
362-
`.statusMessage`, and `.url`. Because it resembles _IncomingMessage_, see the
363-
[Node.js API about
364-
IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
365-
for more information.
364+
- `response`: is an object that contains meta information about the response. It includes the following properties: `.rawHeaders`, `.rawTrailers`, `.httpVersionMajor`, `.httpVersionMinor`, `httpVersion`, `.method`, `.statusCode`, `.statusMessage`, `.url`, `.complete`, `.aborted`, `.upgrade`
366365
- `body`: Body of the response
367366

368367
The functions throw an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) if:
@@ -404,21 +403,76 @@ try {
404403
405404
#### `networkRequest`: asychronous functions
406405
407-
The network functions are asynchronous, meaning a Promise is returned.
408-
**Attention:** The `async/await` pattern should be used to handle this, **not**
409-
the `.then()` and `.catch()`.
406+
<Callout type='warning'>
407+
The use of the asynchronous `networkRequest.X()` functions is only suitable for advanced users.
408+
It makes code execution asynchronous.
409+
This allows you to achieve multiple semi-parallel code executions.
410+
</Callout>
410411
411-
Inside of a Script Task it is possible to send HTTP(S) requests to a given URL.
412-
The prefered way of using `networkRequest` is with its synchronous methods, i.e. the functions wait until an answer is is received.
413-
Since this are network requests, the completion of the methods sometimes need a while until the response is received.
412+
Like the synchronous [`networkRequest.X()`](#networkRequest) functions but asynchronous. So, it immediately continues with the next code instructions and does not block the execution.
413+
The functions return a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) with `{ response, body}` object.
414+
Therefore, you can use `await`, `then()`, `catch()` or `finally()`.
415+
Pausing behavior is the same as for `networkRequest.X()`.
414416
415-
| API | Explanation |
416-
| ----------------------------------------------------------------------------------- | -------------------------------------------------------------- |
417-
| `.get( "<url>", [<{options}>]): { response, body }` | Sends a GET-Request to a URL |
418-
| `.post( "<url>", <{body}>, ["<content-type>"], [<{options}>]): { response, body } ` | Sends a POST-Request to a URL with a `body` (Object or String) |
419-
| `.put( "<url>", <{body}>, ["<content-type>"], [<{options}>]): { response, body }` | Sends PUT-Request to URL with a `body` (Object or String) |
420-
| `.delete( "<url>", [<{options}>]): { response, body }` | Sends a DELETE-Request to a URL |
421-
| `.head( "<url>", [<{options}>]): { response, body }` | Sends a HEAD-Request to a URL |
417+
| API | |
418+
| -------------------------------- | ------------------------------------------------- |
419+
| `.getAsync( "<url>", [<{options}>]): Promise<{ response, body }>` | Sends a GET-Request to a URL |
420+
| `.postAsync( "<url>", <{body}>, ["<content-type>"], [<{options}>]): Promise<{ response, body }>` | Sends a POST-Request to a URL with a `body` (Object or String) |
421+
| `.putAsync( "<url>", <{body}>, ["<content-type>"], [<{options}>]): Promise<{ response, body }>` | Sends PUT-Request to URL with a `body` (Object or String) |
422+
| `.deleteAsync( "<url>", [<{options}>]): Promise<{ response, body }>` | Sends a DELETE-Request to a URL |
423+
| `.headAsync( "<url>", [<{options}>]): Promise<{ response, body }>` | Sends a HEAD-Request to a URL |
424+
425+
426+
**Example Code**
427+
428+
```js
429+
networkRequest.getAsync(
430+
'https://www.proceed-labs.org/',
431+
{
432+
headers: {
433+
'user-agent': 'PROCEED-ENGINE/1.0.0'
434+
},
435+
rejectUnauthorized: true,
436+
},
437+
).then(
438+
({ response, body }) => {
439+
console.info("Request successful: " + body.slice(-7));
440+
console.log(response);
441+
}
442+
).catch(({ response, body }) => {
443+
console.info("Request NOT successful: " + body.slice(-7));
444+
console.log(response);
445+
}).finally(() => {
446+
console.info("\n\nRequest FINAL\n\n");
447+
});
448+
```
449+
450+
#### `networkServer`: create a listening HTTP Server
451+
452+
TODO
453+
454+
| API | |
455+
| -------------------------------- | ------------------------------------------------- |
456+
| `.get( "path" ): { req, res }` | TODO |
457+
| `.post( "path" ): { req, res }` | TODO |
458+
| `.put( "path" ): { req, res }` | TODO |
459+
| `.delete( "path" ): { req, res }` | TODO |
460+
| `.getAsync( "path" ): Promise<{ req, res }>` | TODO |
461+
| `.postAsync( "path" ): Promise<{ req, res }>` | TODO |
462+
| `.putAsync( "path" ): Promise<{ req, res }>` | TODO |
463+
| `.deleteAsync( "path" ): Promise<{ req, res }>` | TODO |
464+
| `.get( "path", [<clb()>] ): void` | TODO |
465+
| `.post( "path", [<clb()>] ): void` | TODO |
466+
| `.put( "path", [<clb()>] ): void` | TODO |
467+
| `.delete( "path", [<clb()>] ): void` | TODO |
468+
| `.close(): void` | TODO |
469+
470+
471+
472+
**Example Code**
473+
474+
```js
475+
```
422476
423477
### Trigger Events
424478

0 commit comments

Comments
 (0)