Module: WolfCore::HttpOperations

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

Instance Method Summary collapse

Methods included from AsyncUtils

#run_async

Methods included from ExceptionOperations

#raise_service_error

Instance Method Details

#async_http_get(**args) ⇒ Object



6
7
8
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 6

def async_http_get(**args)
  run_async { http_get(**args) }
end

#async_http_post(**args) ⇒ Object



14
15
16
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 14

def async_http_post(**args)
  run_async { http_post(**args) }
end

#async_http_put(**args) ⇒ Object



22
23
24
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 22

def async_http_put(**args)
  run_async { http_put(**args) }
end

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



10
11
12
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 10

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



18
19
20
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 18

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



26
27
28
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 26

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



40
41
42
43
44
45
46
47
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 40

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



30
31
32
33
34
35
36
37
38
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 30

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