Module: WolfCore::HttpOperations

Includes:
ExceptionOperations
Included in:
ApplicationService, FkmOperations
Defined in:
lib/wolf_core/application/http_operations.rb

Instance Method Summary collapse

Methods included from ExceptionOperations

#raise_service_error

Instance Method Details

#http_get(url:, headers: {}, query: nil) ⇒ Object



5
6
7
# File 'lib/wolf_core/application/http_operations.rb', line 5

def http_get(url:, headers: {}, query: nil)
  WolfCore::HttpDataSource.http_get(url: url, headers: headers, query: query)
end

#http_post(url:, headers: {}, body: nil, query: nil) ⇒ Object



9
10
11
# File 'lib/wolf_core/application/http_operations.rb', line 9

def http_post(url:, headers: {}, body: nil, query: nil)
  WolfCore::HttpDataSource.http_post(url: url, headers: headers, query: query, body: body)
end

#http_put(url:, headers: {}, body: nil, query: nil) ⇒ Object



13
14
15
# File 'lib/wolf_core/application/http_operations.rb', line 13

def http_put(url:, headers: {}, body: nil, query: nil)
  WolfCore::HttpDataSource.http_put(url: url, headers: headers, query: query, body: body)
end

#parse_http_response(response) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/wolf_core/application/http_operations.rb', line 27

def parse_http_response(response)
  body = JSON.parse(response.body) rescue response.body
  OpenStruct.new({
    code: response.code,
    body: body,
    message: response.message,
  })
end

#validate_http_response(response:, message:, error_data: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/wolf_core/application/http_operations.rb', line 17

def validate_http_response(response:, message:, error_data: nil)
  unless response.code == 200
    error_data = {
      message: message,
      response: parse_http_response(response)
    }.merge(error_data || {})
    raise_service_error(error_data)
  end
end