lib/backlog_kit/client.rb in backlog_kit-0.11.0 vs lib/backlog_kit/client.rb in backlog_kit-0.12.0
- old
+ new
@@ -1,8 +1,9 @@
require 'backlog_kit/error'
require 'backlog_kit/response'
require 'backlog_kit/version'
+require 'backlog_kit/client/authorization'
require 'backlog_kit/client/git'
require 'backlog_kit/client/group'
require 'backlog_kit/client/issue'
require 'backlog_kit/client/notification'
require 'backlog_kit/client/priority'
@@ -17,10 +18,11 @@
require 'backlog_kit/response/raise_error'
require 'backlog_kit/hash_extensions'
module BacklogKit
class Client
+ include BacklogKit::Client::Authorization
include BacklogKit::Client::Git
include BacklogKit::Client::Group
include BacklogKit::Client::Issue
include BacklogKit::Client::Notification
include BacklogKit::Client::Priority
@@ -32,21 +34,40 @@
include BacklogKit::Client::User
include BacklogKit::Client::Wiki
USER_AGENT = "BacklogKit Ruby Gem #{BacklogKit::VERSION}".freeze
- attr_accessor(:space_id, :api_key)
+ attr_accessor(
+ :space_id,
+ :api_key,
+ :client_id,
+ :client_secret,
+ :refresh_token,
+ :redirect_uri,
+ :state,
+ :access_token
+ )
def initialize(options = {})
- @space_id = ENV['BACKLOG_SPACE_ID']
- @api_key = ENV['BACKLOG_API_KEY']
+ @space_id = ENV['BACKLOG_SPACE_ID']
+ @api_key = ENV['BACKLOG_API_KEY']
+ @client_id = ENV['BACKLOG_OAUTH_CLIENT_ID']
+ @client_secret = ENV['BACKLOG_OAUTH_CLIENT_SECRET']
+ @refresh_token = ENV['BACKLOG_OAUTH_REFRESH_TOKEN']
options.each do |key, value|
instance_variable_set(:"@#{key}", value)
end
end
+ def authorization_url
+ url = "#{host}/OAuth2AccessRequest.action?response_type=code&client_id=#{@client_id}"
+ url += "&redirect_uri=#{URI.escape(@redirect_uri)}" if @redirect_uri
+ url += "&state=#{@state}" if @state
+ url
+ end
+
def get(path, params = {})
request(:get, path, params)
end
def post(path, params = {})
@@ -65,12 +86,12 @@
request(:delete, path, params)
end
private
- def request(method, path, params = {})
- params.camelize_keys!
+ def request(method, path, params = {}, raw_params = false)
+ params.camelize_keys! unless raw_params
faraday_response = connection.send(method, request_path(path), params)
BacklogKit::Response.new(faraday_response)
rescue Faraday::ConnectionFailed => e
raise BacklogKit::Error, "#{BacklogKit::ConnectionError.name.demodulize} - #{e.message}"
end
@@ -89,13 +110,21 @@
def host
"https://#{space_id}.backlog.jp"
end
def request_headers
- { 'User-Agent' => USER_AGENT }
+ headers = { 'User-Agent' => USER_AGENT }
+ headers.merge!('Authorization' => "Bearer #{@access_token}") if oauth_request?
+ headers
end
+ def oauth_request?
+ !@api_key && @access_token
+ end
+
def request_path(path)
- "/api/v2/#{URI.escape(path)}?apiKey=#{URI.escape(@api_key.to_s)}"
+ path = "/api/v2/#{URI.escape(path)}"
+ path += "?apiKey=#{URI.escape(@api_key.to_s)}" if @api_key
+ path
end
end
end