1 /** 2 A node with the ability to send HTTP requests. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.httprequest; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.meta; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.object; 22 import godot.classdb; 23 import godot.node; 24 import godot.httpclient; 25 /** 26 A node with the ability to send HTTP requests. 27 28 Uses $(D HTTPClient) internally. 29 Can be used to make HTTP requests, i.e. download or upload files or web content via HTTP. 30 */ 31 @GodotBaseClass struct HTTPRequest 32 { 33 enum string _GODOT_internal_name = "HTTPRequest"; 34 public: 35 @nogc nothrow: 36 union { godot_object _godot_object; Node _GODOT_base; } 37 alias _GODOT_base this; 38 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 39 package(godot) __gshared bool _classBindingInitialized = false; 40 package(godot) static struct _classBinding 41 { 42 __gshared: 43 @GodotName("request") GodotMethod!(GodotError, String, PoolStringArray, bool, long, String) request; 44 @GodotName("cancel_request") GodotMethod!(void) cancelRequest; 45 @GodotName("get_http_client_status") GodotMethod!(HTTPClient.Status) getHttpClientStatus; 46 @GodotName("set_use_threads") GodotMethod!(void, bool) setUseThreads; 47 @GodotName("is_using_threads") GodotMethod!(bool) isUsingThreads; 48 @GodotName("set_body_size_limit") GodotMethod!(void, long) setBodySizeLimit; 49 @GodotName("get_body_size_limit") GodotMethod!(long) getBodySizeLimit; 50 @GodotName("set_max_redirects") GodotMethod!(void, long) setMaxRedirects; 51 @GodotName("get_max_redirects") GodotMethod!(long) getMaxRedirects; 52 @GodotName("set_download_file") GodotMethod!(void, String) setDownloadFile; 53 @GodotName("get_download_file") GodotMethod!(String) getDownloadFile; 54 @GodotName("get_downloaded_bytes") GodotMethod!(long) getDownloadedBytes; 55 @GodotName("get_body_size") GodotMethod!(long) getBodySize; 56 @GodotName("_redirect_request") GodotMethod!(void, String) _redirectRequest; 57 @GodotName("_request_done") GodotMethod!(void, long, long, PoolStringArray, PoolByteArray) _requestDone; 58 } 59 bool opEquals(in HTTPRequest other) const { return _godot_object.ptr is other._godot_object.ptr; } 60 HTTPRequest opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 61 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 62 mixin baseCasts; 63 static HTTPRequest _new() 64 { 65 static godot_class_constructor constructor; 66 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("HTTPRequest"); 67 if(constructor is null) return typeof(this).init; 68 return cast(HTTPRequest)(constructor()); 69 } 70 @disable new(size_t s); 71 /// 72 enum Result : int 73 { 74 /** 75 Request successful. 76 */ 77 resultSuccess = 0, 78 /** 79 80 */ 81 resultChunkedBodySizeMismatch = 1, 82 /** 83 Request failed while connecting. 84 */ 85 resultCantConnect = 2, 86 /** 87 Request failed while resolving. 88 */ 89 resultCantResolve = 3, 90 /** 91 Request failed due to connection(read/write) error. 92 */ 93 resultConnectionError = 4, 94 /** 95 Request failed on SSL handshake. 96 */ 97 resultSslHandshakeError = 5, 98 /** 99 Request does not have a response(yet). 100 */ 101 resultNoResponse = 6, 102 /** 103 Request exceeded its maximum size limit, see $(D setBodySizeLimit). 104 */ 105 resultBodySizeLimitExceeded = 7, 106 /** 107 Request failed. (Unused) 108 */ 109 resultRequestFailed = 8, 110 /** 111 HTTPRequest couldn't open the download file. 112 */ 113 resultDownloadFileCantOpen = 9, 114 /** 115 HTTPRequest couldn't write to the download file. 116 */ 117 resultDownloadFileWriteError = 10, 118 /** 119 Request reached its maximum redirect limit, see $(D setMaxRedirects). 120 */ 121 resultRedirectLimitReached = 11, 122 } 123 /// 124 enum Constants : int 125 { 126 resultSuccess = 0, 127 resultChunkedBodySizeMismatch = 1, 128 resultCantConnect = 2, 129 resultCantResolve = 3, 130 resultConnectionError = 4, 131 resultSslHandshakeError = 5, 132 resultNoResponse = 6, 133 resultBodySizeLimitExceeded = 7, 134 resultRequestFailed = 8, 135 resultDownloadFileCantOpen = 9, 136 resultDownloadFileWriteError = 10, 137 resultRedirectLimitReached = 11, 138 } 139 /** 140 Creates request on the underlying $(D HTTPClient). If there is no configuration errors, it tries to connect using $(D HTTPClient.connectToHost) and passes parameters onto $(D HTTPClient.request). 141 Returns `OK` if request is successfully created. (Does not imply that the server has responded), `ERR_UNCONFIGURED` if not in the tree, `ERR_BUSY` if still processing previous request, `ERR_INVALID_PARAMETER` if given string is not a valid URL format, or `ERR_CANT_CONNECT` if not using thread and the $(D HTTPClient) cannot connect to host. 142 */ 143 GodotError request(StringArg0, StringArg4)(in StringArg0 url, in PoolStringArray custom_headers = PoolStringArray.init, in bool ssl_validate_domain = true, in long method = 0, in StringArg4 request_data = "") 144 { 145 checkClassBinding!(typeof(this))(); 146 return ptrcall!(GodotError)(_classBinding.request, _godot_object, url, custom_headers, ssl_validate_domain, method, request_data); 147 } 148 /** 149 Cancels the current request. 150 */ 151 void cancelRequest() 152 { 153 checkClassBinding!(typeof(this))(); 154 ptrcall!(void)(_classBinding.cancelRequest, _godot_object); 155 } 156 /** 157 Returns the current status of the underlying $(D HTTPClient). See `STATUS_*` enum on $(D HTTPClient). 158 */ 159 HTTPClient.Status getHttpClientStatus() const 160 { 161 checkClassBinding!(typeof(this))(); 162 return ptrcall!(HTTPClient.Status)(_classBinding.getHttpClientStatus, _godot_object); 163 } 164 /** 165 166 */ 167 void setUseThreads(in bool enable) 168 { 169 checkClassBinding!(typeof(this))(); 170 ptrcall!(void)(_classBinding.setUseThreads, _godot_object, enable); 171 } 172 /** 173 174 */ 175 bool isUsingThreads() const 176 { 177 checkClassBinding!(typeof(this))(); 178 return ptrcall!(bool)(_classBinding.isUsingThreads, _godot_object); 179 } 180 /** 181 182 */ 183 void setBodySizeLimit(in long bytes) 184 { 185 checkClassBinding!(typeof(this))(); 186 ptrcall!(void)(_classBinding.setBodySizeLimit, _godot_object, bytes); 187 } 188 /** 189 190 */ 191 long getBodySizeLimit() const 192 { 193 checkClassBinding!(typeof(this))(); 194 return ptrcall!(long)(_classBinding.getBodySizeLimit, _godot_object); 195 } 196 /** 197 198 */ 199 void setMaxRedirects(in long amount) 200 { 201 checkClassBinding!(typeof(this))(); 202 ptrcall!(void)(_classBinding.setMaxRedirects, _godot_object, amount); 203 } 204 /** 205 206 */ 207 long getMaxRedirects() const 208 { 209 checkClassBinding!(typeof(this))(); 210 return ptrcall!(long)(_classBinding.getMaxRedirects, _godot_object); 211 } 212 /** 213 214 */ 215 void setDownloadFile(StringArg0)(in StringArg0 path) 216 { 217 checkClassBinding!(typeof(this))(); 218 ptrcall!(void)(_classBinding.setDownloadFile, _godot_object, path); 219 } 220 /** 221 222 */ 223 String getDownloadFile() const 224 { 225 checkClassBinding!(typeof(this))(); 226 return ptrcall!(String)(_classBinding.getDownloadFile, _godot_object); 227 } 228 /** 229 Returns the amount of bytes this HTTPRequest downloaded. 230 */ 231 long getDownloadedBytes() const 232 { 233 checkClassBinding!(typeof(this))(); 234 return ptrcall!(long)(_classBinding.getDownloadedBytes, _godot_object); 235 } 236 /** 237 Returns the response body length. 238 */ 239 long getBodySize() const 240 { 241 checkClassBinding!(typeof(this))(); 242 return ptrcall!(long)(_classBinding.getBodySize, _godot_object); 243 } 244 /** 245 246 */ 247 void _redirectRequest(StringArg0)(in StringArg0 arg0) 248 { 249 Array _GODOT_args = Array.empty_array; 250 _GODOT_args.append(arg0); 251 String _GODOT_method_name = String("_redirect_request"); 252 this.callv(_GODOT_method_name, _GODOT_args); 253 } 254 /** 255 256 */ 257 void _requestDone(in long arg0, in long arg1, in PoolStringArray arg2, in PoolByteArray arg3) 258 { 259 Array _GODOT_args = Array.empty_array; 260 _GODOT_args.append(arg0); 261 _GODOT_args.append(arg1); 262 _GODOT_args.append(arg2); 263 _GODOT_args.append(arg3); 264 String _GODOT_method_name = String("_request_done"); 265 this.callv(_GODOT_method_name, _GODOT_args); 266 } 267 /** 268 The file to download into. Will output any received file into it. 269 */ 270 @property String downloadFile() 271 { 272 return getDownloadFile(); 273 } 274 /// ditto 275 @property void downloadFile(String v) 276 { 277 setDownloadFile(v); 278 } 279 /** 280 If `true` multithreading is used to improve performance. 281 */ 282 @property bool useThreads() 283 { 284 return isUsingThreads(); 285 } 286 /// ditto 287 @property void useThreads(bool v) 288 { 289 setUseThreads(v); 290 } 291 /** 292 Maximum allowed size for response bodies. 293 */ 294 @property long bodySizeLimit() 295 { 296 return getBodySizeLimit(); 297 } 298 /// ditto 299 @property void bodySizeLimit(long v) 300 { 301 setBodySizeLimit(v); 302 } 303 /** 304 Maximum number of allowed redirects. 305 */ 306 @property long maxRedirects() 307 { 308 return getMaxRedirects(); 309 } 310 /// ditto 311 @property void maxRedirects(long v) 312 { 313 setMaxRedirects(v); 314 } 315 }