faraday-openapi 0.1.1 → 0.2.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/CHANGELOG.md +4 -0
- data/README.md +26 -10
- data/lib/faraday/openapi/errors.rb +3 -0
- data/lib/faraday/openapi/middleware.rb +8 -3
- data/lib/faraday/openapi/version.rb +1 -1
- data/lib/faraday/openapi.rb +25 -9
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f4f143b7d1bfe03b425ccb2b7f9551c0f1432495fd49dd9c98f1308a3099901
|
4
|
+
data.tar.gz: 9c0d8e7542a3144f933fdf292ae1fdabfe1a5210a9cea55ac8e035fe769046fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 989d36d2f1704d3bbfa45630eac87f0cf992c7d9f2d3e50fe9a5c5aa840ed0065b6e0b6cf5eaba87b30c0c3d6c9c2cbe2a88bf5d5cc77692dae7f7935b40381b
|
7
|
+
data.tar.gz: a4d6196f44f0df42271dc88c266797a7238c14f9d10bf6322955353d2dc8f78397dec5041f8124c90f94df6bf703ab076f85b9f46ce870877bfffd618ae1e056
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -7,6 +7,14 @@ You can use this to test your client code or to make sure your mocks do match th
|
|
7
7
|
|
8
8
|
Note that the middleware currently deliberately ignores **unknown** responses with status codes 401 or higher because those usually don't come with a useful response body.
|
9
9
|
|
10
|
+
## TL;DR
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
conn = Faraday.new do |f|
|
14
|
+
f.use :openapi
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
10
18
|
## Installation
|
11
19
|
|
12
20
|
Add this line to your application's Gemfile:
|
@@ -21,33 +29,41 @@ And then execute:
|
|
21
29
|
bundle install
|
22
30
|
```
|
23
31
|
|
24
|
-
|
32
|
+
## Usage
|
25
33
|
|
26
|
-
|
27
|
-
|
28
|
-
```
|
34
|
+
In order to avoid loading YAML files at inappropriate times you should
|
35
|
+
register your API description (OAD) globally and reference it via a Symbol in your client code
|
29
36
|
|
30
|
-
|
37
|
+
```ruby
|
38
|
+
# initializer.rb
|
39
|
+
|
40
|
+
require 'faraday/openapi'
|
41
|
+
Faraday::Openapi.register 'dice-openapi.yaml', as: :dice_api
|
42
|
+
|
43
|
+
# Only activate in test env
|
44
|
+
Faraday::Openapi::Middleware.enabled = ENV['RACK_ENV'] == 'test'
|
45
|
+
```
|
31
46
|
|
32
47
|
```ruby
|
48
|
+
# some_client.rb
|
33
49
|
require 'faraday/openapi'
|
34
50
|
|
35
51
|
conn = Faraday.new do |f|
|
36
|
-
f.use :openapi,
|
52
|
+
f.use :openapi, :dice_api
|
37
53
|
end
|
38
54
|
|
39
55
|
# Or validate only requests
|
40
56
|
conn = Faraday.new do |f|
|
41
|
-
f.request :openapi,
|
57
|
+
f.request :openapi, :dice_api
|
42
58
|
end
|
43
59
|
|
44
60
|
# Or validate only responses
|
45
61
|
conn = Faraday.new do |f|
|
46
|
-
f.response :openapi,
|
62
|
+
f.response :openapi, :dice_api
|
47
63
|
end
|
48
64
|
```
|
49
65
|
|
50
|
-
You can disable the whole middleware globally:
|
66
|
+
You can disable the whole middleware globally via Faraday's conventional default_options as well:
|
51
67
|
|
52
68
|
```ruby
|
53
69
|
Faraday::Openapi::Middleware.default_options[:enabled] = false
|
@@ -67,7 +83,7 @@ See how it works [here](https://bundler.io/guides/creating_gem.html#releasing-th
|
|
67
83
|
|
68
84
|
## Contributing
|
69
85
|
|
70
|
-
Bug reports and pull requests are welcome on [Codeberg](https://codeberg.org/ahx/faraday-openapi).
|
86
|
+
Bug reports and pull requests are welcome on [Codeberg](https://codeberg.org/ahx/faraday-openapi) or [Github](https://github.com/ahx/faraday-openapi).
|
71
87
|
|
72
88
|
## License
|
73
89
|
|
@@ -22,11 +22,16 @@ module Faraday
|
|
22
22
|
class Middleware < Faraday::Middleware
|
23
23
|
DEFAULT_OPTIONS = { enabled: true }.freeze
|
24
24
|
|
25
|
-
def
|
25
|
+
def self.enabled=(bool)
|
26
|
+
Faraday::Openapi::Middleware.default_options[:enabled] = bool
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(app, path = :default)
|
26
30
|
super(app)
|
27
|
-
@filepath = filepath
|
28
31
|
@enabled = options.fetch(:enabled, true)
|
29
|
-
|
32
|
+
return unless @enabled
|
33
|
+
|
34
|
+
@oad = path.is_a?(Symbol) ? Faraday::Openapi[path] : OpenapiFirst.load(path)
|
30
35
|
end
|
31
36
|
|
32
37
|
def call(env)
|
data/lib/faraday/openapi.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'openapi_first'
|
3
4
|
require_relative 'openapi/errors'
|
4
5
|
require_relative 'openapi/middleware'
|
5
6
|
require_relative 'openapi/version'
|
@@ -14,18 +15,33 @@ module Faraday
|
|
14
15
|
# After calling this line, the following are both valid ways to set the middleware in a connection:
|
15
16
|
# * conn.use Faraday::Openapi::Middleware
|
16
17
|
# * conn.use :openapi
|
17
|
-
# Without this line, only the former method is valid.
|
18
18
|
Faraday::Middleware.register_middleware(openapi: Faraday::Openapi::Middleware)
|
19
19
|
Faraday::Request.register_middleware(openapi: Faraday::Openapi::Middleware)
|
20
20
|
Faraday::Response.register_middleware(openapi: Faraday::Openapi::Middleware)
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
22
|
+
@registry = {}
|
23
|
+
|
24
|
+
class << self
|
25
|
+
attr_reader :registry
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.register(filepath, as: :default)
|
29
|
+
raise AlreadyRegisteredError, "API description #{as} is already registered" if registry.key?(as)
|
30
|
+
|
31
|
+
registry[as] = OpenapiFirst.load(filepath)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.[](key)
|
35
|
+
registry.fetch(key) do
|
36
|
+
message = if registry.empty?
|
37
|
+
'No API descriptions have been registered. Please register your API description via ' \
|
38
|
+
"Faraday::Openapi.register('myopenapi.yaml')"
|
39
|
+
else
|
40
|
+
"API description #{key.inspect} was not found. Please register your API description via " \
|
41
|
+
"Faraday::Openapi.register('myopenapi.yaml'#{key == :default ? '' : ", as: #{key.inspect}"})"
|
42
|
+
end
|
43
|
+
raise NotRegisteredError, message
|
44
|
+
end
|
45
|
+
end
|
30
46
|
end
|
31
47
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-openapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Haller
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-02-
|
10
|
+
date: 2025-02-26 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: faraday
|
@@ -93,7 +93,7 @@ licenses:
|
|
93
93
|
metadata:
|
94
94
|
bug_tracker_uri: https://codeberg.org/ahx/faraday-openapi/issues
|
95
95
|
changelog_uri: https://codeberg.org/ahx/faraday-openapi/src/branch/main/CHANGELOG.md
|
96
|
-
documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.
|
96
|
+
documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.2.0
|
97
97
|
homepage_uri: https://codeberg.org/ahx/faraday-openapi
|
98
98
|
rubygems_mfa_required: 'true'
|
99
99
|
source_code_uri: https://codeberg.org/ahx/faraday-openapi
|
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
114
|
- !ruby/object:Gem::Version
|
115
115
|
version: '0'
|
116
116
|
requirements: []
|
117
|
-
rubygems_version: 3.6.
|
117
|
+
rubygems_version: 3.6.5
|
118
118
|
specification_version: 4
|
119
119
|
summary: Validate requests/responses against OpenAPI API descriptions
|
120
120
|
test_files: []
|