ATLAS Offline Software
CrestRequest.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2019-2024 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 #ifndef CRESTAPI_REQUEST_H
6 #define CRESTAPI_REQUEST_H
7 
8 #include <string>
9 #include <map>
10 #include <iosfwd>
11 #include <cstdint>
12 #include "nlohmann/json.hpp"
13 #include <iostream>
14 #include <curl/curl.h>
15 
16 namespace Crest
17 {
18 
19  // The action to be performed
20  enum class Action
21  {
22  GET = 0,
23  POST = 1,
24  PUT = 2,
25  DELETE = 3
26  };
27 
29  {
30  private:
31  std::string m_str{};
32 
33  public:
34  void add(const std::string &key, const std::string &value)
35  {
36  if (m_str.empty())
37  {
38  m_str = key;
39  m_str += '=';
40  m_str += value;
41  }
42  else
43  {
44  m_str += '&';
45  m_str += key;
46  m_str += '=';
47  m_str += value;
48  }
49  }
50 
51  const std::string &getParams() const
52  {
53  return m_str;
54  }
55 
56  void reset()
57  {
58  m_str.clear();
59  }
60  };
61 
63  {
64 
65  private:
66  std::string m_prefix = "http://";
67  std::string m_host;
68  std::string m_port;
69  std::string make_url(const std::string &address) const;
70 
71  char* m_CREST_PROXY = NULL;
72  const char* m_CREST_PROXY_VAR = "SOCKS_PROXY";
73 
74  // Auxiliary method to make request to the CREST Server. This method is used by other methods realizing the
75  // requests with the concrete kinds of data (iovs|payloads|tagsā€¦).
76  // They are used to clean the response from the CREST Server from the additional information and to check the
77  // response code.
78  bool isJson(const std::string& str);
79  nlohmann::json getJson(const std::string& str, const char* method);
80  nlohmann::json getJson(const std::string& str);
81  int checkErrors(const nlohmann::json& js, const char* method);
82  std::string parseXMLOutput(const std::string_view xmlBuffer);
83  std::string removeCR(const std::string& str);
84 
85  void getProxyPath();
86 
87  public:
88  CrestRequest();
89  ~CrestRequest();
90 
91  void setHost(const std::string& host);
92  void setPort(const std::string& port);
93  void setPrefix(const std::string& prefix);
94 
105  std::string performRequest(const std::string &current_path, Action action, nlohmann::json &js, const std::string& header_params = "");
106 
118  std::string performRequest(const std::string &current_path, Action action, nlohmann::json &js,
119  const char *method_name, const std::string& header_params = "");
120 
121 
122  std::vector<char> getPayloadRequest(const std::string &current_path);
123 
124 
125  std::string uploadPayload(const std::string &current_path,
126  const std::string& tag, uint64_t endtime, const nlohmann::json& js,
127  const std::string& objectType, const std::string& compressionType, const std::string& version,
128  const std::vector<std::string>& files);
129 
130  void checkResult(CURLcode res, long response_code, const std::string& st, const char* method_name);
131  };
132 }
133 
134 #endif // CRESTAPI_REQUEST_H
RunTileTBRec.method
method
Definition: RunTileTBRec.py:73
Crest::CrestRequest::m_prefix
std::string m_prefix
Definition: CrestRequest.h:66
Crest::CrestRequest::make_url
std::string make_url(const std::string &address) const
Definition: CrestRequest.cxx:48
Crest::Action::POST
@ POST
json
nlohmann::json json
Definition: HistogramDef.cxx:9
Crest::CrestRequest::~CrestRequest
~CrestRequest()
Definition: CrestRequest.cxx:28
Crest::CrestRequest::parseXMLOutput
std::string parseXMLOutput(const std::string_view xmlBuffer)
Definition: CrestRequest.cxx:395
Crest::Action::DELETE
@ DELETE
Crest::CrestRequest::performRequest
std::string performRequest(const std::string &current_path, Action action, nlohmann::json &js, const std::string &header_params="")
General auxiliary method to make request to the CREST Server.
Crest::CrestRequest::uploadPayload
std::string uploadPayload(const std::string &current_path, const std::string &tag, uint64_t endtime, const nlohmann::json &js, const std::string &objectType, const std::string &compressionType, const std::string &version, const std::vector< std::string > &files)
Definition: CrestRequest.cxx:241
Crest::CrestRequest::m_CREST_PROXY
char * m_CREST_PROXY
Definition: CrestRequest.h:71
athena.value
value
Definition: athena.py:124
Crest::CrestRequest::setPrefix
void setPrefix(const std::string &prefix)
Definition: CrestRequest.cxx:43
Crest::CrestRequest::CrestRequest
CrestRequest()
Definition: CrestRequest.cxx:22
Crest::CrestRequest::checkResult
void checkResult(CURLcode res, long response_code, const std::string &st, const char *method_name)
Definition: CrestRequest.cxx:447
Crest
Definition: CrestApi.h:30
hotSpotInTAG.objectType
objectType
Definition: hotSpotInTAG.py:107
Crest::urlParameters::m_str
std::string m_str
Definition: CrestRequest.h:31
Crest::CrestRequest::getJson
nlohmann::json getJson(const std::string &str, const char *method)
Definition: CrestRequest.cxx:481
Crest::Action::GET
@ GET
Crest::CrestRequest::setHost
void setHost(const std::string &host)
Definition: CrestRequest.cxx:33
Crest::CrestRequest::removeCR
std::string removeCR(const std::string &str)
Definition: CrestRequest.cxx:421
Crest::urlParameters::reset
void reset()
Definition: CrestRequest.h:56
Crest::CrestRequest::isJson
bool isJson(const std::string &str)
Definition: CrestRequest.cxx:434
generateReferenceFile.files
files
Definition: generateReferenceFile.py:12
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
Crest::CrestRequest::getProxyPath
void getProxyPath()
Crest::CrestRequest::m_port
std::string m_port
Definition: CrestRequest.h:68
Crest::CrestRequest::getPayloadRequest
std::vector< char > getPayloadRequest(const std::string &current_path)
Definition: CrestRequest.cxx:183
Crest::Action::PUT
@ PUT
Crest::urlParameters::add
void add(const std::string &key, const std::string &value)
Definition: CrestRequest.h:34
Crest::CrestRequest::checkErrors
int checkErrors(const nlohmann::json &js, const char *method)
Definition: CrestRequest.cxx:511
RTTAlgmain.address
address
Definition: RTTAlgmain.py:55
Crest::CrestRequest
Definition: CrestRequest.h:63
Crest::Action
Action
Definition: CrestRequest.h:21
get_generator_info.version
version
Definition: get_generator_info.py:33
Crest::CrestRequest::m_CREST_PROXY_VAR
const char * m_CREST_PROXY_VAR
Definition: CrestRequest.h:72
python.CaloScaleNoiseConfig.action
action
Definition: CaloScaleNoiseConfig.py:77
str
Definition: BTagTrackIpAccessor.cxx:11
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
Crest::urlParameters
Definition: CrestRequest.h:29
Crest::CrestRequest::setPort
void setPort(const std::string &port)
Definition: CrestRequest.cxx:38
Crest::urlParameters::getParams
const std::string & getParams() const
Definition: CrestRequest.h:51
Crest::CrestRequest::m_host
std::string m_host
Definition: CrestRequest.h:67
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37