HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FS_WebAPI.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  *
8  * Sample usage:
9  * FS_WebAPIResponse response =
10  * FS_WebAPI("http://www.orbolt.com/api").call1(
11  * "is_asset_in_store", "SideFX::spaceship");
12  * response.myJSONResult.dump();
13  * std::cout << "\nerrors: " << response.myErrors.nonNullBuffer()
14  * << std::endl;
15  * Prints:
16  * true
17  * errors:
18  */
19 
20 #ifndef __HOM_WebAPI_h__
21 #define __HOM_WebAPI_h__
22 
23 #include "FS_API.h"
24 #include <UT/UT_JSONValue.h>
25 #include <UT/UT_JSONWriter.h>
26 #include <UT/UT_UniquePtr.h>
27 #include <UT/UT_DeepString.h>
28 #include <UT/UT_Lock.h>
29 #include <UT/UT_WorkBuffer.h>
30 
32 {
33 public:
36 };
37 
39 {
40 public:
41  FS_WebAPI(const char *api_url);
42 
43  void clearCookies();
44 
45  // This is a really simple implementation. It doesn't check for duplicates
46  // or anything like that.
47  void addCookie(const char *name, const char *value);
48 
49  void setProxy(const char *proxyUrl, int port);
50 
51  void clearProxy();
52 
53  // The following methods let you make API calls to named functions in
54  // the Web API. Call the appropriate method for the given number of
55  // arguments.
56  FS_WebAPIResponse call0(const char *api_function) const;
57 
58  template <typename T1>
59  FS_WebAPIResponse call1(const char *api_function,
60  const T1 &arg1) const;
61 
62  template <typename T1, typename T2>
63  FS_WebAPIResponse call2(const char *api_function,
64  const T1 &arg1, const T2 &arg2) const;
65 
66  template <typename T1, typename T2, typename T3>
67  FS_WebAPIResponse call3(const char *api_function,
68  const T1 &arg1, const T2 &arg2, const T3 &arg3) const;
69 
70  template <typename T1, typename T2, typename T3, typename T4>
71  FS_WebAPIResponse call4(const char *api_function,
72  const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4) const;
73 
74  template <typename T1, typename T2, typename T3, typename T4, typename T5>
75  FS_WebAPIResponse call5(const char *api_function,
76  const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4,
77  const T5 &arg5) const;
78 
79 private:
80  FS_WebAPIResponse callFunctionWithJSONArgs(
81  const UT_WorkBuffer &json_data) const;
82 
83  void ensureCURLIsInitialized();
84 
85  void writeAPICallPrefixToJSON(
86  UT_JSONWriter &writer, const char *api_function) const;
87  void writeAPICallSuffixToJSON(UT_JSONWriter &writer) const;
88 
89  // These helper templated functions know how to write different value types
90  // to a UT_JSONWriter.
91  // TODO: Make these templated methods of UT_JSONWriter.
92  template <typename T>
93  void writeValue(UT_JSONWriter &writer, T value) const
94  { writer.jsonValue(value); }
95 
96  UT_String myAPIURL;
97  UT_StringHolder myProxyHostname;
98  int myProxyPort;
99  UT_Lock myCurlInitLock;
100  UT_WorkBuffer myCookies;
101  bool myHasInitializedCurl;
102 };
103 
104 template <> FS_API
105 void FS_WebAPI::writeValue<const char *>(
106  UT_JSONWriter &writer, const char *value) const;
107 
108 template <> FS_API
109 void FS_WebAPI::writeValue<std::string>(
110  UT_JSONWriter &writer, std::string value) const;
111 
112 template <> FS_API
113 void FS_WebAPI::writeValue<UT_JSONValue>(
114  UT_JSONWriter &writer, UT_JSONValue value) const;
115 
116 //---------------------------------------------------------------------------
117 
118 
119 template <typename T1>
122  const char *api_function, const T1 &arg1) const
123 {
124  UT_WorkBuffer json_data;
125  UT_UniquePtr<UT_JSONWriter> json_writer(
126  UT_JSONWriter::allocWriter(json_data));
127 
128  writeAPICallPrefixToJSON(*json_writer, api_function);
129  writeValue(*json_writer, arg1);
130  writeAPICallSuffixToJSON(*json_writer);
131 
132  return callFunctionWithJSONArgs(json_data);
133 }
134 
135 template <typename T1, typename T2>
138  const char *api_function,
139  const T1 &arg1, const T2 &arg2) const
140 {
141  UT_WorkBuffer json_data;
142  UT_UniquePtr<UT_JSONWriter> json_writer(
143  UT_JSONWriter::allocWriter(json_data));
144 
145  writeAPICallPrefixToJSON(*json_writer, api_function);
146  writeValue(*json_writer, arg1);
147  writeValue(*json_writer, arg2);
148  writeAPICallSuffixToJSON(*json_writer);
149 
150  return callFunctionWithJSONArgs(json_data);
151 }
152 
153 template <typename T1, typename T2, typename T3>
156  const char *api_function,
157  const T1 &arg1, const T2 &arg2, const T3 &arg3) const
158 {
159  UT_WorkBuffer json_data;
160  UT_UniquePtr<UT_JSONWriter> json_writer(
161  UT_JSONWriter::allocWriter(json_data));
162 
163  writeAPICallPrefixToJSON(*json_writer, api_function);
164  writeValue(*json_writer, arg1);
165  writeValue(*json_writer, arg2);
166  writeValue(*json_writer, arg3);
167  writeAPICallSuffixToJSON(*json_writer);
168 
169  return callFunctionWithJSONArgs(json_data);
170 }
171 
172 template <typename T1, typename T2, typename T3, typename T4>
175  const char *api_function,
176  const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4) const
177 {
178  UT_WorkBuffer json_data;
179  UT_UniquePtr<UT_JSONWriter> json_writer(
180  UT_JSONWriter::allocWriter(json_data));
181 
182  writeAPICallPrefixToJSON(*json_writer, api_function);
183  writeValue(*json_writer, arg1);
184  writeValue(*json_writer, arg2);
185  writeValue(*json_writer, arg3);
186  writeValue(*json_writer, arg4);
187  writeAPICallSuffixToJSON(*json_writer);
188 
189  return callFunctionWithJSONArgs(json_data);
190 }
191 
192 template <typename T1, typename T2, typename T3, typename T4, typename T5>
195  const char *api_function,
196  const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4,
197  const T5 &arg5) const
198 {
199  UT_WorkBuffer json_data;
200  UT_UniquePtr<UT_JSONWriter> json_writer(
201  UT_JSONWriter::allocWriter(json_data));
202 
203  writeAPICallPrefixToJSON(*json_writer, api_function);
204  writeValue(*json_writer, arg1);
205  writeValue(*json_writer, arg2);
206  writeValue(*json_writer, arg3);
207  writeValue(*json_writer, arg4);
208  writeValue(*json_writer, arg5);
209  writeAPICallSuffixToJSON(*json_writer);
210 
211  return callFunctionWithJSONArgs(json_data);
212 }
213 
214 #endif
bool jsonValue(bool value)
FS_WebAPIResponse call5(const char *api_function, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5) const
Definition: FS_WebAPI.h:194
FS_WebAPIResponse call4(const char *api_function, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4) const
Definition: FS_WebAPI.h:174
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
static UT_JSONWriter * allocWriter(UT_WorkBuffer &buffer)
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
FS_WebAPIResponse call2(const char *api_function, const T1 &arg1, const T2 &arg2) const
Definition: FS_WebAPI.h:137
UT_JSONValue myJSONResult
Definition: FS_WebAPI.h:34
FS_WebAPIResponse call1(const char *api_function, const T1 &arg1) const
Definition: FS_WebAPI.h:121
GLuint const GLchar * name
Definition: glcorearb.h:785
UT_DeepString myErrors
Definition: FS_WebAPI.h:35
GLsizei const GLfloat * value
Definition: glcorearb.h:823
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:75
FS_WebAPIResponse call3(const char *api_function, const T1 &arg1, const T2 &arg2, const T3 &arg3) const
Definition: FS_WebAPI.h:155
#define FS_API
Definition: FS_API.h:10