class Kintone::OAuthApi
Attributes
access_token[R]
@return [OAuth2::AccessToken]
Public Class Methods
new(domain, token, opts = {}, &block)
click to toggle source
@param [String] domain the kintone's domain (e.g. 'foobar.cybozu.com'). @param [String] token the Access Token value. @param [Hash] opts the options to create the Access Token with. @option opts [String] :client_id (nil) the client_id value. @option opts [String] :client_secret (nil) the client_secret value. @option opts [String] :refresh_token (nil) the refresh_token value. @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken will expire. @yield [builder] The Faraday connection builder
# File lib/kintone/oauth_api.rb, line 15 def initialize(domain, token, opts = {}, &block) client_options = { site: "https://#{domain}", token_url: '/oauth2/token', authorize_url: '/oauth2/authorization', connection_build: connection_builder(&block) } client = ::OAuth2::Client.new(opts[:client_id], opts[:client_secret], client_options) @access_token = ::OAuth2::AccessToken.new(client, token, refresh_token: opts[:refresh_token], expires_at: opts[:expires_at]) end
Public Instance Methods
delete(url, body = nil)
click to toggle source
# File lib/kintone/oauth_api.rb, line 47 def delete(url, body = nil) json_body = body.to_json if body.respond_to?(:to_json) opts = request_options(body: json_body) request(:delete, url, opts) end
get(url, params = {})
click to toggle source
# File lib/kintone/oauth_api.rb, line 30 def get(url, params = {}) opts = request_options(params: params, headers: nil) request(:get, url, opts) end
post(url, body)
click to toggle source
# File lib/kintone/oauth_api.rb, line 35 def post(url, body) json_body = body.to_json if body.respond_to?(:to_json) opts = request_options(body: json_body) request(:post, url, opts) end
post_file(url, path, content_type, original_filename)
click to toggle source
# File lib/kintone/oauth_api.rb, line 53 def post_file(url, path, content_type, original_filename) body = { file: Faraday::UploadIO.new(path, content_type, original_filename) } headers = { 'Content-Type' => 'multipart/form-data' } opts = request_options(body: body, headers: headers) res = request(:post, url, opts) res['fileKey'] end
put(url, body)
click to toggle source
# File lib/kintone/oauth_api.rb, line 41 def put(url, body) json_body = body.to_json if body.respond_to?(:to_json) opts = request_options(body: json_body) request(:put, url, opts) end
refresh!()
click to toggle source
# File lib/kintone/oauth_api.rb, line 26 def refresh! @access_token = @access_token.refresh! end
Private Instance Methods
connection_builder(&block)
click to toggle source
# File lib/kintone/oauth_api.rb, line 63 def connection_builder(&block) lambda { |con| con.request :url_encoded con.request :multipart # NOTE: comment out for avoiding following bug at OAuth2 v1.4.4. # In 2.x the bug will be fixed. # refer to https://github.com/oauth-xx/oauth2/pull/380 # con.response :json, content_type: /\bjson$/ block.call(con) if block_given? } end
default_headers()
click to toggle source
# File lib/kintone/oauth_api.rb, line 99 def default_headers { 'Content-Type' => 'application/json' } end
request(verb, url, opts)
click to toggle source
# File lib/kintone/oauth_api.rb, line 75 def request(verb, url, opts) response = @access_token.request(verb, url, opts) validate_response(response) rescue OAuth2::Error => e response = e.response raise Kintone::KintoneError.new(response.body, response.status) end
request_options(params: nil, body: nil, headers: default_headers)
click to toggle source
# File lib/kintone/oauth_api.rb, line 91 def request_options(params: nil, body: nil, headers: default_headers) opts = {} opts[:headers] = headers opts[:params] = params if params opts[:body] = body if body opts end
validate_response(response, expected_status = 200)
click to toggle source
# File lib/kintone/oauth_api.rb, line 83 def validate_response(response, expected_status = 200) if response.status != expected_status raise Kintone::KintoneError.new(response.body, response.status) end JSON.parse(response.body) end