Skip to content
Snippets Groups Projects
Commit c065ebff authored by Sergey Yakubov's avatar Sergey Yakubov
Browse files

refactor worker

parent a4737bcd
Branches
Tags
No related merge requests found
......@@ -56,31 +56,46 @@ std::string GetIDFromJson(const std::string& json_string, Error* err) {
return std::to_string(id);
}
Error ServerDataBroker::GetFileInfoFromServer(FileInfo* info, const std::string& operation) {
void ServerDataBroker::ProcessServerError(Error* err,const std::string& response,std::string* redirect_uri) {
if ((*err)->GetErrorType() != asapo::ErrorType::kEndOfFile) {
(*err)->Append(response);
return;
} else {
if (response.find("id") != std::string::npos) {
auto id = GetIDFromJson(response, err);
if (*err) {
return;
}
*redirect_uri = server_uri_ + "/database/" + source_name_ + "/" + id;
}
}
*err=nullptr;
return;
}
Error ServerDataBroker::ProcessRequest(std::string* response,std::string request_uri) {
Error err;
HttpCode code;
std::string full_uri = server_uri_ + "/database/" + source_name_ + "/" + operation;
std::string response;
*response = httpclient__->Get(request_uri, &code, &err);
if (err != nullptr) {
return err;
}
return HttpCodeToWorkerError(code);
}
Error ServerDataBroker::GetFileInfoFromServer(FileInfo* info, const std::string& operation) {
std::string request_uri = server_uri_ + "/database/" + source_name_ + "/" + operation;
uint64_t elapsed_ms = 0;
std::string response;
while (true) {
response = httpclient__->Get(full_uri, &code, &err);
if (err != nullptr) {
return err;
auto err = ProcessRequest(&response,request_uri);
if (err == nullptr) {
break;
}
err = HttpCodeToWorkerError(code);
if (err == nullptr) break;
if (err->GetErrorType() != asapo::ErrorType::kEndOfFile) {
err->Append(response);
ProcessServerError(&err,response,&request_uri);
if (err != nullptr) {
return err;
} else {
if (response.find("id") != std::string::npos) {
auto id = GetIDFromJson(response, &err);
if (err) {
return err;
}
full_uri = server_uri_ + "/database/" + source_name_ + "/" + id;
}
}
if (elapsed_ms >= timeout_ms_) {
......
......@@ -20,6 +20,8 @@ class ServerDataBroker final : public asapo::DataBroker {
std::unique_ptr<HttpClient> httpclient__;
private:
Error GetFileInfoFromServer(FileInfo* info, const std::string& operation);
void ProcessServerError(Error* err,const std::string& response,std::string* redirect_uri);
Error ProcessRequest(std::string* response,std::string request_uri);
std::string server_uri_;
std::string source_name_;
uint64_t timeout_ms_ = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment