class BacklogKit::Client
Client
for the Backlog API
Constants
- USER_AGENT
Attributes
Public Class Methods
Initialize a new Client
object with given options
@param options [Hash] Initialize options @option options [String] :space_id Backlog space id @option options [String] :second_level_domain Backlog second level domain @option options [String] :top_level_domain Backlog top level domain @option options [String] :api_key Backlog api key @option options [String] :client_id Backlog OAuth client id @option options [String] :client_secret Backlog OAuth client secret @option options [String] :refresh_token Backlog OAuth refresh token
# File lib/backlog_kit/client.rb, line 69 def initialize(options = {}) @space_id = ENV['BACKLOG_SPACE_ID'] @second_level_domain = ENV['BACKLOG_SECOND_LEVEL_DOMAIN'] || 'backlog' @top_level_domain = ENV['BACKLOG_TOP_LEVEL_DOMAIN'] || 'com' @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
Public Instance Methods
Make a HTTP DELETE request
@param path [String] Path for request @param params [Hash] Request parameters @return [BacklogKit::Response] Response
from API server
# File lib/backlog_kit/client.rb, line 134 def delete(path, params = {}) request(:delete, path, params) end
Make a HTTP GET request
@param path [String] Path for request @param params [Hash] Request parameters @return [BacklogKit::Response] Response
from API server
# File lib/backlog_kit/client.rb, line 98 def get(path, params = {}) request(:get, path, params) end
Make a HTTP PATCH request
@param path [String] Path for request @param params [Hash] Request parameters @return [BacklogKit::Response] Response
from API server
# File lib/backlog_kit/client.rb, line 125 def patch(path, params = {}) request(:patch, path, params) end
Make a HTTP POST request
@param path [String] Path for request @param params [Hash] Request parameters @return [BacklogKit::Response] Response
from API server
# File lib/backlog_kit/client.rb, line 107 def post(path, params = {}) request(:post, path, params) end
Make a HTTP PUT request
@param path [String] Path for request @param params [Hash] Request parameters @return [BacklogKit::Response] Response
from API server
# File lib/backlog_kit/client.rb, line 116 def put(path, params = {}) request(:put, path, params) end
Private Instance Methods
# File lib/backlog_kit/client.rb, line 148 def connection Faraday.new(url: host, headers: request_headers) do |faraday| faraday.request(:multipart) faraday.request(:url_encoded) faraday.response(:json, content_type: /application\/json/) faraday.response(:file_parser) faraday.response(:error) faraday.adapter(Faraday.default_adapter) end end
# File lib/backlog_kit/client.rb, line 159 def host "https://#{space_id}.#{second_level_domain}.#{top_level_domain}" end
# File lib/backlog_kit/client.rb, line 169 def oauth_request? !@api_key && @access_token end
# File lib/backlog_kit/client.rb, line 140 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
# File lib/backlog_kit/client.rb, line 163 def request_headers headers = { 'User-Agent' => USER_AGENT } headers['Authorization'] = "Bearer #{@access_token}" if oauth_request? headers end
# File lib/backlog_kit/client.rb, line 173 def request_path(path) path = "/api/v2/#{URI.escape(path)}" path += "?apiKey=#{URI.escape(@api_key.to_s)}" if @api_key path end