restapi.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /** @file */
  2. // -------------------------------------------------------------------------
  3. // states, (result + error) codes
  4. // defined in C++
  5. enum ERestResultState
  6. {
  7. EREST_EMPTY, // not initialized
  8. EREST_PENDING, // awaiting processing
  9. EREST_FEEDING, // awaiting incoming data
  10. EREST_SUCCESS, // result and/ or data are ready (success), awaiting data processing to be finished (no longer blocking queue processing)
  11. EREST_PROCESSED, // finished (either successfully or with failure) and eill be removed ASAP
  12. EREST_ERROR, // (state >= EREST_ERROR) == error happened
  13. EREST_ERROR_CLIENTERROR, // (EREST_ERROR == EREST_ERROR_CLIENTERROR)
  14. EREST_ERROR_SERVERERROR,
  15. EREST_ERROR_APPERROR,
  16. EREST_ERROR_TIMEOUT,
  17. EREST_ERROR_NOTIMPLEMENTED,
  18. EREST_ERROR_UNKNOWN,
  19. };
  20. // -------------------------------------------------------------------------
  21. // options
  22. // defined in C++
  23. enum ERestOption
  24. {
  25. ERESTOPTION_UNKNOWN, // invalid option
  26. ERESTOPTION_READOPERATION, // read operation timeout (default 10sec)
  27. ERESTOPTION_CONNECTION, // connection timeout (default 10sec)
  28. // note: limit for timeout is between <3 .. 120> seconds, you cannot exceed this value
  29. };
  30. // -------------------------------------------------------------------------
  31. // object to be used from script for result binding
  32. //
  33. // [Example:]
  34. //
  35. // RestCallback cbx1 = new RestCallback;
  36. // RestContext ctx = GetRestApi().GetRestContext("http://somethingsomewhere.com/path/");
  37. // ctx.GET(cbx1,"RequestPath?Argument=Something");
  38. //
  39. // Event are then called upon RestCallback()
  40. //
  41. class RestCallback : Managed
  42. {
  43. /**
  44. \brief Called in case request failed (ERestResultState) - Note! May be called multiple times in case of (RetryCount > 1)
  45. */
  46. void OnError( int errorCode )
  47. {
  48. // override this with your implementation
  49. Print(" !!! OnError() ");
  50. };
  51. /**
  52. \brief Called in case request timed out or handled improperly (no error, no success, no data)
  53. */
  54. void OnTimeout()
  55. {
  56. // override this with your implementation
  57. Print(" !!! OnTimeout() ");
  58. };
  59. /**
  60. \brief Called when data arrived and/ or response processed successfully
  61. */
  62. void OnSuccess( string data, int dataSize )
  63. {
  64. // override this with your implementation
  65. Print(" !!! OnSuccess() size=" + dataSize );
  66. if( dataSize > 0 )
  67. Print(data); // !!! NOTE: Print() will not output string longer than 1024b, check your dataSize !!!
  68. };
  69. /**
  70. \brief Called when data arrived and/ or file created successfully
  71. */
  72. void OnFileCreated( string fileName, int dataSize )
  73. {
  74. // override this with your implementation
  75. Print(" !!! OnFileCreated() file=" + fileName + " size=" + dataSize );
  76. };
  77. };
  78. // -------------------------------------------------------------------------
  79. // context API and request API
  80. class RestContext
  81. {
  82. private void RestContext() {}
  83. private void ~RestContext() {}
  84. /**
  85. \brief Processes GET request and returns result (ERestResultState) and/ or data (timeout, error) when finished
  86. */
  87. proto native int GET( RestCallback cb, string request );
  88. /**
  89. \brief Processes GET request and returns data immediately (thread blocking operation!)
  90. */
  91. proto native string GET_now( string request );
  92. /**
  93. \brief Processes GET request and returns result (ERestResultState) and/ or stores data int specified file (timeout, error) when finished
  94. */
  95. proto native int FILE( RestCallback cb, string request, string filename );
  96. /**
  97. \brief Processes GET request and returns result (ERestResultState) and/ stores data int specified file immediately (thread blocking operation!)
  98. */
  99. proto native int FILE_now( string request, string filename );
  100. /**
  101. \brief Pushes POST request and returns result (ERestResultState) and/ or data (timeout, error) when finished
  102. */
  103. proto native int POST( RestCallback cb, string request, string data );
  104. /**
  105. \brief Processes POST request and returns data immediately (thread blocking operation!)
  106. */
  107. proto native string POST_now( string request, string data );
  108. /**
  109. \brief Clear all pending requests and buffers
  110. */
  111. proto native void reset();
  112. /**
  113. \brief Set Content-Type header (string)
  114. default content type is "application/octet-stream"
  115. but you can specify whatever you like, for example "application/json" "application/sql" "text/plain"
  116. */
  117. proto native void SetHeader( string value );
  118. };
  119. // -------------------------------------------------------------------------
  120. // RestApi core for context create/ access + debug features
  121. class RestApi
  122. {
  123. private void RestApi() {}
  124. private void ~RestApi() {}
  125. /**
  126. \brief Get new or existing context for http comm GetRestContext("www.server915.com/interface/")
  127. */
  128. proto native RestContext GetRestContext( string serverURL );
  129. /**
  130. \brief Get count of registered contexes
  131. */
  132. proto native int GetContextCount();
  133. /**
  134. \brief Enable debug output to console (disabled by default)
  135. */
  136. proto native void EnableDebug( bool bEnable );
  137. /**
  138. \brief List of all currently active contexes and processed (pending) requests
  139. */
  140. proto native void DebugList();
  141. /**
  142. \brief Set specific option (integer)
  143. */
  144. proto native void SetOption( int option, int value );
  145. };
  146. // -------------------------------------------------------------------------
  147. // RestApi create/ access methods out of Hive initialization
  148. proto native RestApi CreateRestApi();
  149. proto native void DestroyRestApi();
  150. proto native RestApi GetRestApi();