og_pilot_ruby 0.4.0 → 0.4.1
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/og_pilot_ruby/client.rb +41 -5
- data/lib/og_pilot_ruby/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8c58555c3ed39eb0b1bd4948ef10c79492111730917b25ba1d30455079447917
|
|
4
|
+
data.tar.gz: 12b44e9f843610011c42a308a2907449c30598f413aa3a1edc8fa3a3c73b653f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9f99983ecc6ee9de233d087ca93ea3b82f84a7f89c1d5aa75d79c8dbe1aa4bd7ad312cf752ed8f614af04e445028e84c4a8b8969836dd9bb3a51f6d8d6f6feb1
|
|
7
|
+
data.tar.gz: cc38075c320be5bfda43e9d1d0d957ecb8a67564908e56e6b04c5d003ff04cc3098f9b91b11ccda2f43bd37483dc2715ac7c7d58b03c7efd16113bdc80ad000f
|
data/lib/og_pilot_ruby/client.rb
CHANGED
|
@@ -10,6 +10,7 @@ require_relative "jwt_encoder"
|
|
|
10
10
|
module OgPilotRuby
|
|
11
11
|
class Client
|
|
12
12
|
ENDPOINT_PATH = "/api/v1/images"
|
|
13
|
+
MAX_REDIRECTS = 5
|
|
13
14
|
|
|
14
15
|
def initialize(config)
|
|
15
16
|
@config = config
|
|
@@ -24,12 +25,12 @@ module OgPilotRuby
|
|
|
24
25
|
params[:path] = manual_path.to_s.strip.empty? ? resolved_path(default:) : normalize_path(manual_path)
|
|
25
26
|
|
|
26
27
|
uri = build_uri(params, iat:)
|
|
27
|
-
response = request(uri, json:, headers:)
|
|
28
|
+
response, final_uri = request(uri, json:, headers:)
|
|
28
29
|
|
|
29
30
|
if json
|
|
30
31
|
JSON.parse(response.body)
|
|
31
32
|
else
|
|
32
|
-
response["Location"] ||
|
|
33
|
+
response["Location"] || final_uri.to_s
|
|
33
34
|
end
|
|
34
35
|
end
|
|
35
36
|
|
|
@@ -37,18 +38,35 @@ module OgPilotRuby
|
|
|
37
38
|
|
|
38
39
|
attr_reader :config
|
|
39
40
|
|
|
40
|
-
def request(uri, json:, headers:)
|
|
41
|
+
def request(uri, json:, headers:, method: :post, redirects_left: MAX_REDIRECTS)
|
|
41
42
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
42
43
|
http.use_ssl = uri.scheme == "https"
|
|
43
44
|
http.open_timeout = config.open_timeout if config.open_timeout
|
|
44
45
|
http.read_timeout = config.read_timeout if config.read_timeout
|
|
45
46
|
|
|
46
|
-
request =
|
|
47
|
+
request = build_http_request(method, uri)
|
|
47
48
|
request["Accept"] = "application/json" if json
|
|
48
49
|
headers.each { |key, value| request[key] = value }
|
|
49
50
|
|
|
50
51
|
response = http.request(request)
|
|
51
|
-
|
|
52
|
+
if response.is_a?(Net::HTTPRedirection)
|
|
53
|
+
location = response["Location"]
|
|
54
|
+
if location && !location.empty?
|
|
55
|
+
raise OgPilotRuby::RequestError, "OG Pilot request failed with too many redirects" if redirects_left <= 0
|
|
56
|
+
|
|
57
|
+
redirect_uri = URI.join(uri.to_s, location)
|
|
58
|
+
redirect_method = redirect_method_for(response, method)
|
|
59
|
+
return request(
|
|
60
|
+
redirect_uri,
|
|
61
|
+
json:,
|
|
62
|
+
headers:,
|
|
63
|
+
method: redirect_method,
|
|
64
|
+
redirects_left: redirects_left - 1
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
return [response, uri] unless response.is_a?(Net::HTTPClientError) || response.is_a?(Net::HTTPServerError)
|
|
52
70
|
|
|
53
71
|
raise OgPilotRuby::RequestError, "OG Pilot request failed with status #{response.code}: #{response.body}"
|
|
54
72
|
rescue OpenSSL::SSL::SSLError => e
|
|
@@ -63,6 +81,24 @@ module OgPilotRuby
|
|
|
63
81
|
raise OgPilotRuby::RequestError, "OG Pilot request failed with unauthorized: #{e.message}"
|
|
64
82
|
end
|
|
65
83
|
|
|
84
|
+
def build_http_request(method, uri)
|
|
85
|
+
case method
|
|
86
|
+
when :post
|
|
87
|
+
Net::HTTP::Post.new(uri)
|
|
88
|
+
when :get
|
|
89
|
+
Net::HTTP::Get.new(uri)
|
|
90
|
+
else
|
|
91
|
+
raise ArgumentError, "Unsupported HTTP method: #{method.inspect}"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def redirect_method_for(response, current_method)
|
|
96
|
+
return current_method unless current_method == :post
|
|
97
|
+
|
|
98
|
+
status_code = response.code.to_i
|
|
99
|
+
[307, 308].include?(status_code) ? :post : :get
|
|
100
|
+
end
|
|
101
|
+
|
|
66
102
|
def build_uri(params, iat:)
|
|
67
103
|
payload = build_payload(params, iat:)
|
|
68
104
|
token = OgPilotRuby::JwtEncoder.encode(payload, api_key!)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: og_pilot_ruby
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sunergos IT LLC
|
|
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
97
97
|
- !ruby/object:Gem::Version
|
|
98
98
|
version: '0'
|
|
99
99
|
requirements: []
|
|
100
|
-
rubygems_version:
|
|
100
|
+
rubygems_version: 4.0.3
|
|
101
101
|
specification_version: 4
|
|
102
102
|
summary: Ruby client for the OG Pilot Open Graph image generator.
|
|
103
103
|
test_files: []
|