# Polling Handling ![Logo](http://funkyimg.com/i/2R7NT.png)

![WidergyWeb](https://img.shields.io/badge/WIDERGY-WEB-00d564.svg)
![PackageSize](https://img.shields.io/bundlephobia/minzip/@widergy/polling-handling.svg?style=flat)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)


```
< Hello! can you give me the user data?
> Hi, I don't have that information, but my friend 83abb91c-31f0-4be6-9b8b-88cbf716c can get it for you.
< Mmm... ok.. Hey 83abb91c-31f0-4be6-9b8b-88cbf716c can you give me the user data?
> Processing
< 🤔 Ok ... now?
> Processing
< 🤔 Ok ... now?
> Processing
< 😒 now?
> OK! { userInfo: ... }
< Great! 😁
```

## Installing

```
npm install --save @widergy/polling-handling

yarn add @widergy/polling-handling
```

## Basic usage

#### On application starts

```js
import Polling from '@widergy/polling-handling';

class App extends Component {
  componentDidMount = () => {
    Polling.init('Polling endpoint base url, for example: http://apibaseurl.com/api/v1/async_request/jobs/');
  ....
```

#### When you need to consume a service with polling

```js
import Polling from '@widergy/polling-handling';

// Service that triggers async worker generation 
const someService = parameters => api.get('/some_endpoint', parameters);

....
// When we need to consume that service, and poll the async worker to obtain info:
const response = await Polling.handle(someService(parameters));
```

## Interface

### Input

#### Polling.init(baseUrl)

- baseUrl (Required): Endpoint base for all async jobs, then handle concats job id to this url.

#### Polling.handle(originalRequest, timeout)

- originalRequest (Required): Api call that returns job code for poll.

- timeout (Optional): For override the default timeout value (60000 milliseconds).

### Output

If requests fails returns a response object with two keys:
- error: Complete error details from api answer.
- status: Status code from api answer.

If request finish ok, a response object with only one key:
- data: Complete information from api answer.

## License

MIT © [Widergy](https://www.widergy.com/)
