zaikio-client-helpers 0.2.2 → 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: b04e37308c3d4d671262df75c9e71a56feccfa2ecbddd561a5c495d9bb08116c
4
- data.tar.gz: f9cd398097d4e20637398ce37bf75cd035d52385f6beafb58d32c8d787ac50b5
3
+ metadata.gz: a7c1175b44ca6b5ce9c35a6c24e3c2c546da668af38553303510b6e3ebb549f5
4
+ data.tar.gz: 57f77bce22039beedee0f51da1ff1655dc3e602de987d3d13ce594337e5f37c0
5
5
  SHA512:
6
- metadata.gz: 41d8e99d08d8dc5bc2f6cfcb247285857222a830ac74a73da9339cd293aedf43fdcd0ac74a9fb4d08479028c7a34956c1afb00406a7655e33769fbca39132a47
7
- data.tar.gz: '08baf07f1cee0553900681da973abd696ab9f12b40c4dd03d8df46fb27402d20716721dd537908b77c14993e9b0ca5f59fe798ca0ae8f8cf00562cee2f8ac41a'
6
+ metadata.gz: 1eeb54b3661b5aa9d7730fa2751e1632273a63011cf3259f7db5df8228bd9b1208951b692332a54bf449750f1bde6ef8ea23f97d3927c896f96042a628e7d508
7
+ data.tar.gz: a22972654c7b0a53f5844a7005d3afb08345cfab355d5493ff58c99fb2e28030f54854bc8bdb32aec118723470df0cd58d98ba101feaf1e04ff16f6295eceb3b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2022-08-15
4
+
5
+ - Add `Zaikio::Client::Helpers::AuthorizationMiddleware` and `Zaikio::Client.with_token` to pass down bearer token to multiple clients (e.g. hub + procurement) at the same time
6
+ - Add `Zaikio::Client::Helpers::Configuration` as an abstract configuration class
7
+ - Add `Zaikio::Client.create_connection`
8
+
9
+ ## [0.2.4] - 2022-03-29
10
+
11
+ - Add support for Faraday 2.x
12
+
13
+ ## [0.2.3] - 2021-11-12
14
+
15
+ - Handle "uncountable" responses (i.e. those without a Total-Count or Total-Pages header)
16
+
3
17
  ## [0.2.2] - 2021-08-12
4
18
 
5
19
  - Attempt to fix `NoMethodError` when looking for pagination headers
@@ -0,0 +1,27 @@
1
+ require "faraday"
2
+
3
+ module Zaikio
4
+ module Client
5
+ module Helpers
6
+ class AuthorizationMiddleware < Faraday::Middleware
7
+ def self.token
8
+ Thread.current[:zaikio_client_access_token]
9
+ end
10
+
11
+ def self.token=(value)
12
+ Thread.current[:zaikio_client_access_token] = value
13
+ end
14
+
15
+ def self.reset_token
16
+ Thread.current[:zaikio_client_access_token] = nil
17
+ end
18
+
19
+ def call(request_env)
20
+ request_env[:request_headers]["Authorization"] = "Bearer #{self.class.token}" if self.class.token
21
+
22
+ @app.call(request_env)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ require "logger"
2
+
3
+ module Zaikio
4
+ module Client
5
+ module Helpers
6
+ class Configuration
7
+ attr_accessor :host
8
+ attr_reader :environment
9
+ attr_writer :logger
10
+
11
+ def self.hosts
12
+ raise NotImplementedError
13
+ end
14
+
15
+ def initialize
16
+ self.environment = :sandbox
17
+ end
18
+
19
+ def logger
20
+ @logger ||= Logger.new($stdout)
21
+ end
22
+
23
+ def environment=(env)
24
+ @environment = env.to_sym
25
+ @host = host_for(environment)
26
+ end
27
+
28
+ private
29
+
30
+ def host_for(environment)
31
+ self.class.hosts.fetch(environment) do
32
+ raise "Invalid Zaikio::Client environment '#{environment}'"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -2,7 +2,13 @@ require "faraday"
2
2
  require "multi_json"
3
3
 
4
4
  module Zaikio::Client::Helpers
5
- class JSONParser < Faraday::Response::Middleware
5
+ superclass = if Gem.loaded_specs["faraday"].version >= Gem::Version.new("2.0")
6
+ Faraday::Middleware
7
+ else
8
+ Faraday::Response::Middleware
9
+ end
10
+
11
+ JSONParser = Class.new(superclass) do
6
12
  def on_complete(env)
7
13
  case env.status
8
14
  when 404 then raise Spyke::ResourceNotFound.new(nil, url: env.url)
@@ -11,6 +11,12 @@ module Zaikio::Client::Helpers
11
11
 
12
12
  METADATA_KEY = :pagination
13
13
 
14
+ superclass = if Gem.loaded_specs["faraday"].version >= Gem::Version.new("2.0")
15
+ Faraday::Middleware
16
+ else
17
+ Faraday::Response::Middleware
18
+ end
19
+
14
20
  # Faraday Middleware for extracting any pagination headers into the a top-level
15
21
  # :metadata hash, or the env hash for non-JSON responses.
16
22
  #
@@ -22,7 +28,7 @@ module Zaikio::Client::Helpers
22
28
  # response = conn.get("/")
23
29
  # response.env[METADATA_KEY]
24
30
  # #=> {total_count: 4, total_pages: 1, current_page: 1}
25
- class FaradayMiddleware < Faraday::Response::Middleware
31
+ FaradayMiddleware = Class.new(superclass) do
26
32
  def on_complete(env)
27
33
  @env = env
28
34
 
@@ -76,7 +82,11 @@ module Zaikio::Client::Helpers
76
82
  end
77
83
 
78
84
  def last_page?
79
- current_page >= total_pages
85
+ if total_pages.nil?
86
+ find_some.empty?
87
+ else
88
+ current_page >= total_pages
89
+ end
80
90
  end
81
91
 
82
92
  def supports_pagination?
@@ -3,7 +3,7 @@
3
3
  module Zaikio
4
4
  module Client
5
5
  module Helpers
6
- VERSION = "0.2.2"
6
+ VERSION = "0.3.0"
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,31 @@
1
+ require "zaikio/client/model"
2
+ require "zaikio/client/helpers/json_parser"
3
+ require "zaikio/client/helpers/pagination"
4
+ require "zaikio/client/helpers/configuration"
5
+ require "zaikio/client/helpers/authorization_middleware"
6
+
7
+ module Zaikio
8
+ module Client
9
+ class << self
10
+ def with_token(token)
11
+ original_token = Helpers::AuthorizationMiddleware.token
12
+ Helpers::AuthorizationMiddleware.token = token
13
+ yield
14
+ ensure
15
+ Helpers::AuthorizationMiddleware.token = original_token
16
+ end
17
+
18
+ def create_connection(configuration)
19
+ Faraday.new(url: configuration.host,
20
+ ssl: { verify: configuration.environment != :test }) do |c|
21
+ c.request :json
22
+ c.response :logger, configuration&.logger, headers: false
23
+ c.use Zaikio::Client::Helpers::Pagination::FaradayMiddleware
24
+ c.use Zaikio::Client::Helpers::JSONParser
25
+ c.use Zaikio::Client::Helpers::AuthorizationMiddleware
26
+ c.adapter Faraday.default_adapter
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -3,6 +3,4 @@
3
3
  require_relative "zaikio/client/helpers/version"
4
4
 
5
5
  require_relative "zaikio/error"
6
- require_relative "zaikio/client/model"
7
- require_relative "zaikio/client/helpers/json_parser"
8
- require_relative "zaikio/client/helpers/pagination"
6
+ require_relative "zaikio/client"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaikio-client-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zaikio GMBH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-13 00:00:00.000000000 Z
11
+ date: 2022-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,14 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: '0'
29
+ version: '1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: multi_json
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -42,16 +48,16 @@ dependencies:
42
48
  name: spyke
43
49
  requirement: !ruby/object:Gem::Requirement
44
50
  requirements:
45
- - - ">="
51
+ - - "~>"
46
52
  - !ruby/object:Gem::Version
47
- version: '0'
53
+ version: '6'
48
54
  type: :runtime
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
- - - ">="
58
+ - - "~>"
53
59
  - !ruby/object:Gem::Version
54
- version: '0'
60
+ version: '6'
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: vcr
57
63
  requirement: !ruby/object:Gem::Requirement
@@ -92,6 +98,9 @@ files:
92
98
  - README.md
93
99
  - Rakefile
94
100
  - lib/zaikio-client-helpers.rb
101
+ - lib/zaikio/client.rb
102
+ - lib/zaikio/client/helpers/authorization_middleware.rb
103
+ - lib/zaikio/client/helpers/configuration.rb
95
104
  - lib/zaikio/client/helpers/json_parser.rb
96
105
  - lib/zaikio/client/helpers/pagination.rb
97
106
  - lib/zaikio/client/helpers/version.rb