Module: MicroCMS::HttpUtil

Included in:
Client
Defined in:
lib/microcms.rb

Overview

HttpUtil

Instance Method Summary collapse

Instance Method Details

#send_http_request(method, endpoint, path, query = nil, body = nil) ⇒ Object

Raises:



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

def send_http_request(method, endpoint, path, query = nil, body = nil)
  uri = build_uri(endpoint, path, query)
  http = build_http(uri)
  req = build_request(method, uri, body)
  res = http.request(req)

  raise APIError.new(status_code: res.code.to_i, body: res.body) if res.code.to_i >= 400

  JSON.parse(res.body, object_class: OpenStruct) if res.header['Content-Type'].include?('application/json') # rubocop:disable Style/OpenStructUse
end