hookd-client 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eade8cb3959a4a3792d6bf6c7db1cc9ea04fe6d1890ff895fe9400e823d720bf
4
- data.tar.gz: b49e156b95cac1a65897f3dfc8f85f55e96e8de9181885b9399357e1c8dbcc96
3
+ metadata.gz: ba2b5b2434c982f13d1515e488d1bcc30056f66448fe34d9529004ae0ea1229a
4
+ data.tar.gz: 437ffa486b1a4bb37c6c3e89dc33ab9dbb927b831496435948b47039b0d91384
5
5
  SHA512:
6
- metadata.gz: 2fc01eb61437a52f4ed20cec5a9af0b4c408427da54fe87e8186163254f41105a6026970bb96bb32a886761fdb12bc329e99aa38a77f6d2f498573fbb0511b16
7
- data.tar.gz: 3f40e48b70f0ac245125382fc9ff5150f4fdfdae1439b8436bf22ce31e852bea9f36d033014f94d4dc61b66c91eea18d26dc8e9ccb13371462069d13977f0b37
6
+ metadata.gz: b3551c335feda2be9dfd807c1de65fe1f6f7e46698f193cb3112323c02c3b332e66aa9563caebdc3d24f730d5b8d49484099dfd7c0fd1e3f1370c9860350c79b
7
+ data.tar.gz: 4f162c429ea63559cac8749ffcdace2f80014317c30d2d06e6d8954a44a7c8cdd308222e9133bc292a16db9bddcd1283f25b5f762922b46a68eb130737f23432
data/lib/hookd/client.rb CHANGED
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'net/http'
3
+ require 'httpx'
4
4
  require 'json'
5
- require 'uri'
6
5
 
7
6
  module Hookd
8
7
  # HTTP client for interacting with Hookd server
@@ -12,7 +11,13 @@ module Hookd
12
11
  def initialize(server:, token:)
13
12
  @server = server
14
13
  @token = token
15
- @uri = URI.parse(server)
14
+ @http = HTTPX.with(
15
+ headers: { 'X-API-Key' => token },
16
+ timeout: {
17
+ connect_timeout: 10,
18
+ read_timeout: 30
19
+ }
20
+ )
16
21
  end
17
22
 
18
23
  # Register one or more hooks
@@ -25,9 +30,7 @@ module Hookd
25
30
  def register(count: nil)
26
31
  body = count.nil? ? nil : { count: count }
27
32
 
28
- if count && (!count.is_a?(Integer) || count < 1)
29
- raise ArgumentError, 'count must be a positive integer'
30
- end
33
+ raise ArgumentError, 'count must be a positive integer' if count && (!count.is_a?(Integer) || count < 1)
31
34
 
32
35
  response = post('/register', body)
33
36
 
@@ -71,43 +74,43 @@ module Hookd
71
74
  private
72
75
 
73
76
  def get(path)
74
- request = Net::HTTP::Get.new(path)
75
- request['X-API-Key'] = token
76
- execute_request(request)
77
+ url = "#{@server}#{path}"
78
+ response = @http.get(url)
79
+ handle_response(response)
77
80
  end
78
81
 
79
82
  def post(path, body = nil)
80
- request = Net::HTTP::Post.new(path)
81
- request['X-API-Key'] = token
82
- request['Content-Type'] = 'application/json'
83
- request.body = body.to_json if body
84
- execute_request(request)
83
+ url = "#{@server}#{path}"
84
+ options = { headers: { 'Content-Type' => 'application/json' } }
85
+ options[:json] = body if body
86
+
87
+ response = @http.post(url, **options)
88
+ handle_response(response)
85
89
  end
86
90
 
87
- def execute_request(request)
88
- http = Net::HTTP.new(@uri.host, @uri.port)
89
- http.use_ssl = @uri.scheme == 'https'
90
- http.open_timeout = 10
91
- http.read_timeout = 30
91
+ def handle_response(response)
92
+ # HTTPX returns HTTPX::ErrorResponse for connection/timeout errors
93
+ if response.is_a?(HTTPX::ErrorResponse)
94
+ error = response.error
95
+ raise ConnectionError, "Connection failed: #{error.message}"
96
+ end
92
97
 
93
- response = http.request(request)
98
+ body = response.body.to_s
94
99
 
95
- case response.code.to_i
100
+ case response.status
96
101
  when 200, 201
97
- raise Error, 'Empty response body from server' if response.body.nil? || response.body.empty?
102
+ raise Error, 'Empty response body from server' if body.nil? || body.empty?
98
103
 
99
- JSON.parse(response.body)
104
+ JSON.parse(body)
100
105
  when 401
101
- raise AuthenticationError, "Authentication failed: #{response.body}"
106
+ raise AuthenticationError, "Authentication failed: #{body}"
102
107
  when 404
103
- raise NotFoundError, "Resource not found: #{response.body}"
108
+ raise NotFoundError, "Resource not found: #{body}"
104
109
  when 500..599
105
- raise ServerError, "Server error (#{response.code}): #{response.body}"
110
+ raise ServerError, "Server error (#{response.status}): #{body}"
106
111
  else
107
- raise Error, "Unexpected response (#{response.code}): #{response.body}"
112
+ raise Error, "Unexpected response (#{response.status}): #{body}"
108
113
  end
109
- rescue SocketError, Errno::ECONNREFUSED, Net::OpenTimeout, Net::ReadTimeout => e
110
- raise ConnectionError, "Connection failed: #{e.message}"
111
114
  rescue JSON::ParserError => e
112
115
  raise Error, "Invalid JSON response: #{e.message}"
113
116
  end
data/lib/hookd/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hookd
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hookd-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua MARTINELLE
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies: []
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: httpx
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.0'
12
26
  description: Ruby client library for Hookd, a DNS/HTTP interaction server for security
13
27
  testing and debugging
14
28
  email: