Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#include "CondTools/RunInfo/interface/OMSAccess.h"
#include "CondCore/CondDB/interface/WebUtils.h"
#include "CondCore/CondDB/interface/Exception.h"

namespace cond {

  OMSServiceResultRef::OMSServiceResultRef(const boost::property_tree::ptree* row) : m_row(row) {}

  bool OMSServiceResultRef::empty() { return m_row == nullptr; }

  std::string OMSServiceResultRef::getAttribute(const std::string& attributeName) {
    return m_row->get<std::string>(attributeName);
  }

  OMSServiceResultIterator::OMSServiceResultIterator(boost::property_tree::ptree::const_iterator iter) : m_iter(iter) {}

  OMSServiceResultRef OMSServiceResultIterator::operator*() {
    auto& attributeList = m_iter->second.get_child("attributes");
    return OMSServiceResultRef(&attributeList);
  }

  OMSServiceResultIterator& OMSServiceResultIterator::operator++() {
    m_iter++;
    return *this;
  }

  bool OMSServiceResultIterator::operator==(const OMSServiceResultIterator& rhs) { return m_iter == rhs.m_iter; }
  bool OMSServiceResultIterator::operator!=(const OMSServiceResultIterator& rhs) { return m_iter != rhs.m_iter; }

  OMSServiceResult::OMSServiceResult() {}

  OMSServiceResultIterator OMSServiceResult::begin() const { return OMSServiceResultIterator(m_data->begin()); }

  OMSServiceResultIterator OMSServiceResult::end() const { return OMSServiceResultIterator(m_data->end()); }

  OMSServiceResultRef OMSServiceResult::front() const {
    auto& attributeList = m_data->front().second.get_child("attributes");
    return OMSServiceResultRef(&attributeList);
  }

  OMSServiceResultRef OMSServiceResult::back() const {
    auto& attributeList = m_data->back().second.get_child("attributes");
    return OMSServiceResultRef(&attributeList);
  }

  size_t OMSServiceResult::parseData(const std::string& data) {
    m_data = nullptr;
    std::stringstream sout;
    sout << data;
    try {
      boost::property_tree::read_json(sout, m_root);
    } catch (boost::property_tree::json_parser_error const& ex) {
      throw cond::Exception(ex.what(), "OMSServiceResult::parseData");
    }
    if (!m_root.empty()) {
      m_data = &m_root.get_child("data");
    }
    return m_root.size();
  }

  size_t OMSServiceResult::size() const {
    size_t ret = 0;
    if (m_data) {
      ret = m_data->size();
    }
    return ret;
  }

  bool OMSServiceResult::empty() const { return size() == 0; }

  void OMSServiceQuery::addVar(const std::string& varName) {
    std::stringstream varList;
    if (m_varList.empty()) {
      varList << "&fields=";
    } else {
      varList << m_varList << ",";
    }
    varList << varName;
    m_varList = varList.str();
  }

  OMSServiceQuery::OMSServiceQuery(const std::string& baseUrl, const std::string& function) {
    m_url = baseUrl + "/" + function;
  }

  OMSServiceQuery& OMSServiceQuery::addOutputVar(const std::string& varName) {
    addVar(varName);
    return *this;
  }
  OMSServiceQuery& OMSServiceQuery::addOutputVars(const std::initializer_list<const char*>& varNames) {
    for (auto v : varNames)
      addVar(v);
    return *this;
  }

  OMSServiceQuery& OMSServiceQuery::limit(int value) {
    std::stringstream pageLimit;
    if (m_filter.empty()) {
      pageLimit << "?";
    } else {
      pageLimit << "&";
    }
    pageLimit << "page[limit]=" << value;
    m_limit = pageLimit.str();
    return *this;
  }

  bool OMSServiceQuery::execute() {
    bool ret = false;
    std::string out;
    m_status = cond::httpGet(url(), out);
    if (m_status == 200 || m_status == 201) {
      m_result = std::make_unique<OMSServiceResult>();
      m_result->parseData(out);
      ret = true;
    }
    return ret;
  }

  unsigned long OMSServiceQuery::status() { return m_status; }

  OMSServiceResult& OMSServiceQuery::result() { return *m_result; }

  std::string OMSServiceQuery::url() { return m_url + m_filter + m_limit + m_varList; }

  OMSService::OMSService() : m_baseUrl() {}

  void OMSService::connect(const std::string& baseUrl) { m_baseUrl = baseUrl; }
  std::unique_ptr<OMSServiceQuery> OMSService::query(const std::string& function) const {
    return std::make_unique<OMSServiceQuery>(m_baseUrl, function);
  }
}  // namespace cond