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 +4 -4
- data/lib/hookd/client.rb +32 -29
- data/lib/hookd/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ba2b5b2434c982f13d1515e488d1bcc30056f66448fe34d9529004ae0ea1229a
|
|
4
|
+
data.tar.gz: 437ffa486b1a4bb37c6c3e89dc33ab9dbb927b831496435948b47039b0d91384
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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 '
|
|
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
|
-
@
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
98
|
+
body = response.body.to_s
|
|
94
99
|
|
|
95
|
-
case response.
|
|
100
|
+
case response.status
|
|
96
101
|
when 200, 201
|
|
97
|
-
raise Error, 'Empty response body from server' if
|
|
102
|
+
raise Error, 'Empty response body from server' if body.nil? || body.empty?
|
|
98
103
|
|
|
99
|
-
JSON.parse(
|
|
104
|
+
JSON.parse(body)
|
|
100
105
|
when 401
|
|
101
|
-
raise AuthenticationError, "Authentication failed: #{
|
|
106
|
+
raise AuthenticationError, "Authentication failed: #{body}"
|
|
102
107
|
when 404
|
|
103
|
-
raise NotFoundError, "Resource not found: #{
|
|
108
|
+
raise NotFoundError, "Resource not found: #{body}"
|
|
104
109
|
when 500..599
|
|
105
|
-
raise ServerError, "Server error (#{response.
|
|
110
|
+
raise ServerError, "Server error (#{response.status}): #{body}"
|
|
106
111
|
else
|
|
107
|
-
raise Error, "Unexpected response (#{response.
|
|
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
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.
|
|
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:
|