faraday-openapi 0.1.1 → 0.3.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: 3b0869a090382dc9f8780e3039255868de82f073a707f57a7d80293c03a1137e
4
- data.tar.gz: b21feb28749ab4d2adbd5ce16dbc64dc944c375836af229e17e5fabeff9e63cd
3
+ metadata.gz: c5288681710835ea083d20cd469415b3cba5a880cb535692419a0090c0e1432f
4
+ data.tar.gz: 8e4cbe4290b598c8f9fdb5f15eb1505070c83e1dc8bbc91761a01fb9401d4913
5
5
  SHA512:
6
- metadata.gz: '08cb67099b1fb9a6d3ee9f223f7ecc2d0d1fdabad49eb462c6c4262df3433f8b6267fcc31af83679ccfcf7904d7daa6d16d70d6b471caabc482c05a616b349fd'
7
- data.tar.gz: f39f446d15a1c20228a2c1157825eb2b139345e5db794b5e5a5e53ed3b62af7077bcae3ce9d7bcf4233496ae262c9b7ca1e334b298413b0ea30dc70522b06270
6
+ metadata.gz: a7e2eb94fdc7d4e6b96d03485cc6364f7f49d6ab4eb1b13928e03015734e40dcf84cfa21e323935794d23d036bc02945926ffb00a007694e1bf7af00d2919707
7
+ data.tar.gz: 23cfeddcd5ff1dd3cdf7512ae08780799b15bc4ddab990f7edc1246bde72b4cd167bc7b3f8fba76637f702047ec61945b64c5d6eb34ae3fd1808ab80ec1a6c1f
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.3.0
6
+
7
+ - Breaking change: Use `Faraday::Openapi.enabled=` instead of `Faraday::Openapi::Middleware.enabled=`
8
+ - Fix: Make `f.request :openapi` handle only request, not response validation
9
+ - Support passing a Hash to .register
10
+
11
+ ## 0.2.0
12
+
13
+ - Add Faraday::Openapi.register to easily load, cache and reference OADs
14
+
5
15
  ## 0.1.1
6
16
 
7
17
  Fix URL to homepage, changelog
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,36 +29,44 @@ And then execute:
21
29
  bundle install
22
30
  ```
23
31
 
24
- Or install it yourself as:
32
+ ## Usage
25
33
 
26
- ```shell
27
- gem install faraday-openapi
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
- ## Usage
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.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, 'openapi/openapi.yaml'
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, 'openapi/openapi.yaml'
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, 'openapi/openapi.yaml'
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 all middlewares in this gem globally, which you probably want to do on production.
51
67
 
52
68
  ```ruby
53
- Faraday::Openapi::Middleware.default_options[:enabled] = false
69
+ Faraday::Openapi.enabled = false
54
70
  ```
55
71
 
56
72
 
@@ -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
 
@@ -10,5 +10,8 @@ module Faraday
10
10
 
11
11
  # Raised if response does not match API description or is unknown
12
12
  class ResponseInvalidError < Error; end
13
+
14
+ class AlreadyRegisteredError < Error; end
15
+ class NotRegisteredError < Error; end
13
16
  end
14
17
  end
@@ -7,34 +7,24 @@ require_relative 'response'
7
7
 
8
8
  module Faraday
9
9
  module Openapi
10
- # This class provides the main implementation for your middleware.
11
- # Your middleware can implement any of the following methods:
12
- # * on_request - called when the request is being prepared
13
- # * on_complete - called when the response is being processed
14
- #
15
- # Optionally, you can also override the following methods from Faraday::Middleware
16
- # * initialize(app, options = {}) - the initializer method
17
- # * call(env) - the main middleware invocation method.
18
- # This already calls on_request and on_complete, so you normally don't need to override it.
19
- # You may need to in case you need to "wrap" the request or need more control
20
- # (see "retry" middleware: https://github.com/lostisland/faraday-retry/blob/41b7ea27e30d99ebfed958abfa11d12b01f6b6d1/lib/faraday/retry/middleware.rb#L147).
21
- # IMPORTANT: Remember to call `@app.call(env)` or `super` to not interrupt the middleware chain!
22
- class Middleware < Faraday::Middleware
23
- DEFAULT_OPTIONS = { enabled: true }.freeze
24
-
25
- def initialize(app, filepath)
10
+ # Methods for all middlewares
11
+ module Base
12
+ def initialize(app, path = :default)
26
13
  super(app)
27
- @filepath = filepath
28
- @enabled = options.fetch(:enabled, true)
29
- @oad = OpenapiFirst.load(filepath) if @enabled
14
+ return unless Openapi.enabled
15
+
16
+ @oad = path.is_a?(Symbol) ? Faraday::Openapi[path] : OpenapiFirst.load(path)
30
17
  end
31
18
 
32
19
  def call(env)
33
- return app.call(env) unless @enabled
20
+ return app.call(env) unless Openapi.enabled
34
21
 
35
22
  super
36
23
  end
24
+ end
37
25
 
26
+ # on_request method to handle request validation
27
+ module RequestValidation
38
28
  # This method will be called when the request is being prepared.
39
29
  # You can alter it as you like, accessing things like request_body, request_headers, and more.
40
30
  # Refer to Faraday::Env for a list of accessible fields:
@@ -47,7 +37,10 @@ module Faraday
47
37
  rescue OpenapiFirst::RequestInvalidError => e
48
38
  raise RequestInvalidError, e.message
49
39
  end
40
+ end
50
41
 
42
+ # on_complete method to handle response validation
43
+ module ResponseValidation
51
44
  # This method will be called when the response is being processed.
52
45
  # You can alter it as you like, accessing things like response_body, response_headers, and more.
53
46
  # Refer to Faraday::Env for a list of accessible fields:
@@ -64,5 +57,21 @@ module Faraday
64
57
  raise ResponseInvalidError, e.message
65
58
  end
66
59
  end
60
+
61
+ class Middleware < Faraday::Middleware
62
+ include Base
63
+ include RequestValidation
64
+ include ResponseValidation
65
+ end
66
+
67
+ class RequestMiddleware < Faraday::Middleware
68
+ include Base
69
+ include RequestValidation
70
+ end
71
+
72
+ class ResponseMiddleware < Faraday::Middleware
73
+ include Base
74
+ include ResponseValidation
75
+ end
67
76
  end
68
77
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module Openapi
5
- VERSION = '0.1.1'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
@@ -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,36 @@ 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
- Faraday::Request.register_middleware(openapi: Faraday::Openapi::Middleware)
20
- Faraday::Response.register_middleware(openapi: Faraday::Openapi::Middleware)
19
+ Faraday::Request.register_middleware(openapi: Faraday::Openapi::RequestMiddleware)
20
+ Faraday::Response.register_middleware(openapi: Faraday::Openapi::ResponseMiddleware)
21
21
 
22
- # Alternatively, you can register your middleware under Faraday::Request or Faraday::Response.
23
- # This will allow to load your middleware using the `request` or `response` methods respectively.
24
- #
25
- # Load middleware with conn.request :openapi
26
- # Faraday::Request.register_middleware(openapi: Faraday::Openapi::Middleware)
27
- #
28
- # Load middleware with conn.response :openapi
29
- # Faraday::Response.register_middleware(openapi: Faraday::Openapi::Middleware)
22
+ @registry = {}
23
+ @enabled = true
24
+
25
+ class << self
26
+ attr_reader :registry
27
+ attr_accessor :enabled
28
+ end
29
+
30
+ def self.register(filepath, as: :default)
31
+ raise AlreadyRegisteredError, "API description #{as} is already registered" if registry.key?(as)
32
+
33
+ oad = filepath.is_a?(Hash) ? OpenapiFirst.parse(filepath) : OpenapiFirst.load(filepath)
34
+ registry[as] = oad
35
+ end
36
+
37
+ def self.[](key)
38
+ registry.fetch(key) do
39
+ message = if registry.empty?
40
+ 'No API descriptions have been registered. Please register your API description via ' \
41
+ "Faraday::Openapi.register('myopenapi.yaml')"
42
+ else
43
+ "API description #{key.inspect} was not found. Please register your API description via " \
44
+ "Faraday::Openapi.register('myopenapi.yaml'#{key == :default ? '' : ", as: #{key.inspect}"})"
45
+ end
46
+ raise NotRegisteredError, message
47
+ end
48
+ end
30
49
  end
31
50
  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.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-18 00:00:00.000000000 Z
10
+ date: 2025-03-14 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.1.1
96
+ documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.3.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.2
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: []