Class: Mashape::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client.rb

Class Method Summary collapse

Class Method Details

.do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil, &callback) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/http_client.rb', line 25

def HttpClient.do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil, &callback)
  if callback
    return Thread.new do
      callback.call(internal_do_request(method, url, parameters, content_type, response_type, authentication_handlers))
    end
  else
    return internal_do_request(method, url, parameters, content_type, response_type, authentication_handlers)
  end
end

.internal_do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/http_client.rb', line 35

def HttpClient.internal_do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil)
   httpResponse = nil;
   
   headers = {}
   if parameters == nil
        parameters = {}
   end
   
   # figure out what kind of auth we have and where to put it
   authentication_handlers.each do |handler|
     if handler.kind_of? Mashape::HeaderAuthentication
       headers = headers.merge(handler.handleHeader)
     elsif handler.kind_of? Mashape::QueryAuthentication
       parameters = parameters.merge(handler.handleParams)
     elsif handler.kind_of? Mashape::OAuth10aAuthentication
       if url.end_with?("/oauth_url") == false && (handler.handleParams[:access_token] == nil || handler.handleParams[:access_secret] == nil)
         raise Mashape::JsonException.new("Before consuming OAuth endpoint, invoke authorize('access_token','access_secret') with not null values")
       end
       # These headers will be processed by the proxy to sign the request
       headers["x-mashape-oauth-consumerkey"] = handler.handleParams[:consumer_key]
       headers["x-mashape-oauth-consumersecret"] = handler.handleParams[:consumer_secret]
       headers["x-mashape-oauth-accesstoken"] = handler.handleParams[:access_token]
       headers["x-mashape-oauth-accesssecret"] = handler.handleParams[:access_secret]
     elsif handler.kind_of? Mashape::OAuth2Authentication
       if url.end_with?("/oauth_url") == false && handler.handleParams[:access_token] == nil
          raise Mashape::JsonException.new("Before consuming OAuth endpoint, invoke authorize('access_token') with a not null value")
       end
       parameters = parameters.merge({"access_token" => handler.handleParams[:access_token]})
     end
     
   end
   
   Mashape::HttpUtils.setRequestHeaders(content_type, response_type, headers)
   
   if(content_type == :json)
     unless parameters[:json_param_body] == nil
       parameters = JSON(parameters[:json_param_body])
     else
       parameters = {}
     end
   end
   
   begin
    case method
      when :get
        uri = Addressable::URI.new
        uri.query_values = parameters
        httpResponse = RestClient.get url + "?" + uri.query, headers
      when :post
        httpResponse = RestClient.post url, parameters, headers
      when :put
        httpResponse = RestClient.put url, parameters, headers
      when :delete
        httpResponse = RestClient.delete url, parameters, headers
      when :patch
        httpResponse = RestClient.patch url, parameters, headers
    end
    rescue => e
      httpResponse = e.response
    end
    
    response = HttpResponse.new
    response.code = httpResponse.code
    response.headers = httpResponse.headers
    response.raw_body = httpResponse
    
    Mashape::HttpUtils.setResponse(response_type, response)
    
    return response
end