Class: PxModule::PxHttpClient

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/perimeterx/utils/px_http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(px_config) ⇒ PxHttpClient

Returns a new instance of PxHttpClient.



13
14
15
16
17
# File 'lib/perimeterx/utils/px_http_client.rb', line 13

def initialize(px_config)
  @px_config = px_config
  @logger = px_config[:logger]
  @logger.debug("PxHttpClient[initialize]: HTTP client is being initilized with base_uri: #{px_config[:backend_url]}")
end

Instance Attribute Details

#px_clientObject

Returns the value of attribute px_client.



11
12
13
# File 'lib/perimeterx/utils/px_http_client.rb', line 11

def px_client
  @px_client
end

#px_configObject

Returns the value of attribute px_config.



10
11
12
# File 'lib/perimeterx/utils/px_http_client.rb', line 10

def px_config
  @px_config
end

Instance Method Details

#get(url, headers) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/perimeterx/utils/px_http_client.rb', line 80

def get(url, headers)
  s = Time.now
  begin
    @logger.debug("PxHttpClient[get]: sending get request to #{url} with headers {#{headers.to_json()}}")
    
    #set url
    uri = URI(url)
    req = Net::HTTP::Get.new(uri)
    
    # set headers
    headers.each do |key, value|
      req[key] = value
    end
    
    # send request   
    response = Net::HTTP.start(uri.hostname, uri.port) {|http|
      http.request(req)
    }
    
  ensure
    e = Time.now
    @logger.debug("PxHttpClient[get]: runtime: #{(e-s) * 1000.0}")
  end
  return response
end

#post(path, body, headers, api_timeout = 1, connection_timeout = 1) ⇒ Object

Runs a POST command to Perimeter X servers Params:

path

string containing uri

body

hash object, containing the request body, must be converted to json format

headers

hash object, hold headers

api_timeout

int, sets the timeout for a request

connection_timeout

int, sets the timeout for opening a connection



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/perimeterx/utils/px_http_client.rb', line 27

def post(path, body, headers, api_timeout = 1, connection_timeout = 1)
  s = Time.now
  begin
    @logger.debug("PxHttpClient[post]: posting to #{path} headers {#{headers.to_json()}} body: {#{body.to_json()}} ")
    response = Typhoeus.post(
        "#{px_config[:backend_url]}#{path}",
        headers: headers,
        body: body.to_json,
        timeout: api_timeout,
        connecttimeout: connection_timeout
    )
    if response.timed_out?
      @logger.warn('PerimeterxS2SValidator[verify]: request timed out')
      return false
    end
  ensure
    e = Time.now
    @logger.debug("PxHttpClient[post]: runtime: #{(e-s) * 1000.0}")
  end
  return response
end

#post_xhr(url, body, headers) ⇒ Object



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
77
# File 'lib/perimeterx/utils/px_http_client.rb', line 50

def post_xhr(url, body, headers)
  s = Time.now
  begin
    @logger.debug("PxHttpClient[post]: sending xhr post request to #{url} with headers {#{headers.to_json()}}")
    
    #set url
    uri = URI(url)
    req = Net::HTTP::Post.new(uri)
    
    # set body
    req.body=body
    
    # set headers
    headers.each do |key, value|
      req[key] = value
    end
    
    # send request   
    response = Net::HTTP.start(uri.hostname, uri.port) {|http|
      http.request(req)
    }

  ensure
    e = Time.now
    @logger.debug("PxHttpClient[get]: runtime: #{(e-s) * 1000.0}")
  end
  return response
end