11# Copyright (c) 2009-2011 VMware, Inc.
2- require 'eventmachine'
3- require 'em-http-request'
42require 'httpclient'
53
64require 'vcap/services/api/const'
@@ -13,38 +11,6 @@ module Api
1311end
1412
1513module VCAP ::Services ::Api
16- class AsyncHttpRequest
17- class << self
18- def new ( url , token , verb , timeout , msg = VCAP ::Services ::Api ::EMPTY_REQUEST )
19- req = {
20- head : {
21- VCAP ::Services ::Api ::GATEWAY_TOKEN_HEADER => token ,
22- 'Content-Type' => 'application/json'
23- } ,
24- body : msg . encode
25- }
26- if timeout
27- EM ::HttpRequest . new ( url , inactivity_timeout : timeout ) . send ( verb . to_sym , req )
28- else
29- EM ::HttpRequest . new ( url ) . send ( verb . to_sym , req )
30- end
31- end
32-
33- def request ( url , token , verb , timeout , msg = VCAP ::Services ::Api ::EMPTY_REQUEST )
34- req = new ( url , token , verb , timeout , msg )
35- f = Fiber . current
36- req . callback { f . resume ( req ) }
37- req . errback { f . resume ( req ) }
38- http = Fiber . yield
39- raise UnexpectedResponse . new ( "Error sending request #{ msg . extract . to_json } to gateway #{ @url } : #{ http . error } " ) unless http . error . empty?
40-
41- code = http . response_header . status . to_i
42- body = http . response
43- [ code , body ]
44- end
45- end
46- end
47-
4814 module SynchronousHttpRequest
4915 def self . request ( url , token , verb , _timeout , msg = VCAP ::Services ::Api ::EMPTY_REQUEST )
5016 header = {
@@ -57,30 +23,4 @@ def self.request(url, token, verb, _timeout, msg=VCAP::Services::Api::EMPTY_REQU
5723 [ msg . code , msg . body ]
5824 end
5925 end
60-
61- class AsyncHttpMultiPartUpload
62- class << self
63- def new ( url , timeout , multipart , head = { } )
64- req = {
65- head : head ,
66- body : '' ,
67- multipart : multipart
68- }
69-
70- if timeout
71- EM ::HttpRequest . new ( url , inactivity_timeout : timeout ) . post req
72- else
73- EM ::HttpRequest . new ( url ) . post req
74- end
75- end
76-
77- def fibered ( url , timeout , multipart , head = { } )
78- req = new ( url , timeout , multipart , head )
79- f = Fiber . current
80- req . callback { f . resume ( req ) }
81- req . errback { f . resume ( req ) }
82- Fiber . yield
83- end
84- end
85- end
8626end
0 commit comments