Class: VCAP::Services::Api::ServiceGatewayClient::HttpClient
- Inherits:
-
Object
- Object
- VCAP::Services::Api::ServiceGatewayClient::HttpClient
- Defined in:
- lib/services/api/clients/service_gateway_client.rb
Constant Summary collapse
- METHODS_MAP =
{ get: Net::HTTP::Get, post: Net::HTTP::Post, put: Net::HTTP::Put, delete: Net::HTTP::Delete, }
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#initialize(uri, token, timeout, request_id) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #perform_request(http_method, path, msg = EMPTY_REQUEST) ⇒ Object
Constructor Details
#initialize(uri, token, timeout, request_id) ⇒ HttpClient
Returns a new instance of HttpClient.
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/services/api/clients/service_gateway_client.rb', line 155 def initialize(uri, token, timeout, request_id) @uri = URI.parse(uri) @timeout = timeout @token = token @headers = { 'Content-Type' => 'application/json', GATEWAY_TOKEN_HEADER => token, "X-VCAP-Request-ID" => request_id.to_s } end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
153 154 155 |
# File 'lib/services/api/clients/service_gateway_client.rb', line 153 def headers @headers end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
153 154 155 |
# File 'lib/services/api/clients/service_gateway_client.rb', line 153 def timeout @timeout end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
153 154 155 |
# File 'lib/services/api/clients/service_gateway_client.rb', line 153 def token @token end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
153 154 155 |
# File 'lib/services/api/clients/service_gateway_client.rb', line 153 def uri @uri end |
Instance Method Details
#perform_request(http_method, path, msg = EMPTY_REQUEST) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/services/api/clients/service_gateway_client.rb', line 166 def perform_request(http_method, path, msg = EMPTY_REQUEST) klass = METHODS_MAP[http_method] request = klass.new(path, headers) request.body = msg.encode opts = {} if uri.scheme == "https" opts[:use_ssl] = true end response = Net::HTTP.start(uri.host, uri.port, opts) do |http| http.request(request) end code = response.code.to_i body = response.body return body if code == 200 begin err = ServiceErrorResponse.decode(body) rescue JsonMessage::Error raise UnexpectedResponse, "Can't decode gateway response. status code: #{code}, response body: #{body}" end case code when 404 then raise NotFoundResponse.new(err) when 503 then raise GatewayInternalResponse.new(err) else raise ErrorResponse.new(code, err) end end |