haveapi-go-client 0.19.3 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e280d2ae1a1a0bad8fb3436d97493f3b15f6accb65e919af2fa7cb0fc751108f
4
- data.tar.gz: 21f2f8bdf6ddf4f0d28e994fabbc865be6a0cce7fc1a9f67c868a02ab2d8427f
3
+ metadata.gz: 7a7636a78332b0aa0b89c5eb21f0d3d0944629c006165e899670677ee3e8e83e
4
+ data.tar.gz: d1cb9bcd4d094670197d613aaa8775724e02304ed5b073292423056ec33a4621
5
5
  SHA512:
6
- metadata.gz: 8b520855fe4036001908341c247bf9b71b6868ba91fdbba5dab188b45f479be40bd1dbe12825ee2499d8d1e84ca2d3064ca6e5fe8b70111b6490288662a6a7b4
7
- data.tar.gz: 570f73252a60726a109e3c23bddbc5f09aa3f2543f9706d8db4a901e96ea7fa87bccbaf4dd13aa713bc695b795097137f1fb6452255354e06f89b808e3b897db
6
+ metadata.gz: 8b67cc15963852f808c5554acdd668b6813e2b06d6ff721d3157fc348de8bd466399fd14bd73e5c8571b678305ad4198b31db0b284070acef090172f3186e7e4
7
+ data.tar.gz: f9932502872003d594552901d06895a62869abb4feb2e289546e4c7659c73fbb98180d85fbe9c867dd8e09416d20dca16736925c42f1c6fc1c23a0e030e004f3
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
4
  gem 'haveapi-client', path: '../ruby'
5
+
6
+ group :development do
7
+ gem 'bundler'
8
+ gem 'rake'
9
+ end
@@ -1,6 +1,5 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
1
+ lib = File.expand_path('lib', __dir__)
2
+ $:.unshift(lib) unless $:.include?(lib)
4
3
  require 'haveapi/go_client/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
@@ -9,17 +8,15 @@ Gem::Specification.new do |spec|
9
8
  spec.authors = ['Jakub Skokan']
10
9
  spec.email = ['jakub.skokan@vpsfree.cz']
11
10
  spec.summary =
12
- spec.description = 'Go client generator'
11
+ spec.description = 'Go client generator'
13
12
  spec.homepage = ''
14
13
  spec.license = 'MIT'
15
14
 
15
+ spec.required_ruby_version = ">= #{File.read('../../.ruby-version').strip}"
16
+
16
17
  spec.files = `git ls-files -z`.split("\x0")
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency 'bundler'
22
- spec.add_development_dependency 'rake'
23
-
24
- spec.add_runtime_dependency 'haveapi-client', '~> 0.19.3'
21
+ spec.add_runtime_dependency 'haveapi-client', '~> 0.21.0'
25
22
  end
@@ -59,12 +59,12 @@ module HaveAPI::GoClient
59
59
  @name = name.to_s
60
60
  @prefix = prefix
61
61
  @aliases = desc[:aliases]
62
- @full_dot_name = resource.full_dot_name + '#' + @name.capitalize
62
+ @full_dot_name = "#{resource.full_dot_name}##{@name.capitalize}"
63
63
  @go_name = camelize(name)
64
64
  @go_type = full_go_type
65
- @go_invocation_type = go_type + 'Invocation'
66
- @go_request_type = go_type + 'Request'
67
- @go_response_type = go_type + 'Response'
65
+ @go_invocation_type = "#{go_type}Invocation"
66
+ @go_request_type = "#{go_type}Request"
67
+ @go_response_type = "#{go_type}Response"
68
68
  @input = desc[:input] && InputOutput.new(self, :io, :input, desc[:input])
69
69
  @output = desc[:output] && InputOutput.new(self, :io, :output, desc[:output])
70
70
  @http_method = desc[:method]
@@ -96,7 +96,7 @@ module HaveAPI::GoClient
96
96
  end
97
97
 
98
98
  def input_output
99
- %i(input output).select do |v|
99
+ %i[input output].select do |v|
100
100
  send(v) && send(v).parameters.any?
101
101
  end.map { |v| send(v) }
102
102
  end
@@ -106,9 +106,7 @@ module HaveAPI::GoClient
106
106
  end
107
107
 
108
108
  def resolve_associations
109
- input_output.each do |io|
110
- io.resolve_associations
111
- end
109
+ input_output.each(&:resolve_associations)
112
110
 
113
111
  metadata && metadata.resolve_associations
114
112
  end
@@ -118,6 +116,7 @@ module HaveAPI::GoClient
118
116
  end
119
117
 
120
118
  protected
119
+
121
120
  attr_reader :prefix
122
121
 
123
122
  def full_go_type
@@ -126,7 +125,7 @@ module HaveAPI::GoClient
126
125
  names << 'Action'
127
126
  names.concat(resource.resource_path.map(&:go_name))
128
127
  names << go_name
129
- names.join('')
128
+ names.join
130
129
  end
131
130
  end
132
131
  end
@@ -14,7 +14,7 @@ module HaveAPI::GoClient
14
14
  Resource.new(self, k, v)
15
15
  end.sort!
16
16
 
17
- @resources.each { |r| r.resolve_associations }
17
+ @resources.each(&:resolve_associations)
18
18
 
19
19
  @auth_methods = desc[:authentication].map do |k, v|
20
20
  AuthenticationMethods.new(self, k, v)
@@ -5,6 +5,8 @@ module HaveAPI::GoClient
5
5
  AuthenticationMethods.register(name, self)
6
6
  end
7
7
 
8
+ def initialize(api_version, name, desc); end
9
+
8
10
  # @param dst [String]
9
11
  def generate(dst)
10
12
  raise NotImplementedError
@@ -4,15 +4,11 @@ module HaveAPI::GoClient
4
4
  class Authentication::Basic < Authentication::Base
5
5
  register :basic
6
6
 
7
- def initialize(api_version, name, desc)
8
-
9
- end
10
-
11
7
  def generate(gen)
12
8
  ErbTemplate.render_to_if_changed(
13
9
  'authentication/basic.go',
14
10
  {
15
- package: gen.package,
11
+ package: gen.package
16
12
  },
17
13
  File.join(gen.dst, 'auth_basic.go')
18
14
  )
@@ -0,0 +1,32 @@
1
+ require 'haveapi/go_client/authentication/base'
2
+
3
+ module HaveAPI::GoClient
4
+ class Authentication::OAuth2 < Authentication::Base
5
+ register :oauth2
6
+
7
+ # HTTP header the token is sent in
8
+ # @return [String]
9
+ attr_reader :http_header
10
+
11
+ # Token revocation URL
12
+ # @return [String]
13
+ attr_reader :revoke_url
14
+
15
+ def initialize(api_version, name, desc)
16
+ super
17
+ @http_header = desc[:http_header]
18
+ @revoke_url = desc[:revoke_url]
19
+ end
20
+
21
+ def generate(gen)
22
+ ErbTemplate.render_to_if_changed(
23
+ 'authentication/oauth2.go',
24
+ {
25
+ package: gen.package,
26
+ auth: self
27
+ },
28
+ File.join(gen.dst, 'auth_oauth2.go')
29
+ )
30
+ end
31
+ end
32
+ end
@@ -17,13 +17,14 @@ module HaveAPI::GoClient
17
17
  attr_reader :resource
18
18
 
19
19
  def initialize(api_version, name, desc)
20
+ super
20
21
  @http_header = desc[:http_header]
21
22
  @query_parameter = desc[:query_parameter]
22
23
  @resource = Resource.new(
23
24
  api_version,
24
25
  :token,
25
26
  desc[:resources][:token],
26
- prefix: 'auth_token',
27
+ prefix: 'auth_token'
27
28
  )
28
29
  resource.resolve_associations
29
30
  end
@@ -33,7 +34,7 @@ module HaveAPI::GoClient
33
34
  'authentication/token.go',
34
35
  {
35
36
  package: gen.package,
36
- auth: self,
37
+ auth: self
37
38
  },
38
39
  File.join(gen.dst, 'auth_token.go')
39
40
  )
@@ -49,7 +50,7 @@ module HaveAPI::GoClient
49
50
  # @return [Array<Action>]
50
51
  def custom_actions
51
52
  @custom_actions ||= resource.actions.reject do |a|
52
- %w(request renew revoke).include?(a.name)
53
+ %w[request renew revoke].include?(a.name)
53
54
  end
54
55
  end
55
56
  end
@@ -3,11 +3,10 @@ require 'haveapi/go_client/authentication/base'
3
3
  module HaveAPI::GoClient
4
4
  class Authentication::Unsupported < Authentication::Base
5
5
  def initialize(api_version, name, desc)
6
+ super
6
7
  warn "Ignoring unsupported authentication method #{name}"
7
8
  end
8
9
 
9
- def generate(gen)
10
-
11
- end
10
+ def generate(gen); end
12
11
  end
13
12
  end
@@ -1,5 +1,5 @@
1
1
  module HaveAPI::GoClient
2
- module Authentication ; end
2
+ module Authentication; end
3
3
 
4
4
  module AuthenticationMethods
5
5
  # @param name [Symbol]
@@ -16,9 +16,9 @@ module HaveAPI::GoClient
16
16
 
17
17
  # @param api_version [ApiVersion]
18
18
  # @param name [String]
19
- def self.new(api_version, name, *args)
19
+ def self.new(api_version, name, *)
20
20
  klass = get(name) || Authentication::Unsupported
21
- klass.new(api_version, name, *args)
21
+ klass.new(api_version, name, *)
22
22
  end
23
23
  end
24
24
  end
@@ -5,7 +5,7 @@ module HaveAPI::GoClient
5
5
  class Cli
6
6
  def self.run
7
7
  options = {
8
- package: 'client',
8
+ package: 'client'
9
9
  }
10
10
 
11
11
  parser = OptionParser.new do |opts|
@@ -26,7 +26,7 @@ module HaveAPI::GoClient
26
26
  end
27
27
 
28
28
  def initialize(name, vars)
29
- @_tpl = ERB.new(File.new(HaveAPI::GoClient.tpl(name)).read, 0, '-')
29
+ @_tpl = ERB.new(File.new(HaveAPI::GoClient.tpl(name)).read, trim_mode: '-')
30
30
 
31
31
  vars.each do |k, v|
32
32
  if v.is_a?(Proc)
@@ -36,7 +36,7 @@ module HaveAPI::GoClient
36
36
  if self.module
37
37
  ErbTemplate.render_to_if_changed(
38
38
  'go.mod',
39
- {mod: self.module},
39
+ { mod: self.module },
40
40
  File.join(dst, 'go.mod')
41
41
  )
42
42
 
@@ -44,12 +44,12 @@ module HaveAPI::GoClient
44
44
  FileUtils.mkpath(dst)
45
45
  end
46
46
 
47
- %w(client authentication request response types).each do |v|
47
+ %w[client authentication request response types].each do |v|
48
48
  ErbTemplate.render_to_if_changed(
49
49
  "#{v}.go",
50
50
  {
51
- package: package,
52
- api: api,
51
+ package:,
52
+ api:
53
53
  },
54
54
  File.join(dst, "#{v}.go")
55
55
  )
@@ -60,12 +60,13 @@ module HaveAPI::GoClient
60
60
  end
61
61
 
62
62
  def go_fmt
63
- unless system('go', 'fmt', chdir: dst)
64
- fail "go fmt failed"
65
- end
63
+ return if system('go', 'fmt', chdir: dst)
64
+
65
+ raise 'go fmt failed'
66
66
  end
67
67
 
68
68
  protected
69
+
69
70
  attr_reader :api
70
71
  end
71
72
  end
@@ -37,12 +37,12 @@ module HaveAPI::GoClient
37
37
  @parameters = desc[:parameters].map do |k, v|
38
38
  Parameter.new(role, direction, self, k.to_s, v)
39
39
  end.compact.sort!
40
- @go_type = action.go_type + (prefix ? prefix : '') + direction.to_s.capitalize
40
+ @go_type = action.go_type + (prefix || '') + direction.to_s.capitalize
41
41
  @go_namespace = camelize(desc[:namespace])
42
42
  end
43
43
 
44
44
  def resolve_associations
45
- parameters.each { |p| p.resolve }
45
+ parameters.each(&:resolve)
46
46
  end
47
47
  end
48
48
  end
@@ -46,8 +46,8 @@ module HaveAPI::GoClient
46
46
  object && object.resolve_associations
47
47
  end
48
48
 
49
- %i(global object).each do |type|
50
- %i(input output).each do |dir|
49
+ %i[global object].each do |type|
50
+ %i[input output].each do |dir|
51
51
  define_method(:"has_#{type}_#{dir}?") do
52
52
  t = send(type)
53
53
  next(false) unless t
@@ -1,5 +1,5 @@
1
1
  module HaveAPI::GoClient
2
- module Parameters ; end
2
+ module Parameters; end
3
3
 
4
4
  module Parameter
5
5
  # @param klass [Class]
@@ -16,8 +16,8 @@ module HaveAPI::GoClient
16
16
  # @param desc [Hash]
17
17
  # @return [Parameters::Base, nil]
18
18
  def self.new(role, direction, io, name, desc)
19
- klass, _ =
20
- @handlers.select do |klass, block|
19
+ klass, =
20
+ @handlers.select do |_klass, block|
21
21
  block.call(role, direction, name, desc)
22
22
  end.first
23
23
 
@@ -28,6 +28,7 @@ module HaveAPI::GoClient
28
28
  end
29
29
 
30
30
  protected
31
+
31
32
  def find_resource(path)
32
33
  root = parameter.io.action.resource.api_version
33
34
  path = path.clone
@@ -37,7 +38,7 @@ module HaveAPI::GoClient
37
38
  resource = root.resources.detect { |r| r.name == name }
38
39
 
39
40
  if resource.nil?
40
- fail "associated resource '#{name}' not found in "+
41
+ raise "associated resource '#{name}' not found in " +
41
42
  (root.is_a?(ApiVersion) ? 'root' : root.resource_path.map(&:name).join('.'))
42
43
 
43
44
  elsif path.empty?
@@ -48,7 +49,7 @@ module HaveAPI::GoClient
48
49
  end
49
50
  end
50
51
 
51
- fail 'programming error'
52
+ raise 'programming error'
52
53
  end
53
54
  end
54
55
  end
@@ -64,11 +64,10 @@ module HaveAPI::GoClient
64
64
  end
65
65
 
66
66
  protected
67
+
67
68
  # @return [Hash]
68
69
  attr_reader :desc
69
70
 
70
- def do_resolve
71
-
72
- end
71
+ def do_resolve; end
73
72
  end
74
73
  end
@@ -10,6 +10,7 @@ module HaveAPI::GoClient
10
10
  end
11
11
 
12
12
  protected
13
+
13
14
  def do_resolve
14
15
  @go_in_type = @go_out_type = 'string'
15
16
  end
@@ -2,7 +2,7 @@ require 'haveapi/go_client/parameters/base'
2
2
 
3
3
  module HaveAPI::GoClient
4
4
  class Parameters::Resource < Parameters::Base
5
- handle do |role, direction, name, desc|
5
+ handle do |_role, _direction, _name, desc|
6
6
  desc[:type] == 'Resource'
7
7
  end
8
8
 
@@ -15,6 +15,7 @@ module HaveAPI::GoClient
15
15
  end
16
16
 
17
17
  protected
18
+
18
19
  def do_resolve
19
20
  @association = Parameters::Association.new(self, desc)
20
21
  @go_in_type = 'int64'
@@ -2,11 +2,12 @@ require 'haveapi/go_client/parameters/base'
2
2
 
3
3
  module HaveAPI::GoClient
4
4
  class Parameters::Typed < Parameters::Base
5
- handle do |role, direction, name, desc|
6
- !%w(Custom Resource).include?(desc[:type])
5
+ handle do |_role, _direction, _name, desc|
6
+ !%w[Custom Resource].include?(desc[:type])
7
7
  end
8
8
 
9
9
  protected
10
+
10
11
  def do_resolve
11
12
  @go_in_type = get_go_type(desc[:type])
12
13
  @go_out_type = get_go_type(desc[:type])
@@ -23,7 +24,7 @@ module HaveAPI::GoClient
23
24
  when 'Boolean'
24
25
  'bool'
25
26
  else
26
- fail "unsupported data type '#{v}'"
27
+ raise "unsupported data type '#{v}'"
27
28
  end
28
29
  end
29
30
  end
@@ -48,7 +48,7 @@ module HaveAPI::GoClient
48
48
  Resource.new(self, k, v)
49
49
  end.sort!
50
50
  @actions = desc[:actions].map do |k, v|
51
- Action.new(self, k.to_s, v, prefix: prefix)
51
+ Action.new(self, k.to_s, v, prefix:)
52
52
  end.sort!
53
53
  end
54
54
 
@@ -78,8 +78,8 @@ module HaveAPI::GoClient
78
78
  end
79
79
 
80
80
  def resolve_associations
81
- actions.each { |a| a.resolve_associations }
82
- resources.each { |r| r.resolve_associations }
81
+ actions.each(&:resolve_associations)
82
+ resources.each(&:resolve_associations)
83
83
  end
84
84
 
85
85
  def generate(gen)
@@ -87,7 +87,7 @@ module HaveAPI::GoClient
87
87
  'resource.go',
88
88
  {
89
89
  package: gen.package,
90
- resource: self,
90
+ resource: self
91
91
  },
92
92
  File.join(gen.dst, prefix_underscore("resource_#{full_name}.go"))
93
93
  )
@@ -99,7 +99,7 @@ module HaveAPI::GoClient
99
99
  'action.go',
100
100
  {
101
101
  package: gen.package,
102
- action: a,
102
+ action: a
103
103
  },
104
104
  File.join(gen.dst, prefix_underscore("resource_#{full_name}_action_#{a.name}.go"))
105
105
  )
@@ -111,6 +111,7 @@ module HaveAPI::GoClient
111
111
  end
112
112
 
113
113
  protected
114
+
114
115
  attr_reader :prefix
115
116
 
116
117
  def prefix_underscore(s)
@@ -133,7 +134,7 @@ module HaveAPI::GoClient
133
134
  names = ['Resource']
134
135
  names.concat(parent_resources.map(&:go_name))
135
136
  names << go_name
136
- prefix_camel(names.join(''))
137
+ prefix_camel(names.join)
137
138
  end
138
139
  end
139
140
  end
@@ -4,7 +4,7 @@ module HaveAPI::GoClient
4
4
  # @param v [String]
5
5
  # @return [String]
6
6
  def camelize(v)
7
- v.to_s.split('_').map(&:capitalize).join('')
7
+ v.to_s.split('_').map(&:capitalize).join
8
8
  end
9
9
  end
10
10
  end
@@ -1,5 +1,5 @@
1
1
  module HaveAPI
2
2
  module GoClient
3
- VERSION = '0.19.3'
3
+ VERSION = '0.21.0'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1,53 @@
1
+ package <%= package %>
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ )
7
+
8
+ type OAuth2Auth struct {
9
+ // The authentication token
10
+ AccessToken string
11
+ }
12
+
13
+ func (auth *OAuth2Auth) Authenticate(request *http.Request) {
14
+ request.Header.Set("<%= auth.http_header %>", auth.AccessToken)
15
+ }
16
+
17
+ // SetExistingTokenAuth will use a previously acquired access token
18
+ func (client *Client) SetExistingOAuth2Auth(accessToken string) {
19
+ client.Authentication = &OAuth2Auth{
20
+ AccessToken: accessToken,
21
+ }
22
+ }
23
+
24
+ // RevokeAuthToken will revoke the access token and remove authentication
25
+ // from the client
26
+ func (client *Client) RevokeAccessToken() error {
27
+ httpClient := &http.Client{}
28
+
29
+ req, err := http.NewRequest("POST", "<%= auth.revoke_url %>", nil)
30
+
31
+ if err != nil {
32
+ return err
33
+ }
34
+
35
+ if client.Authentication != nil {
36
+ client.Authentication.Authenticate(req)
37
+ }
38
+
39
+ resp, err := httpClient.Do(req)
40
+
41
+ if err != nil {
42
+ return err
43
+ }
44
+
45
+ defer resp.Body.Close()
46
+
47
+ if resp.StatusCode != 200 {
48
+ return fmt.Errorf("Unable to revoke access token, HTTP %v", resp.StatusCode)
49
+ }
50
+
51
+ client.Authentication = nil
52
+ return nil
53
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haveapi-go-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.3
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Skokan
@@ -10,48 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 1980-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: haveapi-client
43
15
  requirement: !ruby/object:Gem::Requirement
44
16
  requirements:
45
17
  - - "~>"
46
18
  - !ruby/object:Gem::Version
47
- version: 0.19.3
19
+ version: 0.21.0
48
20
  type: :runtime
49
21
  prerelease: false
50
22
  version_requirements: !ruby/object:Gem::Requirement
51
23
  requirements:
52
24
  - - "~>"
53
25
  - !ruby/object:Gem::Version
54
- version: 0.19.3
26
+ version: 0.21.0
55
27
  description: Go client generator
56
28
  email:
57
29
  - jakub.skokan@vpsfree.cz
@@ -71,6 +43,7 @@ files:
71
43
  - lib/haveapi/go_client/api_version.rb
72
44
  - lib/haveapi/go_client/authentication/base.rb
73
45
  - lib/haveapi/go_client/authentication/basic.rb
46
+ - lib/haveapi/go_client/authentication/oauth2.rb
74
47
  - lib/haveapi/go_client/authentication/token.rb
75
48
  - lib/haveapi/go_client/authentication/unsupported.rb
76
49
  - lib/haveapi/go_client/authentication_methods.rb
@@ -92,6 +65,7 @@ files:
92
65
  - template/action.go.erb
93
66
  - template/authentication.go.erb
94
67
  - template/authentication/basic.go.erb
68
+ - template/authentication/oauth2.go.erb
95
69
  - template/authentication/token.go.erb
96
70
  - template/client.go.erb
97
71
  - template/go.mod.erb
@@ -111,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
85
  requirements:
112
86
  - - ">="
113
87
  - !ruby/object:Gem::Version
114
- version: '0'
88
+ version: 3.2.0
115
89
  required_rubygems_version: !ruby/object:Gem::Requirement
116
90
  requirements:
117
91
  - - ">="