HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #include <UT/UT_Map.h>
31 
33 {
34 public:
37 };
38 
40 {
41 public:
42  FS_WebAPI(const char *api_url);
43 
44  void setProxy(const char *proxyUrl, int port);
45 
46  void clearProxy();
47 
48  FS_WebAPIResponse post(
49  const UT_Map<UT_StringHolder, UT_StringHolder> &variables) const;
50 
51  // The following methods let you make API calls to named functions in
52  // the Web API. Call the appropriate method for the given number of
53  // arguments.
54  FS_WebAPIResponse call0(const char *api_function) const;
55 
56  template <typename T1>
57  FS_WebAPIResponse call1(const char *api_function,
58  const T1 &arg1) const;
59 
60  template <typename T1, typename T2>
61  FS_WebAPIResponse call2(const char *api_function,
62  const T1 &arg1, const T2 &arg2) const;
63 
64  template <typename T1, typename T2, typename T3>
65  FS_WebAPIResponse call3(const char *api_function,
66  const T1 &arg1, const T2 &arg2, const T3 &arg3) const;
67 
68  template <typename T1, typename T2, typename T3, typename T4>
69  FS_WebAPIResponse call4(const char *api_function,
70  const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4) const;
71 
72  template <typename T1, typename T2, typename T3, typename T4, typename T5>
73  FS_WebAPIResponse call5(const char *api_function,
74  const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4,
75  const T5 &arg5) const;
76 
77 private:
78  // This is a really simple implementation. It doesn't check for duplicates
79  // or anything like that.
80  void addCookie(const char *name, const char *value);
81  void clearCookies();
82 
83  FS_WebAPIResponse callFunctionWithJSONArgs(
84  const UT_WorkBuffer &json_data) const;
85 
86  FS_WebAPIResponse postAndGetJSONResponse(
87  void *curl, const char *post_data) const;
88 
89  void chooseCSRFToken(UT_WorkBuffer &csrf_token) const;
90 
91  void ensureCURLIsInitialized();
92 
93  void writeAPICallPrefixToJSON(
94  UT_JSONWriter &writer, const char *api_function) const;
95  void writeAPICallSuffixToJSON(UT_JSONWriter &writer) const;
96 
97  // These helper templated functions know how to write different value types
98  // to a UT_JSONWriter.
99  // TODO: Make these templated methods of UT_JSONWriter.
100  template <typename T>
101  void writeValue(UT_JSONWriter &writer, T value) const
102  { writer.jsonValue(value); }
103 
104  UT_String myAPIURL;
105  UT_StringHolder myProxyHostname;
106  int myProxyPort;
107  UT_Lock myCurlInitLock;
108  UT_WorkBuffer myCookies;
109  bool myHasInitializedCurl;
110 };
111 
112 //---------------------------------------------------------------------------
113 
114 
115 template <typename T1>
118  const char *api_function, const T1 &arg1) const
119 {
120  UT_WorkBuffer json_data;
121  UT_UniquePtr<UT_JSONWriter> json_writer(
122  UT_JSONWriter::allocWriter(json_data));
123 
124  writeAPICallPrefixToJSON(*json_writer, api_function);
125  writeValue(*json_writer, arg1);
126  writeAPICallSuffixToJSON(*json_writer);
127 
128  return callFunctionWithJSONArgs(json_data);
129 }
130 
131 template <typename T1, typename T2>
134  const char *api_function,
135  const T1 &arg1, const T2 &arg2) const
136 {
137  UT_WorkBuffer json_data;
138  UT_UniquePtr<UT_JSONWriter> json_writer(
139  UT_JSONWriter::allocWriter(json_data));
140 
141  writeAPICallPrefixToJSON(*json_writer, api_function);
142  writeValue(*json_writer, arg1);
143  writeValue(*json_writer, arg2);
144  writeAPICallSuffixToJSON(*json_writer);
145 
146  return callFunctionWithJSONArgs(json_data);
147 }
148 
149 template <typename T1, typename T2, typename T3>
152  const char *api_function,
153  const T1 &arg1, const T2 &arg2, const T3 &arg3) const
154 {
155  UT_WorkBuffer json_data;
156  UT_UniquePtr<UT_JSONWriter> json_writer(
157  UT_JSONWriter::allocWriter(json_data));
158 
159  writeAPICallPrefixToJSON(*json_writer, api_function);
160  writeValue(*json_writer, arg1);
161  writeValue(*json_writer, arg2);
162  writeValue(*json_writer, arg3);
163  writeAPICallSuffixToJSON(*json_writer);
164 
165  return callFunctionWithJSONArgs(json_data);
166 }
167 
168 template <typename T1, typename T2, typename T3, typename T4>
171  const char *api_function,
172  const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4) const
173 {
174  UT_WorkBuffer json_data;
175  UT_UniquePtr<UT_JSONWriter> json_writer(
176  UT_JSONWriter::allocWriter(json_data));
177 
178  writeAPICallPrefixToJSON(*json_writer, api_function);
179  writeValue(*json_writer, arg1);
180  writeValue(*json_writer, arg2);
181  writeValue(*json_writer, arg3);
182  writeValue(*json_writer, arg4);
183  writeAPICallSuffixToJSON(*json_writer);
184 
185  return callFunctionWithJSONArgs(json_data);
186 }
187 
188 template <typename T1, typename T2, typename T3, typename T4, typename T5>
191  const char *api_function,
192  const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4,
193  const T5 &arg5) const
194 {
195  UT_WorkBuffer json_data;
196  UT_UniquePtr<UT_JSONWriter> json_writer(
197  UT_JSONWriter::allocWriter(json_data));
198 
199  writeAPICallPrefixToJSON(*json_writer, api_function);
200  writeValue(*json_writer, arg1);
201  writeValue(*json_writer, arg2);
202  writeValue(*json_writer, arg3);
203  writeValue(*json_writer, arg4);
204  writeValue(*json_writer, arg5);
205  writeAPICallSuffixToJSON(*json_writer);
206 
207  return callFunctionWithJSONArgs(json_data);
208 }
209 
210 #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:190
Unsorted map container.
Definition: UT_Map.h:83
FS_WebAPIResponse call4(const char *api_function, const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4) const
Definition: FS_WebAPI.h:170
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:133
UT_JSONValue myJSONResult
Definition: FS_WebAPI.h:35
FS_WebAPIResponse call1(const char *api_function, const T1 &arg1) const
Definition: FS_WebAPI.h:117
GLuint const GLchar * name
Definition: glcorearb.h:785
UT_DeepString myErrors
Definition: FS_WebAPI.h:36
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:151
GridType::Ptr curl(const GridType &grid, bool threaded, InterruptT *interrupt)
Compute the curl of the given vector-valued grid.
#define FS_API
Definition: FS_API.h:10