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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
#ifndef _WITPP_H
#define _WITPP_H
#pragma once
//include the headers
#include <ctime>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <json/json.h>
#include <curl/curl.h>
namespace witpp
{
//this callback function gets called when a data is going to be received
size_t writecb(char* buf, size_t sz, size_t items, void* ud)
{
std::string buffer=reinterpret_cast<char*>(ud);
buffer.clear();
buffer.append(buf, sz*items);
return sz*items;
}
//this class holds the parameters like authorization, version, etc
class Parameter
{
std::string version;
std::string server_token;
public:
Parameter()
{
time_t t=time(0);
struct tm n=*localtime(&t);
char buf[80];
strftime(buf, sizeof(buf), "%Y%m%d", &n);
version=buf;
}
Parameter& setVersion(std::string v)
{
version=v;
return *this;
}
std::string getVersion()
{
return version;
}
Parameter& setAuth(std::string auth)
{
server_token=auth;
return *this;
}
std::string getAuth()
{
return server_token;
}
};
//this class is an exception for wit.ai
class WitException: public std::exception
{
std::string errorText;
int errorCode;
public:
WitException(std::string e, int c):
errorText(e),
errorCode(c)
{
}
const char* what()
{
return errorText.c_str();
}
int getCode()
{
return errorCode;
}
};
//this class represents a responce which the request return's it
class Responce
{
protected:
Json::Value responce;
public:
Responce(std::string r)
{
Json::Reader reader;
if(!reader.parse(r, responce, true))
{
throw std::invalid_argument(reader.getFormatedErrorMessages());
}
if(responce.isMember("error")||responce.isMember("code"))
{
throw WitException(responce["error"].asString(), responce["code"].asInt());;
}
}
};
//this class is derived from Responce that shows a message (for MessageRequest and Speech Request)
class MessageResponce: public Responce
{
public:
MessageResponce(std::string s):
Responce(s)
{
}
std::string getMessageId()
{
return responce["msg_id"].asString();
}
std::string getText()
{
return responce["_text"].asString();
}
Json::Value& getEntities()
{
return responce["entities"];
}
};
//this class represents a request
class Request
{
protected:
CURL* c;
struct curl_slist* headers;
std::string host;
Parameter param;
Request()
{
host="https://api.wit.ai/";
c=curl_easy_init();
headers=curl_slist_append(nullptr, "Content-Type: application/json");
headers=curl_slist_append(headers, "Accept: application/json");
}
~Request()
{
curl_slist_free_all(headers);
curl_easy_cleanup(c);
}
Request& setHost(std::string h)
{
host=h;
return *this;
}
std::string getHost()
{
return host;
}
public:
Request& setParameter(Parameter& p)
{
param=p;
return *this;
}
Parameter& getParameter()
{
return param;
}
virtual Responce& perform()=0;
};
//this class is a request for message
class MessageRequest: public Request
{
CURLcode res;
std::string thread_id;
std::string message_id;
std::string message;
Context context;
bool has_context;
int n_best;
bool verbose;
public:
MessageRequest():
Request()
{
setHost(getHost()+"message");
has_context=false;
n_best=1;
}
MessageRequest& setMessage(std::string m)
{
message=m;
return *this;
}
std::string getMessage()
{
return message;
}
MessageRequest& setMessageId(std::string id)
{
message_id=id;
return *this;
}
std::string getMessageId()
{
return message_id;
}
MessageRequest& setThreadId(std::string id)
{
thread_id=id;
return *this;
}
std::string getThreadId()
{
return thread_id;
}
MessageRequest& setContext(Context c)
{
context=c;
has_context=true;
}
Context& getContext()
{
return context;
}
MessageRequest& setNBest(int n)
{
n_best=n;
return *this;
}
int getNBest()
{
return n_best;
}
MessageRequest&setVerbose(bool v)
{
verbose=v;
}
bool getVerbose()
{
return verbose;
}
Responce& perform()
{
std::string s=host+"?v="+param.getVersion();
if(has_context)
{
s+="&context="+(std::string)context;
}
if(message_id!="")
{
s+="&msg_id="+message_id;
}
if(thread_id!="")
{
s+="&thread_id="+thread_id;
}
s+="&n="+n_best;
s+="&verbose="+verbose?"1":"0";
std::string token_header="Authorization: Bearer "+param.getAuth();
headers=curl_slist_append(headers, token_header.c_str());
char*esc=curl_escape(s.c_str(), s.length());
s=esc;
res=curl_easy_setopt(c, CURLOPT_URL, s.c_str());
if(res!=0)
{
throw WitException((std::string)curl_easy_strerror(res), res);
}
res=curl_easy_setopt(c, CURLOPT_HTTPHEADER, headers);
if(res!=0)
{
throw WitException((std::string)curl_easy_strerror(res), res);
}
res=curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, writecb);
if(res!=0)
{
throw WitException((std::string)curl_easy_strerror(res), res);
}
std::string data;
res=curl_easy_setopt(c, CURLOPT_WRITEDATA, &data);
if(res!=0)
{
throw WitException((std::string)curl_easy_strerror(res), res);
}
res=curl_easy_perform(c);
if(res!=0)
{
throw WitException((std::string)curl_easy_strerror(res), res);
}
curl_free(esc);
return MessageResponce(data);
}
};
}
#endif //_WITPP_H
| |