@@ -15,7 +15,7 @@ class RemoteHTTPDataService
15
15
include DataServiceAutoLoader
16
16
17
17
ONLINE_TEST_URL = "/api/1/msf/online"
18
- EXEC_ASYNC = { :exec_async => true }
18
+ EXEC_ASYNC = { :exec_async => true }
19
19
GET_REQUEST = 'GET'
20
20
POST_REQUEST = 'POST'
21
21
@@ -32,7 +32,7 @@ def initialize(endpoint)
32
32
# POST data and don't wait for the endpoint to process the data before getting a response
33
33
#
34
34
def post_data_async ( path , data_hash )
35
- post_data ( path , data_hash . merge ( EXEC_ASYNC ) )
35
+ make_request ( POST_REQUEST , path , data_hash . merge ( EXEC_ASYNC ) )
36
36
end
37
37
38
38
#
@@ -44,30 +44,7 @@ def post_data_async(path, data_hash)
44
44
# @return A wrapped response (ResponseWrapper), see below.
45
45
#
46
46
def post_data ( path , data_hash )
47
- begin
48
- raise 'Data to post to remote service cannot be null or empty' if ( data_hash . nil? || data_hash . empty? )
49
-
50
- puts "#{ Time . now } - Posting #{ data_hash } to #{ path } "
51
- client = @client_pool . pop ( )
52
- request = Net ::HTTP ::Post . new ( path )
53
- request = build_request ( request , data_hash )
54
- response = client . request ( request )
55
-
56
- if response . code == "200"
57
- #puts "POST request: #{path} with body: #{json_body} sent successfully"
58
- return SuccessResponse . new ( response )
59
- else
60
- puts "POST request: #{ path } with body: #{ request . body } failed with code: #{ response . code } message: #{ response . body } "
61
- return FailedResponse . new ( response )
62
- end
63
- rescue Exception => e
64
- puts "Problem with POST request: #{ e . message } "
65
- e . backtrace . each do |line |
66
- puts "#{ line } \n "
67
- end
68
- ensure
69
- @client_pool << client
70
- end
47
+ make_request ( POST_REQUEST , path , data_hash )
71
48
end
72
49
73
50
#
@@ -79,24 +56,34 @@ def post_data(path, data_hash)
79
56
# @return A wrapped response (ResponseWrapper), see below.
80
57
#
81
58
def get_data ( path , data_hash = nil )
82
- begin
59
+ make_request ( GET_REQUEST , path , data_hash )
60
+ end
83
61
84
- puts "#{ Time . now } - Getting #{ path } with #{ data_hash ? data_hash : "nil" } "
62
+ def make_request ( request_type , path , data_hash = nil )
63
+ begin
64
+ puts "#{ Time . now } - HTTP #{ request_type } request to #{ path } with #{ data_hash ? data_hash : "nil" } "
85
65
client = @client_pool . pop ( )
86
- request = Net ::HTTP ::Get . new ( path )
87
- request = build_request ( request , data_hash )
88
- response = client . request ( request )
66
+ case request_type
67
+ when GET_REQUEST
68
+ request = Net ::HTTP ::Get . new ( path )
69
+ when POST_REQUEST
70
+ request = Net ::HTTP ::Post . new ( path )
71
+ else
72
+ raise Exception , 'A request_type must be specified'
73
+ end
74
+ built_request = build_request ( request , data_hash )
75
+ response = client . request ( built_request )
89
76
90
77
if response . code == "200"
91
78
# puts 'request sent successfully'
92
79
return SuccessResponse . new ( response )
93
80
else
94
- puts "GET request: #{ path } failed with code: #{ response . code } message: #{ response . body } "
81
+ puts "HTTP #{ request_type } request: #{ path } failed with code: #{ response . code } message: #{ response . body } "
95
82
return FailedResponse . new ( response )
96
83
end
97
84
rescue Exception => e
98
- puts "Problem with GET request: #{ e . message } "
99
- e . backtrace . each { |line | puts "#{ line } \n " }
85
+ puts "Problem with HTTP #{ request_type } request: #{ e . message } "
86
+ e . backtrace . each { |line | puts "#{ line } \n " }
100
87
ensure
101
88
@client_pool << client
102
89
end
0 commit comments