Class: APIHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/Helpers/api_helper.rb

Instance Method Summary collapse

Instance Method Details

#LogsEnabled=(logsEnabled) ⇒ Object



78
79
80
# File 'lib/Helpers/api_helper.rb', line 78

def LogsEnabled=(logsEnabled)
  @logsEnabled = logsEnabled
end

#SendAPIRequest(requestData, requestURL) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/Helpers/api_helper.rb', line 9

def SendAPIRequest(requestData, requestURL)
  #response = RestClient.get requestURL, {:params => {:id => 50, 'foo' => 'bar'}}
  #RestClient.post()

  begin

    #check if logs enabled
    if(requestData.getEnableLogs == true)
      @logsEnabled = true
    else
      @logsEnabled = false
    end

    #puts requestData.instance_variable_get("@requestMessage").to_json
    reqJson = requestData.to_json

    if(@logsEnabled == true)
      LogAPIDetails("URL", requestURL.to_s)
      LogAPIDetails("REQ", reqJson)
    end

    uri = URI(requestURL)


    http = Net::HTTP.new(uri.host, uri.port)

    if(requestURL.include?("https:"))

      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER

      #gems/certified-1.0.0/certs/ca-bundle.crt"
      http.ca_file = File.expand_path("../../", __FILE__) + "/Certificates/ca-bundle.crt"

      #http.ssl_version =  :TLSv1_2 #TLSv1_1
      http.ssl_version =  :SSLv23
      #:SSLv23 has fallback feature
    end

    req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
    req.body = reqJson #{param1: 'some value', param2: 'some other value'}.to_json
    #puts req.body

    #res = Net::HTTP.start(uri.hostname, uri.port) do |http|
    #  http.request(req)
    #end

    res = http.request(req)

    #puts res.body
    responseModel = ResponseModel.new
    responseModel.from_json!(res.body) #will require same deserialization ResponseMessage prop, in corresponding manager/method


    if(@logsEnabled == true)
      LogAPIDetails("RES", responseModel.to_json)
    end

    return responseModel

  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
      Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    puts e.http_body

  end


end