Module: IBM::Cloud::SDKHTTP::BaseHTTPMixin

Included in:
IBM::Cloud::SDK::VPC::VpcHTTP, BaseCollection, BaseInstance, IAMToken
Defined in:
lib/ibm/cloud/sdk_http/base_http_mixin.rb

Overview

Generic methods for accessing VPC.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



35
36
37
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 35

def endpoint
  @endpoint
end

Instance Method Details

#adhoc(method: 'get', path: nil, params: nil, payload: nil, payload_type: 'json') ⇒ SDKResponse

Run a custom query and verify response is 2xx or 404.

Parameters:

  • method (String) (defaults to: 'get')

    The HTTP method to use.

  • path (String) (defaults to: nil)

    The relative path from the current object location.

  • params (Hash) (defaults to: nil)

    A hash of query parameters.

  • payload (Hash) (defaults to: nil)

    A hash to send as the body.

  • payload_type (Hash) (defaults to: 'json')

    If json then convert to json string, else send as form data.

Returns:

Raises:



21
22
23
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 21

def adhoc(method: 'get', path: nil, params: nil, payload: nil, payload_type: 'json')
  unchecked_response(method: method, path: path, params: params, payload: payload, payload_type: payload_type).raise_for_status!
end

#delete(path: nil, params: nil) ⇒ SDKResponse

Send a DELETE request and verify response is 2xx or 404.

Parameters:

  • path (String) (defaults to: nil)

    The relative path from the current object location.

  • params (Hash) (defaults to: nil)

    A hash of query parameters.

Returns:

Raises:



84
85
86
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 84

def delete(path: nil, params: nil)
  adhoc(method: 'delete', path: path, params: params)
end

#get(path: nil, params: nil) ⇒ SDKResponse

Perform a GET request and verify response is 2xx or 404.

Parameters:

  • path (String) (defaults to: nil)

    The relative path from the current object location.

  • params (Hash) (defaults to: nil)

    A hash of query parameters.

Returns:

Raises:



42
43
44
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 42

def get(path: nil, params: nil)
  adhoc(method: 'get', path: path, params: params)
end

#metadata(query = nil, payload = nil, payload_type = 'json') ⇒ Hash

Preprocess request parameters with any additional data.

Parameters:

  • query (Hash) (defaults to: nil)

    A hash of query parameters.

  • payload (Hash) (defaults to: nil)

    A hash to send as the body.

  • payload_type (Hash) (defaults to: 'json')

    If json then convert to json string, else send as form data.

Returns:

  • (Hash)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 93

def (query = nil, payload = nil, payload_type = 'json')
  @params ||= {}
  @params.merge!(query) if query

  send_parameters = {
    query: @params,
    headers: { "Authorization": @token.authorization_header }
  }

  # Add payload if it is not nil.
  if payload && payload.empty? == false
    payload = payload.to_json if payload_type == 'json'
    send_parameters[:body] = payload
  end
  send_parameters
end

#patch(payload: nil, path: nil, params: nil, payload_type: 'json') ⇒ SDKResponse

Send a PATCH request and verify response is 2xx or 404.

Parameters:

  • path (String) (defaults to: nil)

    The relative path from the current object location.

  • params (Hash) (defaults to: nil)

    A hash of query parameters.

  • payload (Hash) (defaults to: nil)

    A hash to send as the body.

  • payload_type (Hash) (defaults to: 'json')

    If json then convert to json string, else send as form data.

Returns:

Raises:



75
76
77
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 75

def patch(payload: nil, path: nil, params: nil, payload_type: 'json')
  adhoc(method: 'patch', path: path, params: params, payload: payload, payload_type: payload_type)
end

#post(payload: nil, path: nil, params: nil, payload_type: 'json') ⇒ SDKResponse

Send a POST request and verify response is 2xx or 404.

Parameters:

  • path (String) (defaults to: nil)

    The relative path from the current object location.

  • params (Hash) (defaults to: nil)

    A hash of query parameters.

  • payload (Hash) (defaults to: nil)

    A hash to send as the body.

  • payload_type (Hash) (defaults to: 'json')

    If json then convert to json string, else send as form data.

Returns:

Raises:



53
54
55
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 53

def post(payload: nil, path: nil, params: nil, payload_type: 'json')
  adhoc(method: 'post', path: path, params: params, payload: payload, payload_type: payload_type)
end

#put(payload: nil, path: nil, params: nil, payload_type: 'json') ⇒ SDKResponse

Send a PUT request and verify response is 2xx or 404.

Parameters:

  • path (String) (defaults to: nil)

    The relative path from the current object location.

  • params (Hash) (defaults to: nil)

    A hash of query parameters.

  • payload (Hash) (defaults to: nil)

    A hash to send as the body.

  • payload_type (Hash) (defaults to: 'json')

    If json then convert to json string, else send as form data.

Returns:

Raises:



64
65
66
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 64

def put(payload: nil, path: nil, params: nil, payload_type: 'json')
  adhoc(method: 'put', path: path, params: params, payload: payload, payload_type: payload_type)
end

#unchecked_response(method: 'get', path: nil, params: nil, payload: nil, payload_type: 'json') ⇒ Object

Run a custom query do not verify the response.

Parameters:

  • method (String) (defaults to: 'get')

    The HTTP method to use.

  • path (String) (defaults to: nil)

    The relative path from the current object location.

  • params (Hash) (defaults to: nil)

    A hash of query parameters.

  • payload (Hash) (defaults to: nil)

    A hash to send as the body.

  • payload_type (Hash) (defaults to: 'json')

    If json then convert to json string, else send as form data.



31
32
33
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 31

def unchecked_response(method: 'get', path: nil, params: nil, payload: nil, payload_type: 'json')
  @connection.request(method.to_sym, url(path), (params, payload, payload_type))
end

#url(path = nil) ⇒ Object

Merge path with current class’s endpoint.



111
112
113
114
115
116
# File 'lib/ibm/cloud/sdk_http/base_http_mixin.rb', line 111

def url(path = nil)
  return endpoint unless path
  return path if URI.parse(path).relative? == false

  "#{endpoint}/#{path}"
end