faraday-openapi 0.7.1 → 0.8.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: 124fb060a81c107c6ab137815d17b225a8aed4dff872525d097012cfd89316a2
4
- data.tar.gz: 84347db962ab71d9022d214b8e887e14f5d9f2c844316d39bedea58869493f00
3
+ metadata.gz: 5d3c71ef695361f23541f335033fdf9b508711d99638deafe46b15e85fa6c21f
4
+ data.tar.gz: 196061fad20d239b70243f23e666d1e319a556210b9dd50e46ee9ba1596f9e33
5
5
  SHA512:
6
- metadata.gz: 074fcb13c202c0dc1cb15c985e748d59d5616d99fd3e3324413f3e64f97d40134c918aa5d8bc8f5ab71e27391572378194974ef2cc6abfdcab755974d3b265b7
7
- data.tar.gz: 17f3913b1ab7faa712bf7b9d441ce2b404ddcd5c36386a3aacd2c901445c1d2acaeef3bb3d8b2dc680dd528423587ea30242c00c300f0e723db10cde562ce145
6
+ metadata.gz: 6e19aafe69037f5073a16d007c9e2147e0112b73ab22fe551614fb6f2885f55b3c7592b1ac46a26e862a7664ba8381dd2ef04549034f8ecc72452b5c2131ae11
7
+ data.tar.gz: '078590082998dece96c35935a23489c550d802478a82cf992a2e2402f49330e0954601f441e27536e6bd31f88b5ba0bb50ce531c1512bfac2db216dcc232dfa7'
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.8.0
6
+ - New Feature: `Faraday::Openapi.register` now takes a `source:` option where you can pass an URL. If the registered file does not exist it will fetch the file from that URL. You can call `Faraday::Openapi.update_registered` to update the registered files.
7
+ - Breaking Change: You know have to user `Faraday::Openapi.register` before using the middleware. Passing a filepath like `f.use :openapi, 'my-openapi.yaml` is no longer supported
8
+
5
9
  ## 0.7.1
6
10
  - Fix an issue with unknown responses (like unknown content-type) when partial_responses are allowed
7
11
 
data/README.md CHANGED
@@ -74,6 +74,16 @@ You can disable all middlewares in this gem globally, which you want to do on pr
74
74
  Faraday::Openapi.enabled = false
75
75
  ```
76
76
 
77
+ ### Working with remote OADs
78
+ You can define the origin of the used OAD via the :source option:
79
+
80
+ ```rb
81
+ Faraday::Openapi.register 'consumed/dice.yaml', as: :dice_api, source: 'http://localhost:8080/apis/dice/openapi.yaml',
82
+ ```
83
+
84
+ If 'consumed/dice.yaml' does NOT exist, it will try to download the file from the defined source.
85
+ You can call `Faraday::Openapi.update_registered` to re-download all registered files.
86
+
77
87
 
78
88
  ## Development
79
89
 
@@ -83,9 +93,6 @@ Then, run `bin/test` to run the tests.
83
93
 
84
94
  To install this gem onto your local machine, run `rake build`.
85
95
 
86
- To release a new version, make a commit with a message such as "Bumped to 0.0.2" and then run `rake release`.
87
- See how it works [here](https://bundler.io/guides/creating_gem.html#releasing-the-gem).
88
-
89
96
  ## Contributing
90
97
 
91
98
  Bug reports and pull requests are welcome on [Codeberg](https://codeberg.org/ahx/faraday-openapi).
@@ -9,12 +9,12 @@ module Faraday
9
9
  module Openapi
10
10
  # Methods for all middlewares
11
11
  module Base
12
- def initialize(app, path = :default)
12
+ def initialize(app, name = :default)
13
13
  super(app)
14
14
  return unless Openapi.enabled
15
15
 
16
- @oad = path.is_a?(Symbol) ? Faraday::Openapi[path] : OpenapiFirst.load(path)
17
- @configuration = Faraday::Openapi.configurations[path]
16
+ @config = Faraday::Openapi[name]
17
+ @oad = @config.oad
18
18
  end
19
19
 
20
20
  def call(env)
@@ -67,7 +67,7 @@ module Faraday
67
67
  end
68
68
 
69
69
  def partial_mocking_enabled?
70
- @configuration && @configuration[:allow_partial_responses]
70
+ @config&.allow_partial_responses
71
71
  end
72
72
  end
73
73
 
@@ -7,7 +7,7 @@ module Faraday
7
7
  # @visibility private
8
8
  # Build a Rack::Request from a Faraday::Env
9
9
  module Request
10
- def self.from_env(env) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
10
+ def self.from_env(env) # rubocop:disable Metrics/MethodLength
11
11
  if env.request_body.is_a?(Hash)
12
12
  raise Error,
13
13
  'env.body is a Hash. You probably want to convert the request body ' \
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module Openapi
5
- VERSION = '0.7.1'
5
+ VERSION = '0.8.0'
6
6
  end
7
7
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'fileutils'
4
+ require 'logger'
3
5
  require 'openapi_first'
4
6
  require_relative 'openapi/errors'
5
7
  require_relative 'openapi/middleware'
@@ -20,29 +22,72 @@ module Faraday
20
22
  Faraday::Response.register_middleware(openapi: Faraday::Openapi::ResponseMiddleware)
21
23
 
22
24
  @registry = {}
23
- @configurations = {}
24
25
  @enabled = true
26
+ @logger = Logger.new(
27
+ $stdout,
28
+ formatter: proc do |_severity, _datetime, _progname, msg|
29
+ "Faraday::Openapi: #{msg}\n"
30
+ end
31
+ )
25
32
 
26
33
  class << self
27
- attr_reader :registry, :configurations
28
- attr_accessor :enabled
34
+ attr_reader :registry
35
+ attr_accessor :enabled, :logger
36
+
37
+ private
38
+
39
+ def connection
40
+ @connection ||= Faraday.new(request: {
41
+ open_timeout: 5,
42
+ timeout: 5
43
+ }) do |f|
44
+ f.response :raise_error, include_request: true
45
+ end
46
+ end
47
+
48
+ def download(name, from:, to:)
49
+ filepath = to
50
+ url = from
51
+ FileUtils.mkdir_p(File.dirname(filepath))
52
+ File.write(filepath, connection.get(url).body)
53
+ logger.info(
54
+ "#{self.class.name} loaded API description for #{name.inspect} " \
55
+ "from #{url.inspect} to #{filepath.inspect}"
56
+ )
57
+ rescue Faraday::Error => e
58
+ raise Faraday::Openapi::Error,
59
+ "I was not able to download the OAD for #{name.inspect} from #{url}: #{e.message}"
60
+ end
29
61
  end
30
62
 
31
- def self.register(filepath, as: :default, allow_partial_responses: false)
63
+ Registered = Struct.new(:oad, :allow_partial_responses, :source, :filepath)
64
+ private_constant :Registered
65
+
66
+ def self.register(filepath, as: :default, allow_partial_responses: false, source: nil)
32
67
  raise AlreadyRegisteredError, "API description #{as} is already registered" if registry.key?(as)
33
68
 
69
+ download(as, from: source, to: filepath) if source && !File.exist?(filepath)
70
+
34
71
  oad = filepath.is_a?(Hash) ? OpenapiFirst.parse(filepath) : OpenapiFirst.load(filepath)
35
- registry[as] = oad
36
- configurations[as] = { allow_partial_responses: }
72
+ registry[as] = Registered.new(oad:, allow_partial_responses:, source:, filepath:)
73
+ self
37
74
  rescue OpenapiFirst::FileNotFoundError => e
38
75
  raise Faraday::Openapi::FileNotFoundError, e.message
39
76
  end
40
77
 
78
+ def self.update_registered
79
+ registry.each do |name, config|
80
+ next unless config.source
81
+
82
+ download(name, from: config.source, to: config.filepath)
83
+ end
84
+ end
85
+
41
86
  def self.[](key)
42
87
  registry.fetch(key) do
43
88
  message = if registry.empty?
44
89
  'No API descriptions have been registered. Please register your API description via ' \
45
- "Faraday::Openapi.register('myopenapi.yaml')"
90
+ "Faraday::Openapi.register('myopenapi.yaml') before using the middleware"
46
91
  else
47
92
  "API description #{key.inspect} was not found. Please register your API description via " \
48
93
  "Faraday::Openapi.register('myopenapi.yaml'#{key == :default ? '' : ", as: #{key.inspect}"})"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Haller
@@ -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.7.1
96
+ documentation_uri: http://www.rubydoc.info/gems/faraday-openapi/0.8.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: 4.0.6
117
+ rubygems_version: 4.0.10
118
118
  specification_version: 4
119
119
  summary: Validate requests/responses against OpenAPI API descriptions
120
120
  test_files: []