For the customer to know how long it will take, you need to submit a response. This type of problem can be adequately addressed using an asynchronous approach. There are advantages to doing this. For example, in case of high demand, you can queue requests to your DW and do not leave "locked" threads on your server. In addition, customers do not need to change the connection timeouts to their REST service. Of course the implementation complexity increases a bit, but it pays to have a more elegant and scalable solution.
My suggestion is that you create a service POST /QtdeVendasNoPeriodo/{dtInicio}/{dtFim}
. Upon receiving the request, you submit the report generation to a worker thread and immediately respond to the client with HTTP code 202 Accepted
and the HTTP header Location
with the address where the generation status of the report will be queried.
The response to the POST
service would be something like:
HTTP/1.1 202 Accepted
Location: https://<seu_endereco_web>/filaProcessamento/sdf4524wrwerwe
sdf4524wrwerwe
is any identifier that uniquely reports the generation status of the report. /filaProcessamento/{idStatusProcessamento}
is a service GET
where you would respond, for example:
http GET https://<seu_endereco_web>/filaProcessamento/sdf4524wrwerwe
HTTP/1.1 200 Ok
<resposta>
<status>PENDENTE</status>
<tempoRestante>120 segundos</tempoRestante>
<link rel="cancel" method="delete" href="/filaProcessamento/sdf4524wrwerwe" />
</resposta>
I used XML above, but it could be JSON. The link
tag would be to inform the user of the possibility of canceling the generation of the report. In this case, a DELETE
service would be required.
Once the report has been created, the response to the GET service described above would be
http GET https://<seu_endereco_web>/filaProcessamento/sdf4524wrwerwe
HTTP/1.1 303 See Other
Location: https://<seu_endereco_web>/QtdeVendasNoPeriodo/97525252665
The HTTP code 303
is for client redirection. The Location
header tells the client where the resource is available. It is necessary that you implement a GET /QtdeVendasNoPeriodo/{idResultado}
service, where idResultado
is any identifier of the report expected by the client, which will also be controlled by your application.
Some implementation details need to be decided as needed. For example, in response to the GET /QtdeVendasNoPeriodo/{idResultado}
service, report in the Expires
header how long the report can be cached. Report consistent HTTP codes, such as 404 Not Found
, if the report does not exist or 410 Gone
after the client has already downloaded the report or your application has discarded the data after a certain period of time. Anyway, several improvements can be thought of.
Security tip: idResultado
can be, for example, a large random number or a hash. Thus, any client is likely to be able to access the results of other clients by querying the GET /QtdeVendasNoPeriodo/{idResultado}
service. If you use a sequential, for example, a client may have access to other results only by incrementing values of idResultado
.