rally-wsapi 0.0.6 → 0.1.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
  SHA1:
3
- metadata.gz: be4c514397f117ea3342afc604396b06566f4b85
4
- data.tar.gz: 24a2a62eda3acfc4f07987a3b82e5886b4e4a340
3
+ metadata.gz: e8fcd6dfc98a371448591ac323e980f16b38220b
4
+ data.tar.gz: 16fde94fe2cd3776b90ca3150f92cbc147634e65
5
5
  SHA512:
6
- metadata.gz: c9e2349dcf01af9ab1f30d6aa53985ac261393ba7666c0416f74139c26485d44c87a6cc874cb36f4175fc874e8bdac15c91bfe06fcd5e9579557acf30f41c1f2
7
- data.tar.gz: e4adcad4ba557ed70b8af29009115af83a29b4e3c7036d27cb6efceab2402ca734e454f7265c8d9696263da0581d83fe0fef5e2f28c124e37a6b0df74890cd0d
6
+ metadata.gz: df67aba1e91f9e1e650ca5d5304d6d9c16da733471df6e0f4bebc26d4637502a84da570f1cb754c7c67befdeb52ff277a690cf9c2034b336df8c5439fcc27d0a
7
+ data.tar.gz: 51a297733c01698f72bb1aacd44f7385f7577683b304f0b7fca522e19870e364e039dbe3397ebacb237aee610fbc50e356e7a0a51526ad574670a51a42f09ffd
data/Gemfile.lock CHANGED
@@ -35,7 +35,7 @@ GEM
35
35
  rdoc
36
36
  json (1.8.1)
37
37
  jwt (1.2.0)
38
- mini_portile (0.6.2)
38
+ mini_portile (0.6.1)
39
39
  multi_json (1.10.1)
40
40
  multi_xml (0.5.5)
41
41
  multipart-post (2.0.0)
@@ -47,7 +47,7 @@ GEM
47
47
  multi_json (~> 1.3)
48
48
  multi_xml (~> 0.5)
49
49
  rack (~> 1.2)
50
- rack (1.6.0)
50
+ rack (1.5.2)
51
51
  rake (10.4.2)
52
52
  rdoc (4.2.0)
53
53
  json (~> 1.4)
data/README.md CHANGED
@@ -14,7 +14,15 @@ s = Wsapi::Session.new("deadbeefdeadbeef")
14
14
  The constructor also accepts the following options:
15
15
  * `:workspace_id`, if not given, user's default workspace is used for queries
16
16
  * `:version`, WSAPI version, default is `3.0`
17
+ * `oauth2`, see: (using WSAPI with OAuth2)[https://github.com/flowdock/rally-wsapi#using-wsapi-with-oauth2]
17
18
 
19
+ ### Using WSAPI with OAuth2
20
+
21
+ Authenticating to WSAPI is also possible with OAuth2. Access token used in
22
+ requests is given in place of the API token. Refresh token can be supplied with
23
+ `Wsapi::Session#.setup_refresh_token(client_id, client_secret, refresh_token)`
24
+ method. It takes optional block parameter, which is called every time the
25
+ access token is refreshed.
18
26
 
19
27
  ## Method reference for Session
20
28
 
@@ -81,6 +89,13 @@ get_editors(project_id, opts = {})
81
89
  update_artifact(artifact_type, artifact_id, parameters)
82
90
  ```
83
91
 
92
+ ### Setup refresh token to be used with OAuth2
93
+ ```
94
+ setup_refresh_token(client_id, client_secret, refresh_token, &block)
95
+ ```
96
+
97
+ See (Using WSAPI with OAuth2)[https://github.com/flowdock/rally-wsapi#using-wsapi-with-oauth2] for more info.
98
+
84
99
  ## Result objects
85
100
 
86
101
  There's a couple of convenience classes for the following object types:
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ Jeweler::Tasks.new do |s|
6
6
  s.email = "antti@flowdock.com"
7
7
  s.homepage = "http://github.com/flowdock/rally-wsapi"
8
8
  s.description = "Simple client for Rally WSAPI"
9
- s.authors = ["Antti Pitkänen"]
9
+ s.authors = ["Antti Pitkänen", "Oskari Virtanen"]
10
10
  s.files = FileList["[A-Z]*", "{bin,generators,lib,test}/**/*"]
11
11
  s.licenses = ["MIT"]
12
12
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.1.0
data/lib/wsapi/session.rb CHANGED
@@ -4,7 +4,6 @@ require 'faraday_middleware'
4
4
  require 'excon'
5
5
 
6
6
  require_relative './mapper'
7
-
8
7
  module Wsapi
9
8
  class StandardErrorWithResponse < StandardError
10
9
  attr_reader :response
@@ -19,6 +18,7 @@ module Wsapi
19
18
  class IpAddressLimited < StandardErrorWithResponse; end
20
19
 
21
20
  WSAPI_URL = ENV['WSAPI_URL'] || 'https://rally1.rallydev.com/slm/webservice/'
21
+ AUTH_URL = ENV['RALLY_AUTHENTICATION_URL'] || "https://rally1.rallydev.com/login/oauth2/token"
22
22
 
23
23
  class WsapiAuthentication < Faraday::Middleware
24
24
  def initialize(logger, session_id)
@@ -37,13 +37,17 @@ module Wsapi
37
37
 
38
38
  def initialize(session_id, opts = {})
39
39
  @api_version = opts[:version] || "3.0"
40
- @session_id = session_id
41
40
  @workspace_id = opts[:workspace_id]
42
- @conn = Faraday.new(ssl: {version: :TLSv1}) do |faraday|
43
- faraday.request :json
44
- faraday.use WsapiAuthentication, @session_id
45
- faraday.adapter :excon
46
- end
41
+ @conn = connection(session_id)
42
+ end
43
+
44
+ def setup_refresh_token(client_id, client_secret, refresh_token, &block)
45
+ @oauth2 = {
46
+ client_id: client_id,
47
+ client_secret: client_secret,
48
+ refresh_token: refresh_token,
49
+ refresh_token_updated: block
50
+ }
47
51
  end
48
52
 
49
53
  def get_user_subscription
@@ -106,6 +110,14 @@ module Wsapi
106
110
 
107
111
  private
108
112
 
113
+ def connection(session_id)
114
+ Faraday.new(ssl: {version: :TLSv1}) do |faraday|
115
+ faraday.request :json
116
+ faraday.use WsapiAuthentication, session_id
117
+ faraday.adapter :excon
118
+ end
119
+ end
120
+
109
121
  def workspace_url
110
122
  wsapi_resource_url("Workspace/#{@workspace_id}")
111
123
  end
@@ -115,22 +127,62 @@ module Wsapi
115
127
  end
116
128
 
117
129
  def wsapi_post(url, opts = {})
118
- response = @conn.post url, opts
130
+ response = wsapi_request_with_refresh_token(:post, url, opts)
119
131
  check_response_for_errors!(response)
120
132
 
121
133
  response
122
134
  end
123
135
 
124
136
  def wsapi_get(url, opts = {})
125
- response = @conn.get do |req|
126
- req.url url
127
- req.params['workspace'] = workspace_url if @workspace_id
128
- req.params['query'] = opts[:query] if opts[:query]
129
- req.params['start'] = opts[:start] || 1
130
- req.params['pagesize'] = opts[:pagesize] || 200
131
- req.params['fetch'] = opts[:fetch] || true # by default, fetch full objects
137
+ request_options = {}
138
+ request_options['workspace'] = workspace_url if @workspace_id
139
+ request_options['query'] = opts[:query] if opts[:query]
140
+ request_options['start'] = opts[:start] || 1
141
+ request_options['pagesize'] = opts[:pagesize] || 200
142
+ request_options['fetch'] = opts[:fetch] || true # by default, fetch full objects
143
+
144
+ response = wsapi_request_with_refresh_token(:get, url, request_options)
145
+ check_response_for_errors!(response)
146
+
147
+ response
148
+ end
149
+
150
+ def wsapi_request_with_refresh_token(method, url, opts = {})
151
+ response = @conn.send(method, url, opts)
152
+ if defined?(@oauth2) && response.status == 401
153
+ refresh_token_response = refresh_token!
154
+
155
+ access_token = MultiJson.load(refresh_token_response.body)["access_token"]
156
+ @conn = connection(access_token)
157
+ response = @conn.send(method, url, opts) # Try again with fresh token
158
+ end
159
+
160
+ response
161
+ end
162
+
163
+ def refresh_token!
164
+ client = Faraday.new(ssl: {version: :TLSv1}) do |faraday|
165
+ faraday.request :url_encoded
166
+ faraday.adapter :excon
132
167
  end
168
+
169
+ refresh_params = {
170
+ grant_type: "refresh_token",
171
+ refresh_token: @oauth2[:refresh_token],
172
+ client_id: @oauth2[:client_id],
173
+ client_secret: @oauth2[:client_secret]
174
+ }
175
+
176
+ response = client.post AUTH_URL, refresh_params
177
+
133
178
  check_response_for_errors!(response)
179
+
180
+ response_body = MultiJson.load(response.body)
181
+
182
+ @oauth2[:refresh_token] = response_body["refresh_token"]
183
+ if @oauth2[:refresh_token_updated]
184
+ @oauth2[:refresh_token_updated].call(MultiJson.load(response.body))
185
+ end
134
186
  response
135
187
  end
136
188
 
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rally-wsapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antti Pitkänen
8
+ - Oskari Virtanen
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-01-05 00:00:00.000000000 Z
12
+ date: 2015-01-13 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
@@ -121,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
122
  version: '0'
122
123
  requirements: []
123
124
  rubyforge_project:
124
- rubygems_version: 2.4.5
125
+ rubygems_version: 2.4.3
125
126
  signing_key:
126
127
  specification_version: 4
127
128
  summary: Simple client for Rally WSAPI