Commit c72ebe63 authored by Luciano Orsini's avatar Luciano Orsini
Browse files

references #261: added client API

parent cf77fbb6
# $Id$
#########################################################################
# XDAQ Components for Distributed Data Acquisition #
# Copyright (C) 2000-2019, CERN. #
# All rights reserved. #
# Authors: J. Gutleber, L. Orsini and D. Simelevicius #
# #
# For the licensing terms see LICENSE. #
# For the list of contributors see CREDITS. #
#########################################################################
##
#
# This is the cmsos/worksuite/extern/libhttpserver
#
##
#
# Packages to be built
#
BUILD_HOME:=$(shell pwd)/../..
BUILD_SUPPORT=build
PROJECT_NAME=worksuite
include $(XDAQ_ROOT)/$(BUILD_SUPPORT)/mfAutoconf.rules
include $(XDAQ_ROOT)/$(BUILD_SUPPORT)/mfDefs.$(XDAQ_OS)
Project=$(PROJECT_NAME)
Package=extern/restclient-cpp
PackageName=restclient-cpp
PACKAGE_VER_MAJOR=0
PACKAGE_VER_MINOR=5
PACKAGE_VER_PATCH=2
Summary=This is a simple REST client for C++. It wraps libcurl for HTTP requests.
Description=restclient-cpp provides two ways of interacting with REST endpoints. \
There is a simple one, which does not need you to configure an object to interact with an API. \
However the simple way does not provide a lot of configuration options either. So if you need more than just a simple HTTP call, you will probably want to check out the advanced usage.
Link=https://github.com/mrtazz/restclient-cpp
export CC
export CXX
LD:=$(LDD)
export LD
build: _buildall
_buildall: all
_all: all
default: all
$(PACKING_DIR)/configure:
-rm -rf $(XDAQ_PLATFORM)
tar -xvf 0.5.2.tar.gz
rm -rf $(PACKING_DIR)
mv restclient-cpp-0.5.2 $(PACKING_DIR)
all: $(PACKING_DIR)/configure
cd $(PACKING_DIR); ./autogen.sh; \
./configure --enable-debug --prefix=$(BUILD_HOME)/$(Package)/$(XDAQ_PLATFORM) --exec-prefix=$(BUILD_HOME)/$(Package)/$(XDAQ_PLATFORM); \
make; make install
_installall: install
install: $(PACKING_DIR)/configure
cd $(PACKING_DIR); ./autogen.sh; \
./configure --enable-debug --prefix=$(INSTALL_PREFIX)/$(XDAQ_PLATFORM) --exec-prefix=$(INSTALL_PREFIX)/$(XDAQ_PLATFORM); \
make; make install
_cleanall: clean
clean:
-rm -rf $(XDAQ_PLATFORM)
-rm -rf $(PACKING_DIR)
include $(XDAQ_ROOT)/$(BUILD_SUPPORT)/mfExternRPM.rules
......@@ -27,7 +27,7 @@ IncludeDirs = \
DependentLibraryDirs = $(DIP_LIB_PREFIX)
DependentLibraries = microhttpd httpserver
DependentLibraries = microhttpd httpserver restclient-cpp
LibraryDirs =
......
// $Id$
/*************************************************************************
/*
*************************************************************************
* XDAQ Components for Distributed Data Acquisition *
* Copyright (C) 2000-2021, CERN. *
* All rights reserved. *
......@@ -8,7 +9,8 @@
* *
* For the licensing terms see LICENSE. *
* For the list of contributors see CREDITS. *
*************************************************************************/
*************************************************************************
*/
#ifndef _restate_Service_h_
#define _restate_Service_h_
......@@ -27,6 +29,8 @@ namespace restate
~Service();
const std::shared_ptr<restate::response> render(const restate::request&);
const std::shared_ptr<restate::response> call(const restate::request&);
};
}
......
// $Id$
/*************************************************************************
/*
*************************************************************************
* XDAQ Components for Distributed Data Acquisition *
* Copyright (C) 2000-2021, CERN. *
* All rights reserved. *
......@@ -8,12 +9,18 @@
* *
* For the licensing terms see LICENSE. *
* For the list of contributors see CREDITS. *
*************************************************************************/
*************************************************************************
*/
#ifndef _restate_api_h_
#define _restate_api_h_
#include <httpserver.hpp>
#include "httpserver.hpp"
#include <iostream>
#include <variant>
#include "restclient-cpp/connection.h"
#include "restclient-cpp/restclient.h"
namespace restate
{
......@@ -28,5 +35,53 @@ namespace restate
using string = httpserver::string_response;
void register_resource(const std::string & path, const std::function<std::shared_ptr<restate::response>(const restate::request&)> & r);
//using connection = RestClient::Connection;
using headers = RestClient::HeaderFields;
//using result = RestClient::Response;
class result: public RestClient::Response
{
public:
result()
{
}
result(const RestClient::Response &obj): RestClient::Response({.code = obj.code,.body =obj.body,.headers =obj.headers})
{
}
const restate::headers & getHeaders()
{
return this->headers;
}
int getCode()
{
return this->code;
}
const std::string & getContent()
{
return this->body;
}
};
class connection: public RestClient::Connection {
public:
connection(const std::string & baseurl): RestClient::Connection(baseurl)
{
}
std::shared_ptr<restate::result> get(const std::string & path)
{
auto r = std::make_shared<restate::result>();
*r = RestClient::Connection::get(path);
return r;
//RestClient::Response r = RestClient::Connection::get(path);
//return std::shared_ptr<restate::result>( new restate::result(r));
}
};
}
#endif
......@@ -430,6 +430,7 @@ void restate::Application::config( pt::Address::Reference address)
LOG4CPLUS_DEBUG(getApplicationLogger(), "configuring address for restate" << address->toString() );
restate::Address & a = dynamic_cast<restate::Address &>(*address);
uint16_t port = std::stoi(a.getPort());
servers_.push_back( new httpserver::webserver(httpserver::create_webserver(port)));
auto ws = new httpserver::webserver(httpserver::create_webserver(port).start_method(httpserver::http::http_utils::THREAD_PER_CONNECTION));
servers_.push_back( ws );
}
......@@ -23,6 +23,9 @@ restate::Service::Service(xdaq::ApplicationStub* s) : xdaq::Application(s)
{
auto aFunc = std::bind(&restate::Service::render, this, std::placeholders::_1);
restate::register_resource("/user/service/{name}/{value|[0-9]+}",aFunc);
restate::register_resource("/call",std::bind(&restate::Service::call, this, std::placeholders::_1));
}
restate::Service::~Service()
......@@ -31,6 +34,8 @@ restate::Service::~Service()
const std::shared_ptr<restate::response> restate::Service::render(const restate::request& r)
{
std::cout<< "call restate::Service::render " << std::endl;
std::string name = r.get_arg("name");
unsigned int value = std::stoi(r.get_arg("value"));
......@@ -40,5 +45,22 @@ const std::shared_ptr<restate::response> restate::Service::render(const restate:
page << "<body>" << "<p>Received path " << r.get_path() << " extracted " << name << " with value " << std::to_string(value) << "<p></body></html>";
return std::shared_ptr<restate::response>(new restate::string(page.str(),httpserver::http::http_utils::http_ok, "text/html;charset=UTF-8"));
return std::shared_ptr<restate::response>(new restate::string(page.str(),httpserver::http::http_utils::http_ok, "text/html;charset=UTF-8"));
}
const std::shared_ptr<restate::response> restate::Service::call(const restate::request& r)
{
std::cout<< "call restate::Service::call " << std::endl;
// get a connection object to myself
auto conn = new restate::connection(this->getApplicationContext()->getContextDescriptor()->getURL());
std::shared_ptr<restate::result> result = conn->get("/user/service/call/1");
std::cout<< "result code " << result->getCode() << std::endl;
return std::shared_ptr<restate::response>(new restate::string(result->getContent(),httpserver::http::http_utils::http_ok, "text/plain;charset=UTF-8"));
}
#include "restate/api.h"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment