Im having a couple of small issues whilst attempting to add the
ability to handle REST based api calls with Json
Im trying to create an action controller that will handle:
mapping
/api/v1/test/...... => json_test
/api/v1/test/test1/...... => json_test.test1
/api/v1/test/test2/..... => json_test.test2
My UrlMaps are:
// Add the API routes
1. favcoreApp.mapUrl("^/api/v1/test/(.*)$", "json_test");
2. favcoreApp.mapUrl("^/api/v1/test/test1/(.*)$",
"json_test.test1");
3. favcoreApp.mapUrl("^/api/v1/test/test2/(.*)$",
"json_test.test2");
Problem 1. The definition for the "json_test" mapping call (1) masks
all the other calls, but that may be related to problem 2.
Problem 2. I cant access the sub-components json_test.test1 or
json_test.test2 , they don't seem to be registering and I cant see why
not. The main component seems to be registering fine.
Problem 3. I would like to parse the unmatched part of the URL ($1) to
the component so it can further decode the request and act on it. is
there an easy way of doing that, I can easily fish the whole request
out of the RequestObject, but I would have to remove the prefix, and
that requires the prefix to be set-up in two places, in the URLMap and
in the component, which is not very cool.
for example http://fav.or.it/api/v1/test/channel/200/categories/listall
I would like to get the "channel/200/categories/listall" passed to
the component.
Any Ideas?
Code for the controller is..., i just hacked the output from ecppc
Im also curious about this line in the class
_component_json_test(const tnt::Compident& ci, const
tnt::Urlmapper& um, tnt::Comploader& cl);
any explanations about what it does?
========== json_test.cpp =============================================
#include <tnt/ecpp.h>
#include <tnt/convert.h>
#include <tnt/httprequest.h>
#include <tnt/httpreply.h>
#include <tnt/httpheader.h>
#include <tnt/http.h>
#include <tnt/data.h>
#include <tnt/componentfactory.h>
#include <cxxtools/log.h>
#include <stdexcept>
log_define("component.json_test")
#include "classes/Utils.h"
namespace
{
template <typename T> inline void use(const T&) { }
class _component_json_test : public tnt::EcppComponent
{
_component_json_test& main() { return *this; }
protected:
~_component_json_test();
public:
_component_json_test(const tnt::Compident& ci, const
tnt::Urlmapper& um, tnt::Comploader& cl);
unsigned operator() (tnt::HttpRequest& request, tnt::HttpReply&
reply, tnt::QueryParams& qparam);
class test1_type : public tnt::EcppSubComponent
{
_component_json_test& mainComp;
_component_json_test& main() { return mainComp; }
public:
test1_type(_component_json_test& m, const std::string& name)
: EcppSubComponent(m, name),
mainComp(m)
{ }
unsigned operator() (tnt::HttpRequest& request,
tnt::HttpReply& reply, tnt::QueryParams& qparam);
};
class test2_type : public tnt::EcppSubComponent
{
_component_json_test& mainComp;
_component_json_test& main() { return mainComp; }
public:
test2_type(_component_json_test& m, const std::string& name)
: EcppSubComponent(m, name),
mainComp(m)
{ }
unsigned operator() (tnt::HttpRequest& request,
tnt::HttpReply& reply, tnt::QueryParams& qparam);
};
test1_type test1;
test2_type test2;
};
static tnt::ComponentFactoryImpl<_component_json_test>
json_testFactory("json_test");
#define SET_LANG(lang) \
do \
{ \
request.setLang(lang); \
reply.setLocale(request.getLocale()); \
} while (false)
_component_json_test::_component_json_test(const tnt::Compident& ci,
const tnt::Urlmapper& um, tnt::Comploader& cl)
: EcppComponent(ci, um, cl),
test1(*this, "test1"),
test2(*this, "test2")
{
}
_component_json_test::~_component_json_test()
{
}
unsigned _component_json_test::operator() (tnt::HttpRequest& request,
tnt::HttpReply& reply, tnt::QueryParams& qparam)
{
std::string version = Utils::getVersion();
reply.out() << "{ \"status\": \"ok\", \"version\": \"" << version
<< "\", \"method\": \"test\" }";
return HTTP_OK;
}
unsigned _component_json_test::test1_type::operator()
(tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams&
qparam)
{
std::string version = Utils::getVersion();
reply.out() << "{ \"status\": \"ok\", \"version\": \"" << version <<
"\", \"method\": \"test/test1\" }";
return HTTP_OK;
}
unsigned _component_json_test::test2_type::operator()
(tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams&
qparam)
{
std::string version = Utils::getVersion();
reply.out() << "{ \"status\": \"ok\", \"version\": \"" << version <<
"\", \"method\": \"test/test2\" }";
return HTTP_OK;
}
} // namespace
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general