licensekit-ruby 0.1.0.alpha.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 +7 -0
- data/.gitignore +4 -0
- data/LICENSE +21 -0
- data/README.md +112 -0
- data/examples/01_create_scoped_api_key.rb +16 -0
- data/examples/02_runtime_validate_and_verify.rb +22 -0
- data/lib/licensekit/client.rb +257 -0
- data/lib/licensekit/errors.rb +62 -0
- data/lib/licensekit/generated/clients.rb +833 -0
- data/lib/licensekit/generated/metadata.rb +536 -0
- data/lib/licensekit/generated/operation_scopes.rb +314 -0
- data/lib/licensekit/scopes.rb +16 -0
- data/lib/licensekit/types.rb +53 -0
- data/lib/licensekit/verification.rb +94 -0
- data/lib/licensekit/version.rb +3 -0
- data/lib/licensekit.rb +9 -0
- data/openapi/openapi.yaml +4394 -0
- data/scripts/generate_from_openapi.rb +294 -0
- data/test/test_client.rb +156 -0
- data/test/test_helper.rb +6 -0
- data/test/test_scopes.rb +14 -0
- data/test/test_verification.rb +44 -0
- metadata +85 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7393f8b686ec83cf5e6ae56c89b4b77143ea5deba5b4303075a55851e40a55c5
|
|
4
|
+
data.tar.gz: 813763b1dd7840a3e2cd4b519185bfbac5868615b7df1e3951b7b542992110f1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f830740d4dcb692b6cce194cfe6aae51b0775f2bc7ed6ea77c6c590db788dc41a47c87c8b5ba9c8a09084eb95ea1f539340a15be6a0fd2b1f5a15346bfbb577a
|
|
7
|
+
data.tar.gz: 8b74306a0065d0198a56d42a8d9edc8a8c4fd02b6908ed8157bbedda6e3b6b6263baeed3c5b81e1da5b0bd0c3ba26c3218efb4e6c073068aa0ddba431dddfdc8
|
data/.gitignore
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 David Main
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# `licensekit-ruby`
|
|
2
|
+
|
|
3
|
+
First-party Ruby SDK for `licensekit.dev`.
|
|
4
|
+
|
|
5
|
+
It provides Management, Runtime, and System clients for the LicenseKit licensing API, plus least-privilege scope metadata and Ed25519 runtime-signature verification helpers for activation, validation, metering, and offline-aware license flows.
|
|
6
|
+
|
|
7
|
+
## Why use it
|
|
8
|
+
|
|
9
|
+
- `LicenseKit::ManagementClient`, `LicenseKit::RuntimeClient`, and `LicenseKit::SystemClient`
|
|
10
|
+
- Runtime signature verification backed by `ed25519`
|
|
11
|
+
- Least-privilege scope discovery derived from the OpenAPI contract
|
|
12
|
+
- Hosted and self-hosted support through a configurable `base_url`
|
|
13
|
+
|
|
14
|
+
Links:
|
|
15
|
+
|
|
16
|
+
- [Agent quickstart](https://licensekit.dev/docs/agent-quickstart)
|
|
17
|
+
- [API contract notes](https://licensekit.dev/docs/api-contract)
|
|
18
|
+
- [OpenAPI spec](https://licensekit.dev/openapi.yaml)
|
|
19
|
+
- [LLM reference](https://licensekit.dev/llms.txt)
|
|
20
|
+
- [Source repository](https://github.com/drmain1/licensekit-ruby)
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
gem install licensekit-ruby
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
require "licensekit"
|
|
32
|
+
|
|
33
|
+
base_url = "https://api.licensekit.dev"
|
|
34
|
+
|
|
35
|
+
system = LicenseKit::SystemClient.new(base_url: base_url)
|
|
36
|
+
health = system.health
|
|
37
|
+
puts health["data"]["status"]
|
|
38
|
+
|
|
39
|
+
management = LicenseKit::ManagementClient.new(
|
|
40
|
+
base_url: base_url,
|
|
41
|
+
token: "lkm_..."
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
product = management.create_product(
|
|
45
|
+
body: {
|
|
46
|
+
"name" => "Example App",
|
|
47
|
+
"code" => "example-app"
|
|
48
|
+
}
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
runtime = LicenseKit::RuntimeClient.new(
|
|
52
|
+
base_url: base_url,
|
|
53
|
+
license_key: "lsk_..."
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
result = runtime.validate_license(
|
|
57
|
+
body: {
|
|
58
|
+
"fingerprint" => "host-123"
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
public_keys = system.list_public_keys
|
|
63
|
+
verified = LicenseKit.verify_runtime_result(
|
|
64
|
+
result,
|
|
65
|
+
LicenseKit::PublicKeyStore.new(public_keys["data"])
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
puts [product["data"]["id"], verified.ok].join(" ")
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Package Shape
|
|
72
|
+
|
|
73
|
+
- `LicenseKit::ManagementClient`
|
|
74
|
+
Uses `Authorization: Bearer <token>` for `/api/v1/...` management operations.
|
|
75
|
+
- `LicenseKit::RuntimeClient`
|
|
76
|
+
Uses `Authorization: License <license-key>` for `/api/v1/license/...` runtime operations.
|
|
77
|
+
- `LicenseKit::SystemClient`
|
|
78
|
+
Unauthenticated access to `/health`, `/healthz`, `/readyz`, `/metrics`, and `/api/v1/system/public-keys`.
|
|
79
|
+
|
|
80
|
+
Hosted deployments should prefer `/health` for liveness checks behind `api.licensekit.dev`.
|
|
81
|
+
`/healthz` remains available for local and self-hosted compatibility.
|
|
82
|
+
|
|
83
|
+
## Scope Metadata
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
required = LicenseKit.get_required_scopes("createProduct")
|
|
87
|
+
allowed = LicenseKit.has_required_scopes("createProduct", ["product:write"])
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Raw Response Access
|
|
91
|
+
|
|
92
|
+
Each client exposes a `raw` companion for callers that need status codes and headers.
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
system = LicenseKit::SystemClient.new(base_url: "https://api.licensekit.dev")
|
|
96
|
+
ready = system.raw.readyz
|
|
97
|
+
|
|
98
|
+
puts [ready.status, ready.data["data"]["status"]].join(" ")
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Development
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
bundle install
|
|
105
|
+
ruby scripts/generate_from_openapi.rb
|
|
106
|
+
ruby -Ilib:test test/test_client.rb
|
|
107
|
+
ruby -Ilib:test test/test_scopes.rb
|
|
108
|
+
ruby -Ilib:test test/test_verification.rb
|
|
109
|
+
gem build licensekit-ruby.gemspec
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Generation uses the checked-in OpenAPI snapshot at [`openapi/openapi.yaml`](./openapi/openapi.yaml).
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require "licensekit"
|
|
2
|
+
|
|
3
|
+
base_url = ENV.fetch("LICENSEKIT_BASE_URL", "https://api.licensekit.dev")
|
|
4
|
+
management = LicenseKit::ManagementClient.new(
|
|
5
|
+
base_url: base_url,
|
|
6
|
+
token: ENV.fetch("LICENSEKIT_MANAGEMENT_TOKEN")
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
response = management.create_api_key(
|
|
10
|
+
body: {
|
|
11
|
+
"name" => "product-read",
|
|
12
|
+
"scopes" => ["product:read"]
|
|
13
|
+
}
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
puts response["data"]["api_key"]["name"]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require "licensekit"
|
|
2
|
+
|
|
3
|
+
base_url = ENV.fetch("LICENSEKIT_BASE_URL", "https://api.licensekit.dev")
|
|
4
|
+
runtime = LicenseKit::RuntimeClient.new(
|
|
5
|
+
base_url: base_url,
|
|
6
|
+
license_key: ENV.fetch("LICENSEKIT_LICENSE_KEY")
|
|
7
|
+
)
|
|
8
|
+
system = LicenseKit::SystemClient.new(base_url: base_url)
|
|
9
|
+
|
|
10
|
+
result = runtime.validate_license(
|
|
11
|
+
body: {
|
|
12
|
+
"fingerprint" => ENV.fetch("LICENSEKIT_FINGERPRINT", "host-123")
|
|
13
|
+
}
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
public_keys = system.list_public_keys
|
|
17
|
+
verified = LicenseKit.verify_runtime_result(
|
|
18
|
+
result,
|
|
19
|
+
LicenseKit::PublicKeyStore.new(public_keys["data"])
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
puts [result["data"]["status"], verified.ok].join(" ")
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
require "cgi"
|
|
2
|
+
require "date"
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "time"
|
|
6
|
+
require "uri"
|
|
7
|
+
|
|
8
|
+
module LicenseKit
|
|
9
|
+
class TransportError < StandardError; end
|
|
10
|
+
|
|
11
|
+
def self.normalize_base_url(base_url)
|
|
12
|
+
trimmed = base_url.to_s.strip
|
|
13
|
+
raise ArgumentError, "base_url is required" if trimmed.empty?
|
|
14
|
+
|
|
15
|
+
trimmed.sub(%r{/+\z}, "")
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class DefaultTransport
|
|
19
|
+
def call(request)
|
|
20
|
+
uri = URI.parse(request.url)
|
|
21
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
22
|
+
http.use_ssl = uri.scheme == "https"
|
|
23
|
+
if request.timeout
|
|
24
|
+
http.open_timeout = request.timeout
|
|
25
|
+
http.read_timeout = request.timeout
|
|
26
|
+
http.write_timeout = request.timeout if http.respond_to?(:write_timeout=)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
http_request = request_class(request.method).new(uri.request_uri)
|
|
30
|
+
request.headers.each do |key, value|
|
|
31
|
+
http_request[key] = value
|
|
32
|
+
end
|
|
33
|
+
http_request.body = request.body unless request.body.nil?
|
|
34
|
+
|
|
35
|
+
response = http.request(http_request)
|
|
36
|
+
TransportResponse.new(
|
|
37
|
+
status: response.code.to_i,
|
|
38
|
+
headers: response.each_header.to_h,
|
|
39
|
+
body: response.body.to_s
|
|
40
|
+
)
|
|
41
|
+
rescue StandardError => e
|
|
42
|
+
raise TransportError, e.message
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def request_class(method)
|
|
48
|
+
case method.to_s.upcase
|
|
49
|
+
when "GET" then Net::HTTP::Get
|
|
50
|
+
when "POST" then Net::HTTP::Post
|
|
51
|
+
when "PATCH" then Net::HTTP::Patch
|
|
52
|
+
when "PUT" then Net::HTTP::Put
|
|
53
|
+
when "DELETE" then Net::HTTP::Delete
|
|
54
|
+
else
|
|
55
|
+
raise ArgumentError, "Unsupported HTTP method: #{method}"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
class BaseClient
|
|
61
|
+
def initialize(base_url:, auth_type:, auth_value:, headers: nil, timeout: nil, user_agent: nil, retry_options: nil, transport: nil)
|
|
62
|
+
@base_url = LicenseKit.normalize_base_url(base_url)
|
|
63
|
+
@default_headers = normalize_headers(headers)
|
|
64
|
+
@timeout = timeout
|
|
65
|
+
@user_agent = user_agent
|
|
66
|
+
@auth_type = auth_type
|
|
67
|
+
@auth_value = auth_value
|
|
68
|
+
@retry = retry_options || RetryOptions.new
|
|
69
|
+
@transport = transport || DefaultTransport.new
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def perform_request(operation_id, request = nil, options: nil, **kwargs)
|
|
75
|
+
perform_request_raw(operation_id, request, options: options, **kwargs).data
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def perform_request_raw(operation_id, request = nil, options: nil, **kwargs)
|
|
79
|
+
metadata = Generated::OPERATION_METADATA.fetch(operation_id)
|
|
80
|
+
normalized_request = normalize_request(request, kwargs)
|
|
81
|
+
url = build_url(metadata[:path], normalized_request)
|
|
82
|
+
headers = build_headers(options, normalized_request)
|
|
83
|
+
body = nil
|
|
84
|
+
|
|
85
|
+
if normalized_request.key?(:body)
|
|
86
|
+
headers["Content-Type"] = "application/json"
|
|
87
|
+
body = JSON.generate(normalized_request[:body])
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
transport_request = TransportRequest.new(
|
|
91
|
+
method: metadata[:method],
|
|
92
|
+
url: url,
|
|
93
|
+
headers: headers,
|
|
94
|
+
body: body,
|
|
95
|
+
timeout: options&.timeout || @timeout
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
response = send_with_retry(transport_request)
|
|
99
|
+
success_kind = metadata[:success][response.status]
|
|
100
|
+
raise ApiError.from_response(response.status, parse_error_body(response)) if success_kind.nil?
|
|
101
|
+
|
|
102
|
+
RawResponse.new(
|
|
103
|
+
status: response.status,
|
|
104
|
+
headers: response.headers,
|
|
105
|
+
data: parse_success_body(response, success_kind),
|
|
106
|
+
response: response,
|
|
107
|
+
body: response.body
|
|
108
|
+
)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def normalize_request(request, kwargs)
|
|
112
|
+
merged = {}
|
|
113
|
+
merged.merge!(request) if request.is_a?(Hash)
|
|
114
|
+
merged.merge!(kwargs.transform_keys(&:to_sym))
|
|
115
|
+
merged
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def build_url(path_template, request)
|
|
119
|
+
path = path_template.dup
|
|
120
|
+
path_values = request[:path]
|
|
121
|
+
if path_values
|
|
122
|
+
raise TypeError, "request[:path] must be a hash" unless path_values.is_a?(Hash)
|
|
123
|
+
|
|
124
|
+
path_values.each do |key, value|
|
|
125
|
+
escaped = CGI.escape(value.to_s).gsub("+", "%20")
|
|
126
|
+
path.gsub!("{#{key}}", escaped)
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
raise ArgumentError, "Missing path parameters for #{path_template}" if path.include?("{") || path.include?("}")
|
|
131
|
+
|
|
132
|
+
query_values = request[:query]
|
|
133
|
+
return "#{@base_url}#{path}" if query_values.nil? || query_values.empty?
|
|
134
|
+
raise TypeError, "request[:query] must be a hash" unless query_values.is_a?(Hash)
|
|
135
|
+
|
|
136
|
+
encoded = URI.encode_www_form(iter_query_params(query_values))
|
|
137
|
+
"#{@base_url}#{path}?#{encoded}"
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def iter_query_params(query)
|
|
141
|
+
query.each_with_object([]) do |(key, value), items|
|
|
142
|
+
normalize_query_value(key.to_s, value).each { |entry| items << entry }
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def normalize_query_value(key, value)
|
|
147
|
+
case value
|
|
148
|
+
when nil
|
|
149
|
+
[]
|
|
150
|
+
when Array
|
|
151
|
+
value.flat_map { |item| normalize_query_value(key, item) }
|
|
152
|
+
when Date, DateTime, Time
|
|
153
|
+
[[key, value.iso8601]]
|
|
154
|
+
when true
|
|
155
|
+
[[key, "true"]]
|
|
156
|
+
when false
|
|
157
|
+
[[key, "false"]]
|
|
158
|
+
else
|
|
159
|
+
[[key, value.to_s]]
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def build_headers(options, request)
|
|
164
|
+
headers = @default_headers.dup
|
|
165
|
+
headers.merge!(normalize_headers(options.headers)) if options&.headers
|
|
166
|
+
|
|
167
|
+
headers["Accept"] ||= "application/json"
|
|
168
|
+
headers["User-Agent"] ||= @user_agent if @user_agent
|
|
169
|
+
|
|
170
|
+
case @auth_type
|
|
171
|
+
when "bearer"
|
|
172
|
+
headers["Authorization"] = "Bearer #{@auth_value}" if @auth_value
|
|
173
|
+
when "license"
|
|
174
|
+
headers["Authorization"] = "License #{@auth_value}" if @auth_value
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
headers["Idempotency-Key"] = request[:idempotency_key].to_s if request.key?(:idempotency_key)
|
|
178
|
+
headers
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def send_with_retry(transport_request)
|
|
182
|
+
retries = @retry.retries
|
|
183
|
+
retryable_methods = @retry.retryable_methods
|
|
184
|
+
should_retry = retryable_methods.include?(transport_request.method.to_s.upcase)
|
|
185
|
+
|
|
186
|
+
attempt = 0
|
|
187
|
+
begin
|
|
188
|
+
attempt += 1
|
|
189
|
+
response = @transport.call(transport_request)
|
|
190
|
+
coerce_transport_response(response)
|
|
191
|
+
rescue StandardError => e
|
|
192
|
+
raise e unless should_retry && attempt <= retries
|
|
193
|
+
|
|
194
|
+
sleep([0.1 * attempt, 0.5].min)
|
|
195
|
+
retry
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def coerce_transport_response(response)
|
|
200
|
+
return response if response.is_a?(TransportResponse)
|
|
201
|
+
|
|
202
|
+
if response.is_a?(Hash)
|
|
203
|
+
return TransportResponse.new(
|
|
204
|
+
status: response.fetch(:status) { response.fetch("status") },
|
|
205
|
+
headers: response[:headers] || response["headers"] || {},
|
|
206
|
+
body: response[:body] || response["body"] || ""
|
|
207
|
+
)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
if response.respond_to?(:status) && response.respond_to?(:headers) && response.respond_to?(:body)
|
|
211
|
+
return TransportResponse.new(status: response.status, headers: response.headers, body: response.body)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
raise TypeError, "transport must return a TransportResponse-compatible object"
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def parse_success_body(response, kind)
|
|
218
|
+
case kind
|
|
219
|
+
when "empty"
|
|
220
|
+
nil
|
|
221
|
+
when "text"
|
|
222
|
+
response.body.to_s
|
|
223
|
+
else
|
|
224
|
+
JSON.parse(response.body.to_s)
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def parse_error_body(response)
|
|
229
|
+
content_type = header_value(response.headers, "Content-Type").to_s
|
|
230
|
+
if content_type.include?("application/json")
|
|
231
|
+
JSON.parse(response.body.to_s)
|
|
232
|
+
else
|
|
233
|
+
response.body.to_s
|
|
234
|
+
end
|
|
235
|
+
rescue JSON::ParserError
|
|
236
|
+
nil
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def normalize_headers(headers)
|
|
240
|
+
return {} if headers.nil?
|
|
241
|
+
raise TypeError, "headers must be a hash" unless headers.is_a?(Hash)
|
|
242
|
+
|
|
243
|
+
headers.each_with_object({}) do |(key, value), result|
|
|
244
|
+
result[key.to_s] = value.to_s
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def header_value(headers, name)
|
|
249
|
+
return nil unless headers
|
|
250
|
+
|
|
251
|
+
headers.each do |key, value|
|
|
252
|
+
return value if key.to_s.downcase == name.downcase
|
|
253
|
+
end
|
|
254
|
+
nil
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module LicenseKit
|
|
2
|
+
class ApiError < StandardError
|
|
3
|
+
attr_reader :status, :code, :detail, :request_id, :timestamp, :body
|
|
4
|
+
|
|
5
|
+
def initialize(status:, code:, message:, detail: nil, request_id: nil, timestamp: nil, body: nil)
|
|
6
|
+
super(message)
|
|
7
|
+
@status = status
|
|
8
|
+
@code = code
|
|
9
|
+
@detail = detail
|
|
10
|
+
@request_id = request_id
|
|
11
|
+
@timestamp = timestamp
|
|
12
|
+
@body = body
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.from_response(status, body)
|
|
16
|
+
envelope = LicenseKit.parse_error_envelope(body)
|
|
17
|
+
return new(status: status, code: "UNKNOWN_ERROR", message: "Request failed with status #{status}", body: body) if envelope.nil?
|
|
18
|
+
|
|
19
|
+
meta = envelope["meta"] || {}
|
|
20
|
+
new(
|
|
21
|
+
status: status,
|
|
22
|
+
code: envelope["error"]["code"],
|
|
23
|
+
message: envelope["error"]["message"],
|
|
24
|
+
detail: envelope["error"]["detail"],
|
|
25
|
+
request_id: meta["request_id"],
|
|
26
|
+
timestamp: meta["timestamp"],
|
|
27
|
+
body: body
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.api_error?(value)
|
|
33
|
+
value.is_a?(ApiError)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.parse_error_envelope(body)
|
|
37
|
+
return nil unless body.is_a?(Hash)
|
|
38
|
+
|
|
39
|
+
error = body["error"]
|
|
40
|
+
return nil unless error.is_a?(Hash)
|
|
41
|
+
return nil unless error["code"].is_a?(String) && error["message"].is_a?(String)
|
|
42
|
+
|
|
43
|
+
result = {
|
|
44
|
+
"error" => {
|
|
45
|
+
"code" => error["code"],
|
|
46
|
+
"message" => error["message"]
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
result["error"]["detail"] = error["detail"] if error["detail"].is_a?(String)
|
|
51
|
+
|
|
52
|
+
meta = body["meta"]
|
|
53
|
+
if meta.is_a?(Hash) && meta["request_id"].is_a?(String) && meta["timestamp"].is_a?(String)
|
|
54
|
+
result["meta"] = {
|
|
55
|
+
"request_id" => meta["request_id"],
|
|
56
|
+
"timestamp" => meta["timestamp"]
|
|
57
|
+
}
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
result
|
|
61
|
+
end
|
|
62
|
+
end
|