Module: WolfCore::HttpOperations

Includes:
AsyncUtils, ExceptionOperations
Included in:
ApplicationService, Barton::Routing, 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
9
10
11
12
13
14
15
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 6

def async_http_get(**args)
  puts "starting async_http_get"
  puts "async_http_get args are"
  pp args
  run_async do
    response = http_get(**args)
    puts "async_http_get response is"
    pp parse_http_response(response).to_h
  end
end

#async_http_post(**args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 21

def async_http_post(**args)
  puts "starting async_http_post"
  puts "async_http_post args are"
  pp args
  run_async do
    response = http_post(**args)
    puts "async_http_post response is"
    pp parse_http_response(response).to_h
  end
end

#async_http_put(**args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 36

def async_http_put(**args)
  puts "starting async_http_put"
  puts "async_http_put args are"
  pp args
  run_async do
    response = http_put(**args)
    puts "async_http_put response is"
    pp parse_http_response(response).to_h
  end
end

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



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

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



32
33
34
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 32

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



47
48
49
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 47

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



61
62
63
64
65
66
67
68
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 61

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



51
52
53
54
55
56
57
58
59
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 51

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