Class: Streamly::Request
- Inherits:
-
Object
- Object
- Streamly::Request
- Defined in:
- ext/streamly.c,
ext/streamly.c
Overview
A streaming REST client for Ruby that uses libcurl to do the heavy lifting. The API is almost exactly like rest-client, so users of that library should find it very familiar.
Class Method Summary collapse
-
.new(*args) ⇒ Object
call-seq: new(args).
Instance Method Summary collapse
-
#execute(*args) ⇒ Object
call-seq: rb_streamly_execute.
-
#initialize(*args) ⇒ Object
constructor
call-seq: initialize(args).
Constructor Details
#initialize(*args) ⇒ Object
call-seq: initialize(args)
args
should be a Hash and is required
This Hash should at least contain +:url+ and +:method+ keys.
You may also provide the following optional keys:
+:headers+ - should be a Hash of name/value pairs
+:response_header_handler+ - can be a string or object that responds to #call
If an object was passed, it's #call method will be called and passed the current chunk of data
+:response_body_handler+ - can be a string or object that responds to #call
If an object was passed, it's #call method will be called and passed the current chunk of data
+:payload+ - If +:method+ is either +:post+ or +:put+ this will be used as the request body
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'ext/streamly.c', line 203
static VALUE rb_streamly_init(int argc, VALUE * argv, VALUE self) {
struct curl_instance * instance;
VALUE args, url, payload, headers, username, password, credentials;
GetInstance(self, instance);
instance->handle = curl_easy_init();
instance->request_headers = NULL;
instance->request_method = Qnil;
instance->request_payload_handler = Qnil;
instance->response_header_handler = Qnil;
instance->response_body_handler = Qnil;
instance->options = Qnil;
rb_scan_args(argc, argv, "10", &args);
// Ensure our args parameter is a hash
Check_Type(args, T_HASH);
instance->request_method = rb_hash_aref(args, sym_method);
url = rb_hash_aref(args, sym_url);
payload = rb_hash_aref(args, sym_payload);
headers = rb_hash_aref(args, sym_headers);
username = rb_hash_aref(args, sym_username);
password = rb_hash_aref(args, sym_password);
instance->response_header_handler = rb_hash_aref(args, sym_response_header_handler);
instance->response_body_handler = rb_hash_aref(args, sym_response_body_handler);
// First lets verify we have a :method key
if (NIL_P(instance->request_method)) {
rb_raise(eStreamlyError, "You must specify a :method");
} else {
// OK, a :method was specified, but if it's POST or PUT we require a :payload
if (instance->request_method == sym_post || instance->request_method == sym_put) {
if (NIL_P(payload)) {
rb_raise(eStreamlyError, "You must specify a :payload for POST and PUT requests");
}
}
}
// Now verify a :url was provided
if (NIL_P(url)) {
rb_raise(eStreamlyError, "You must specify a :url to request");
}
if (NIL_P(instance->response_header_handler)) {
instance->response_header_handler = rb_str_new2("");
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
if (default_internal_enc) {
instance->response_header_handler = rb_str_export_to_enc(instance->response_header_handler, default_internal_enc);
} else {
instance->response_header_handler = rb_str_export_to_enc(instance->response_header_handler, utf8Encoding);
}
#endif
}
if (instance->request_method != sym_head && NIL_P(instance->response_body_handler)) {
instance->response_body_handler = rb_str_new2("");
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
if (default_internal_enc) {
instance->response_body_handler = rb_str_export_to_enc(instance->response_body_handler, default_internal_enc);
} else {
instance->response_body_handler = rb_str_export_to_enc(instance->response_body_handler, utf8Encoding);
}
#endif
}
if (!NIL_P(headers)) {
Check_Type(headers, T_HASH);
rb_iterate(rb_each, headers, each_http_header, self);
curl_easy_setopt(instance->handle, CURLOPT_HTTPHEADER, instance->request_headers);
}
// So far so good, lets start setting up our request
// Set the type of request
if (instance->request_method == sym_head) {
curl_easy_setopt(instance->handle, CURLOPT_NOBODY, 1);
} else if (instance->request_method == sym_get) {
curl_easy_setopt(instance->handle, CURLOPT_HTTPGET, 1);
} else if (instance->request_method == sym_post) {
curl_easy_setopt(instance->handle, CURLOPT_POST, 1);
curl_easy_setopt(instance->handle, CURLOPT_POSTFIELDS, RSTRING_PTR(payload));
curl_easy_setopt(instance->handle, CURLOPT_POSTFIELDSIZE, RSTRING_LEN(payload));
// (multipart)
// curl_easy_setopt(instance->handle, CURLOPT_HTTPPOST, 1);
// TODO: get streaming upload working
// curl_easy_setopt(instance->handle, CURLOPT_READFUNCTION, &upload_data_handler);
// curl_easy_setopt(instance->handle, CURLOPT_READDATA, &instance->upload_stream);
// curl_easy_setopt(instance->handle, CURLOPT_INFILESIZE, len);
} else if (instance->request_method == sym_put) {
curl_easy_setopt(instance->handle, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(instance->handle, CURLOPT_POSTFIELDS, RSTRING_PTR(payload));
curl_easy_setopt(instance->handle, CURLOPT_POSTFIELDSIZE, RSTRING_LEN(payload));
// TODO: get streaming upload working
// curl_easy_setopt(instance->handle, CURLOPT_UPLOAD, 1);
// curl_easy_setopt(instance->handle, CURLOPT_READFUNCTION, &upload_data_handler);
// curl_easy_setopt(instance->handle, CURLOPT_READDATA, &instance->upload_stream);
// curl_easy_setopt(instance->handle, CURLOPT_INFILESIZE, len);
} else if (instance->request_method == sym_delete) {
curl_easy_setopt(instance->handle, CURLOPT_CUSTOMREQUEST, "DELETE");
}
// Other common options
curl_easy_setopt(instance->handle, CURLOPT_URL, RSTRING_PTR(url));
curl_easy_setopt(instance->handle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(instance->handle, CURLOPT_MAXREDIRS, 3);
// Response header handling
curl_easy_setopt(instance->handle, CURLOPT_HEADERFUNCTION, &header_handler);
curl_easy_setopt(instance->handle, CURLOPT_HEADERDATA, instance->response_header_handler);
// Response body handling
if (instance->request_method != sym_head) {
curl_easy_setopt(instance->handle, CURLOPT_ENCODING, "identity, deflate, gzip");
curl_easy_setopt(instance->handle, CURLOPT_WRITEFUNCTION, &data_handler);
curl_easy_setopt(instance->handle, CURLOPT_WRITEDATA, instance->response_body_handler);
}
if (!NIL_P(username) || !NIL_P(password)) {
credentials = rb_str_new2("");
if (!NIL_P(username)) {
rb_str_buf_cat(credentials, RSTRING_PTR(username), RSTRING_LEN(username));
}
rb_str_buf_cat(credentials, ":", 1);
if (!NIL_P(password)) {
rb_str_buf_cat(credentials, RSTRING_PTR(password), RSTRING_LEN(password));
}
curl_easy_setopt(instance->handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC | CURLAUTH_DIGEST);
curl_easy_setopt(instance->handle, CURLOPT_USERPWD, RSTRING_PTR(credentials));
rb_gc_mark(credentials);
}
curl_easy_setopt(instance->handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(instance->handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(instance->handle, CURLOPT_ERRORBUFFER, instance->error_buffer);
return self;
}
|
Class Method Details
.new(*args) ⇒ Object
call-seq: new(args)
args
should be a Hash and is required
This Hash should at least contain +:url+ and +:method+ keys.
You may also provide the following optional keys:
+:headers+ - should be a Hash of name/value pairs
+:response_header_handler+ - can be a string or object that responds to #call
If an object was passed, it's #call method will be called and passed the current chunk of data
+:response_body_handler+ - can be a string or object that responds to #call
If an object was passed, it's #call method will be called and passed the current chunk of data
+:payload+ - If +:method+ is either +:post+ or +:put+ this will be used as the request body
180 181 182 183 184 185 |
# File 'ext/streamly.c', line 180
static VALUE rb_streamly_new(int argc, VALUE * argv, VALUE klass) {
struct curl_instance * instance;
VALUE obj = Data_Make_Struct(klass, struct curl_instance, streamly_instance_mark, streamly_instance_free, instance);
rb_obj_call_init(obj, argc, argv);
return obj;
}
|
Instance Method Details
#execute(*args) ⇒ Object
call-seq: rb_streamly_execute
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'ext/streamly.c', line 364
static VALUE rb_streamly_execute(RB_STREAMLY_UNUSED int argc, RB_STREAMLY_UNUSED VALUE * argv, VALUE self) {
VALUE status;
struct curl_instance * instance;
GetInstance(self, instance);
// Done setting up, lets do this!
status = rb_thread_blocking_region(nogvl_perform, instance->handle, RUBY_UBF_IO, 0);
if (!NIL_P(status)) {
rb_raise(status, "%s", instance->error_buffer);
}
// Cleanup
if (instance->request_headers != NULL) {
curl_slist_free_all(instance->request_headers);
instance->request_headers = NULL;
}
curl_easy_reset(instance->handle);
instance->request_payload_handler = Qnil;
if (instance->request_method == sym_head && TYPE(instance->response_header_handler) == T_STRING) {
return instance->response_header_handler;
} else if (TYPE(instance->response_body_handler) == T_STRING) {
return instance->response_body_handler;
} else {
return Qnil;
}
}
|