triton-internal 0.1.3 → 0.2.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
  SHA1:
3
- metadata.gz: f2912604c505dbd1db3661a1b84e6134e2305a71
4
- data.tar.gz: 12177d339bf2a21df7fe071db9efa54dd31c9d4d
3
+ metadata.gz: b16b7be1a4301ff1ce59645470043e7f28aad14d
4
+ data.tar.gz: 9412ab5e44e030236f71089ae01302ee2f36ac0f
5
5
  SHA512:
6
- metadata.gz: 96f7c458feaa86cc933ac6ad3802637ed149f73876d55a0383ef1b70e7f6cbf3c417aa3b4524a6b384ab049714f2612228680b07b0314da50bcc8c5442c1486d
7
- data.tar.gz: 76865f0adc315e88aeb92ad88cdc49927dc9946754d7864f8b9aaf2151806a7b71259abc7dc5ff5520c5b8a219f127cec04b1b58d8bc9f2224e187cef5df4487
6
+ metadata.gz: 9411f1c5c59af68ae14401e73ccc5fa91bec0472bc9fba707d30662c13f014580d7e05cfe9d440c0bbcadfa43659545096bf942345e56c8fd7a5e62808f4f77c
7
+ data.tar.gz: fb7c1d776e78832fa0e85a47a47ae31b96b082494901482868abd74862be61ebd6c518940141b9919be70e8bf96443d843fd546dbb0124c4360942c5e8742293
@@ -47,12 +47,34 @@ module Triton
47
47
  end
48
48
  end
49
49
 
50
+
51
+ attr_reader :path
52
+
50
53
  def initialize(name, params={})
51
54
  @name = name
55
+ @params = params.dup || {}
52
56
  @call = DefinedMethods[self.class][name]
53
- @params = params
54
-
55
57
  raise UnknownCall, "#{@name} isn't a known API method." unless @call
58
+
59
+ @method = @call.fetch(:method, :get).intern
60
+ @path = @call[:path]
61
+ @body_param = @call[:body_param] && @params.delete(@call[:body_param])
62
+
63
+ @params.each do |k,v|
64
+ @path = @path.gsub(":#{k.to_s}") do |_|
65
+ @params.delete(k)
66
+ CGI.escape(v.to_s)
67
+ end
68
+ end
69
+
70
+ @query_string_params = if @method == :get || !!@body_param
71
+ @params
72
+ else
73
+ [@call.fetch(:querystring, [])].flatten.map do |key|
74
+ value = @params.delete(key.to_s)
75
+ [key, value] if value
76
+ end.compact
77
+ end
56
78
  end
57
79
 
58
80
  def execute
@@ -60,7 +82,7 @@ module Triton
60
82
  raise Triton::TestModeLeak, "Request '#{@name}' leaked when in test mode\n#{request.method.upcase} #{URI.parse(request.url).path}\n#{JSON.pretty_generate(@params)}"
61
83
  end
62
84
  Triton.logger.debug("#{self.class.name}/#{@name}: #{request.method.upcase} #{URI.parse(request.url).path}\n#{JSON.pretty_generate(@params)}")
63
- object = JSON.parse(request.execute)
85
+ object = parse(request.execute)
64
86
  if object.is_a?(Hash)
65
87
  IndifferentHash.new.merge!(object)
66
88
  else
@@ -75,37 +97,47 @@ module Triton
75
97
  end
76
98
  end
77
99
 
78
- def request
79
- @request ||= begin
80
- request_method = @call.fetch(:method, :get).intern
81
- path = @call[:path]
82
-
83
- @params.each do |k,v|
84
- path = path.gsub(":#{k.to_s}") do |_|
85
- @params.delete(k)
86
- CGI.escape(v.to_s)
87
- end
100
+ def parse(body)
101
+ parser = @call.fetch(:response, :json)
102
+ if parser.respond_to?(:call)
103
+ parser.call(body)
104
+ else
105
+ case parser
106
+ when :json_lines
107
+ body.split("\n").map { |a| JSON.parse(a) }
108
+ when :json
109
+ JSON.parse(body)
110
+ else
111
+ raise ArgumentError, "Unknown parser '#{parser}'"
88
112
  end
113
+ end
114
+ end
89
115
 
90
- body_param = @call[:body_param] && @params.delete(@call[:body_param])
116
+ def request
117
+ @request ||= begin
91
118
 
92
- if request_method == :get || !!body_param
93
- payload = !!body_param && JSON.generate(body_param)
94
- query = @params.map { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")
119
+ # Now generate the payload, unless it's a GET
120
+ payload = if @method == :get
121
+ nil
122
+ elsif !!@body_param
123
+ JSON.generate(@body_param)
95
124
  else
96
- query = nil
97
- payload = JSON.dump(@params)
125
+ JSON.generate(@params)
98
126
  end
99
127
 
128
+ query = @query_string_params.map { |k,v| [CGI.escape(k.to_s), CGI.escape(v.to_s)].join("=")}.join("&")
129
+
100
130
  url = URI.parse("http://#{self.class.hostname}#{path}")
101
- url.query = if url.query
102
- "#{url.query}&#{query}"
103
- else
104
- query
131
+ if query.length > 0
132
+ url.query = if url.query
133
+ url.query = "#{url.query}&#{query}"
134
+ else
135
+ query
136
+ end
105
137
  end
106
138
 
107
139
  RestClient::Request.new({
108
- :method => request_method,
140
+ :method => @method,
109
141
  :url => url.to_s,
110
142
  :headers => {
111
143
  'Accept' => 'application/json',
@@ -26,7 +26,7 @@ module Triton
26
26
  call('UpdateImage', :method => :post, :path => '/images/:uuid?action=update')
27
27
  call('AdminImportImage', :method => :post, :path => '/images/:uuid?action=import')
28
28
  call('AdminImportRemoteImage', :method => :post, :path => '/images/:uuid?action=import-remote')
29
- call('AdminImportDockerImage', :method => :post, :path => '/images?action=import-docker-image')
29
+ call('AdminImportDockerImage', :method => :post, :path => '/images?action=import-docker-image', :querystring => ['tag', 'repo', 'digest'], :response => :json_lines)
30
30
  call('AdminChangeImageStor', :method => :post, :path => '/images/:uuid?action=change-stor&stor=:newstor')
31
31
  call('ListImageJobs', :method => :get, :path => '/images/:uuid/jobs')
32
32
  call('ListChannels', :method => :get, :path => '/channels')
@@ -1,3 +1,3 @@
1
1
  module Triton
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: triton-internal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Haggett
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-14 00:00:00.000000000 Z
11
+ date: 2017-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  version: '0'
122
122
  requirements: []
123
123
  rubyforge_project:
124
- rubygems_version: 2.5.1
124
+ rubygems_version: 2.6.13
125
125
  signing_key:
126
126
  specification_version: 4
127
127
  summary: Library to wrap the Triton internal APIs