diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-16 06:00:12 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-16 06:00:12 +0000 |
commit | ec5f59dac8d5eca3504ec5fe205dcb7d8fd382a6 (patch) | |
tree | 41edf192475639ca342a84efc3e1d94bf0a65672 /cpp/sca/modules | |
parent | 24842067f999efb5e62f1dd3cde017df1381c0a3 (diff) |
Fixed support for nested elements and lists. Improved failure/error reporting monad. Minor code cleanup.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@880598 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/sca/modules')
-rw-r--r-- | cpp/sca/modules/atom/atom.hpp | 10 | ||||
-rw-r--r-- | cpp/sca/modules/http/curl.hpp | 58 | ||||
-rw-r--r-- | cpp/sca/modules/json/json.hpp | 15 |
3 files changed, 50 insertions, 33 deletions
diff --git a/cpp/sca/modules/atom/atom.hpp b/cpp/sca/modules/atom/atom.hpp index 2077081320..5054c635a0 100644 --- a/cpp/sca/modules/atom/atom.hpp +++ b/cpp/sca/modules/atom/atom.hpp @@ -52,7 +52,7 @@ const list<value> entryValue(const list<value>& e) { */ const list<value> entriesValues(const list<value>& e) { if (isNil(e)) - return list<value>(); + return e; return cons<value>(entryValue(car(e)), entriesValues(cdr(e))); } @@ -62,7 +62,7 @@ const list<value> entriesValues(const list<value>& e) { const failable<list<value>, std::string> readEntry(const list<std::string>& ilist) { const list<value> e = readXML(ilist); if (isNil(e)) - return std::string("Empty entry"); + return mkfailure<list<value>, std::string>("Empty entry"); return entryValue(car(e)); } @@ -72,7 +72,7 @@ const failable<list<value>, std::string> readEntry(const list<std::string>& ilis const failable<list<value>, std::string> readFeed(const list<std::string>& ilist) { const list<value> f = readXML(ilist); if (isNil(f)) - return std::string("Empty feed"); + return mkfailure<list<value>, std::string>("Empty feed"); const list<value> t = filter<value>(selector(mklist<value>(element, "title")), car(f)); const list<value> i = filter<value>(selector(mklist<value>(element, "id")), car(f)); const list<value> e = filter<value>(selector(mklist<value>(element, "entry")), car(f)); @@ -99,7 +99,7 @@ const list<value> entryElement(const list<value>& l) { */ const list<value> entriesElements(const list<value>& l) { if (isNil(l)) - return list<value>(); + return l; return cons<value>(entryElement(car(l)), entriesElements(cdr(l))); } @@ -156,7 +156,7 @@ const list<value> entryValuesToElements(const list<value> val) { */ const list<value> feedValuesToElementsLoop(const list<value> val) { if (isNil(val)) - return list<value>(); + return val; return cons<value>(entryValuesToElements(car(val)), feedValuesToElementsLoop(cdr(val))); } diff --git a/cpp/sca/modules/http/curl.hpp b/cpp/sca/modules/http/curl.hpp index c0d79cef13..5ee3a090b0 100644 --- a/cpp/sca/modules/http/curl.hpp +++ b/cpp/sca/modules/http/curl.hpp @@ -137,13 +137,13 @@ template<typename R> size_t headerCallback(void *ptr, size_t size, size_t nmemb, * Apply an HTTP verb to a list containing a list of headers and a list of content, and * a reduce function used to process the response. */ -curl_slist* headers(curl_slist* cl, const list<std::string> h) { +curl_slist* headers(curl_slist* cl, const list<std::string>& h) { if (isNil(h)) return cl; return headers(curl_slist_append(cl, std::string(car(h)).c_str()), cdr(h)); } -template<typename R> const failable<list<R>, std::string> apply(const list<list<std::string> > req, const lambda<R(std::string, R)>& reduce, const R& initial, const std::string& url, const std::string& verb, CURLHandle& ch) { +template<typename R> const failable<list<R>, std::string> apply(const list<list<std::string> >& req, const lambda<R(std::string, R)>& reduce, const R& initial, const std::string& url, const std::string& verb, const CURLHandle& ch) { // Init the curl session curl_easy_reset(ch); @@ -190,13 +190,13 @@ template<typename R> const failable<list<R>, std::string> apply(const list<list< // Return the HTTP return code or content if (rc) - return std::string(curl_easy_strerror(rc)); + return mkfailure<list<R>, std::string>(curl_easy_strerror(rc)); long httprc; curl_easy_getinfo (ch, CURLINFO_RESPONSE_CODE, &httprc); if (httprc != 200 && httprc != 201) { std::ostringstream es; es << "HTTP code " << httprc; - return es.str(); + return mkfailure<list<R>, std::string>(es.str()); } return mklist<R>(hcx.accum, wcx.accum); } @@ -204,13 +204,13 @@ template<typename R> const failable<list<R>, std::string> apply(const list<list< /** * Evaluate an expression remotely, at the given URL. */ -const failable<value, std::string> evalExpr(const value expr, const std::string& url, CURLHandle& ch) { +const failable<value, std::string> evalExpr(const value& expr, const std::string& url, const CURLHandle& ch) { // Convert expression to a JSON-RPC request json::JSONContext cx; const failable<list<std::string>, std::string> jsreq = jsonRequest(1, car<value>(expr), cdr<value>(expr), cx); if (!hasValue(jsreq)) - return std::string(jsreq); + return mkfailure<value, std::string>(reason(jsreq)); if (logContent) { std::cout<< "content: " << std::endl; @@ -223,7 +223,7 @@ const failable<value, std::string> evalExpr(const value expr, const std::string const list<std::string> h = mklist<std::string>("Content-Type: application/json-rpc"); const failable<list<list<std::string> >, std::string> res = apply<list<std::string> >(mklist<list<std::string> >(h, jsreq), rcons<std::string>, list<std::string>(), url, "POST", ch); if (!hasValue(res)) - return std::string(res); + return mkfailure<value, std::string>(reason(res)); // Return result if (logContent) { @@ -238,7 +238,7 @@ const failable<value, std::string> evalExpr(const value expr, const std::string /** * HTTP GET, return the resource at the given URL. */ -template<typename R> const failable<list<R>, std::string> get(const lambda<R(std::string, R)>& reduce, const R& initial, const std::string& url, CURLHandle& ch) { +template<typename R> const failable<list<R>, std::string> get(const lambda<R(std::string, R)>& reduce, const R& initial, const std::string& url, const CURLHandle& ch) { const list<list<std::string> > req = mklist(list<std::string>(), list<std::string>()); return apply(req, reduce, initial, url, "GET", ch); } @@ -246,17 +246,17 @@ template<typename R> const failable<list<R>, std::string> get(const lambda<R(std /** * HTTP GET, return a list of values representing the resource at the given URL. */ -const failable<value, std::string> get(const std::string& url, CURLHandle& ch) { +const failable<value, std::string> get(const std::string& url, const CURLHandle& ch) { // Get the contents of the resource at the given URL const failable<list<list<std::string> >, std::string> res = get<list<std::string> >(rcons<std::string>, list<std::string>(), url, ch); if (!hasValue(res)) - return std::string(res); + return mkfailure<value, std::string>(reason(res)); const list<list<std::string> > ls = res; - // TODO Return an ATOM feed const std::string ct; if (ct.find("application/atom+xml") != std::string::npos) { + // TODO Return an ATOM feed } // Return the content as a string value @@ -268,12 +268,12 @@ const failable<value, std::string> get(const std::string& url, CURLHandle& ch) { /** * HTTP POST. */ -const failable<value, std::string> post(const value& val, const std::string& url, CURLHandle& ch) { +const failable<value, std::string> post(const value& val, const std::string& url, const CURLHandle& ch) { // Convert value to an ATOM entry const failable<list<std::string>, std::string> entry = atom::writeATOMEntry(atom::entryValuesToElements(val)); if (!hasValue(entry)) - return std::string(entry); + return mkfailure<value, std::string>(reason(entry)); if (logContent) { std::cout << "content:" << std::endl; write(list<std::string>(entry), std::cout); @@ -285,45 +285,63 @@ const failable<value, std::string> post(const value& val, const std::string& url const list<list<std::string> > req = mklist<list<std::string> >(h, entry); const failable<list<list<std::string> >, std::string> res = apply<list<std::string> >(req, rcons<std::string>, list<std::string>(), url, "POST", ch); if (!hasValue(res)) - return std::string(res); + return mkfailure<value, std::string>(reason(res)); return value(true); } /** * HTTP PUT. */ -const failable<value, std::string> put(const value& val, const std::string& url, CURLHandle& ch) { +const failable<value, std::string> put(const value& val, const std::string& url, const CURLHandle& ch) { // Convert value to an ATOM entry const failable<list<std::string>, std::string> entry = atom::writeATOMEntry(atom::entryValuesToElements(val)); if (!hasValue(entry)) - return std::string(entry); + return mkfailure<value, std::string>(reason(entry)); if (logContent) { std::cout << "content:" << std::endl; write(list<std::string>(entry), std::cout); std::cout << std::endl; } - // POST it to the URL + // PUT it to the URL const list<std::string> h = mklist<std::string>("Content-Type: application/atom+xml"); const list<list<std::string> > req = mklist<list<std::string> >(h, entry); const failable<list<list<std::string> >, std::string> res = apply<list<std::string> >(req, rcons<std::string>, list<std::string>(), url, "PUT", ch); if (!hasValue(res)) - return std::string(res); + return mkfailure<value, std::string>(reason(res)); return value(true); } /** * HTTP DELETE. */ -const failable<value, std::string> del(const std::string& url, CURLHandle& ch) { +const failable<value, std::string> del(const std::string& url, const CURLHandle& ch) { const list<list<std::string> > req = mklist(list<std::string>(), list<std::string>()); const failable<list<list<std::string> >, std::string> res = apply<list<std::string> >(req, rcons<std::string>, list<std::string>(), url, "DELETE", ch); if (!hasValue(res)) - return std::string(res); + return mkfailure<value, std::string>(reason(res)); return value(true); } +/** + * HTTP client proxy function. + */ +struct proxy { + proxy(const std::string& url, const CURLHandle& ch) : url(url), ch(ch) { + } + + const value operator()(const list<value>& args) const { + failable<value, std::string> val = evalExpr(args, url, ch); + if (!hasValue(val)) + return value(); + return val; + } + + const std::string url; + const CURLHandle& ch; +}; + } } diff --git a/cpp/sca/modules/json/json.hpp b/cpp/sca/modules/json/json.hpp index 0d21cfe359..f6c8eb5fe8 100644 --- a/cpp/sca/modules/json/json.hpp +++ b/cpp/sca/modules/json/json.hpp @@ -200,7 +200,7 @@ failable<bool, std::string> consume(JSONParser* parser, const list<std::string>& return true; JSString* jstr = JS_NewStringCopyZ(cx, car(ilist).c_str()); if(!JS_ConsumeJSONText(cx, parser, JS_GetStringChars(jstr), JS_GetStringLength(jstr))) - return "JS_ConsumeJSONText failed"; + return mkfailure<bool, std::string>("JS_ConsumeJSONText failed"); return consume(parser, cdr(ilist), cx); } @@ -211,14 +211,14 @@ const failable<list<value>, std::string> readJSON(const list<std::string>& ilist jsval val; JSONParser* parser = JS_BeginJSONParse(cx, &val); if(parser == NULL) - return std::string("JS_BeginJSONParse failed"); + return mkfailure<list<value>, std::string>("JS_BeginJSONParse failed"); const failable<bool, std::string> consumed = consume(parser, ilist, cx); if(!JS_FinishJSONParse(cx, parser, JSVAL_NULL)) - return std::string("JS_FinishJSONParse failed"); + return mkfailure<list<value>, std::string>("JS_FinishJSONParse failed"); if(!hasValue(consumed)) - return std::string(consumed); + return mkfailure<list<value>, std::string>(reason(consumed)); return list<value>(jsValToValue(val, cx)); } @@ -357,11 +357,11 @@ template<typename R> const failable<R, std::string> writeJSON(const lambda<R(std jsval val = OBJECT_TO_JSVAL(o); const failable<bool, std::string> w = writeList(l, o, cx); if (!hasValue(w)) - return std::string(w); + return mkfailure<R, std::string>(reason(w)); WriteContext<R> wcx(reduce, initial, cx); if (!JS_Stringify(cx, &val, NULL, JSVAL_NULL, writeCallback<R>, &wcx)) - return std::string("JS_Stringify failed"); + return mkfailure<R, std::string>("JS_Stringify failed"); return wcx.accum; } @@ -380,8 +380,7 @@ const failable<list<std::string>, std::string> writeJSON(const list<value>& l, c */ const failable<list<std::string>, std::string> jsonRequest(const value& id, const value& func, const value& params, json::JSONContext& cx) { const list<value> r = mklist<value>(mklist<value>("id", id), mklist<value>("method", func), mklist<value>("params", params)); - failable<list<std::string>, std::string> ls = writeJSON(valuesToElements(r), cx); - return ls; + return writeJSON(valuesToElements(r), cx); } /** |