Module: WolfCore::HttpOperations

Instance Method Summary collapse

Methods included from LoggingUtils

#log_object

Methods included from AsyncUtils

#run_async

Methods included from ExceptionOperations

#raise_service_error

Instance Method Details

#async_http_get(**args) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 12

def async_http_get(**args)
  log_object "starting async_http_get"
  log_object args, title: "async_http_get args are"
  run_async do
    response = http_get(**args)
    log_object parse_http_response(response), title: "async_http_get response is"
  end
end

#async_http_post(**args) ⇒ Object



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

def async_http_post(**args)
  log_object "starting async_http_post"
  log_object args, title: "async_http_post args are"
  run_async do
    response = http_post(**args)
    log_object parse_http_response(response), title: "async_http_post response is"
  end
end

#async_http_put(**args) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 73

def async_http_put(**args)
  log_object "starting async_http_put"
  log_object args, title: "async_http_put args are"
  run_async do
    response = http_put(**args)
    log_object parse_http_response(response), title: "async_http_put response is"
  end
end

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



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

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



64
65
66
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 64

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



95
96
97
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 95

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



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 109

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

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



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

def parsed_http_get(url:, headers: {}, query: nil)
  response = http_get(url: url, headers: headers, query: query)
  parse_http_response(response)
end

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



37
38
39
40
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 37

def parsed_http_post(url:, body: nil, headers: {}, query: nil)
  response = http_post(url: url, headers: headers, query: query, body: body)
  parse_http_response(response)
end

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



68
69
70
71
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 68

def parsed_http_put(url:, body: nil, headers: {}, query: nil)
  response = http_put(url: url, headers: headers, query: query, body: body)
  parse_http_response(response)
end

#safe_http_get(url:, headers: {}, query: nil, error_message: nil, title: nil) ⇒ Object



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

def safe_http_get(url:, headers: {}, query: nil, error_message: nil, title: nil)
  response = http_get(url: url, headers: headers, query: query)
  response = parse_http_response(response)

  log_object response, title: title if title.present?

  error_message ||= "Error on safe_http_get"
  validate_http_response(response: response, message: error_message)

  response
end

#safe_http_post(url:, headers: {}, body: nil, query: nil, error_message: nil, title: nil, error_data: nil) ⇒ Object



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

def safe_http_post(url:, headers: {}, body: nil, query: nil, error_message: nil, title: nil, error_data: nil)
  response = http_post(url: url, headers: headers, body: body, query: query)
  response = parse_http_response(response)

  title ||= "safe_http_post response is"
  log_object response, title: title

  error_message ||= "Error on safe_http_post"
  validate_http_response(response: response, message: error_message, error_data: error_data)

  response
end

#safe_http_put(url:, headers: {}, body: nil, query: nil, error_message: nil, title: nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 82

def safe_http_put(url:, headers: {}, body: nil, query: nil, error_message: nil, title: nil)
  response = http_put(url: url, headers: headers, body: body, query: query)
  response = parse_http_response(response)

  title ||= "safe_http_put response is"
  log_object response, title: title

  error_message ||= "Error on safe_http_put"
  validate_http_response(response: response, message: error_message)

  response
end

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



99
100
101
102
103
104
105
106
107
# File 'lib/wolf_core/infrastructure/http_operations.rb', line 99

def validate_http_response(response:, message:, error_data: nil)
  return if response.code == 200

  error_data = {
    message: message,
    response: parse_http_response(response)
  }.merge(error_data || {})
  raise_service_error(error_data)
end