class DiscourseApi::Client
Constants
- DEFAULT_TIMEOUT
Attributes
api_key[RW]
api_username[R]
basic_auth[RW]
host[R]
timeout[R]
Public Class Methods
new(host, api_key = nil, api_username = nil)
click to toggle source
# File lib/discourse_api/client.rb, line 58 def initialize(host, api_key = nil, api_username = nil) raise ArgumentError, "host needs to be defined" if host.nil? || host.empty? @host = host @api_key = api_key @api_username = api_username @use_relative = check_subdirectory(host) end
Public Instance Methods
api_username=(api_username)
click to toggle source
# File lib/discourse_api/client.rb, line 71 def api_username=(api_username) @api_username = api_username @connection.headers["Api-Username"] = api_username unless @connection.nil? end
connection_options()
click to toggle source
# File lib/discourse_api/client.rb, line 76 def connection_options @connection_options ||= { url: @host, request: { timeout: @timeout || DEFAULT_TIMEOUT, }, headers: { accept: "application/json", user_agent: user_agent, }, } end
delete(path, params = {})
click to toggle source
# File lib/discourse_api/client.rb, line 93 def delete(path, params = {}) request(:delete, path, params) end
deprecated(old, new)
click to toggle source
# File lib/discourse_api/client.rb, line 123 def deprecated(old, new) warn "[DEPRECATED]: `#{old}` is deprecated. Please use `#{new}` instead." end
get(path, params = {})
click to toggle source
# File lib/discourse_api/client.rb, line 97 def get(path, params = {}) request(:get, path, params) end
patch(path, params = {})
click to toggle source
# File lib/discourse_api/client.rb, line 115 def patch(path, params = {}) request(:patch, path, params) end
post(path, params = {})
click to toggle source
# File lib/discourse_api/client.rb, line 101 def post(path, params = {}) response = request(:post, path, params) case response.status when 200, 201, 204 response.body else raise DiscourseApi::Error, response.body end end
put(path, params = {})
click to toggle source
# File lib/discourse_api/client.rb, line 111 def put(path, params = {}) request(:put, path, params) end
ssl(options)
click to toggle source
# File lib/discourse_api/client.rb, line 89 def ssl(options) connection_options[:ssl] = options end
timeout=(timeout)
click to toggle source
# File lib/discourse_api/client.rb, line 66 def timeout=(timeout) @timeout = timeout @connection.options.timeout = timeout if @connection end
user_agent()
click to toggle source
# File lib/discourse_api/client.rb, line 119 def user_agent @user_agent ||= "DiscourseAPI Ruby Gem #{DiscourseApi::VERSION}" end
Private Instance Methods
check_subdirectory(host)
click to toggle source
# File lib/discourse_api/client.rb, line 192 def check_subdirectory(host) URI(host).request_uri != "/" end
connection()
click to toggle source
# File lib/discourse_api/client.rb, line 129 def connection @connection ||= Faraday.new connection_options do |conn| # Allow uploading of files conn.request :multipart # Convert request params to "www-form-encoded" conn.request :url_encoded # Allow to interact with forums behind basic HTTP authentication if basic_auth conn.request :authorization, :basic, basic_auth[:user], basic_auth[:password] end # Follow redirects conn.response :follow_redirects, limit: 5 # Parse responses as JSON conn.response :json, content_type: "application/json" # For HTTP debugging, uncomment # conn.response :logger # Use Faraday's default HTTP adapter conn.adapter Faraday.default_adapter # Pass api_key and api_username on every request unless api_username.nil? conn.headers["Api-Key"] = api_key conn.headers["Api-Username"] = api_username end end end
handle_error(response)
click to toggle source
# File lib/discourse_api/client.rb, line 177 def handle_error(response) case response.status when 403 raise DiscourseApi::UnauthenticatedError.new(response.env[:body], response.env) when 404, 410 raise DiscourseApi::NotFoundError.new(response.env[:body], response.env) when 422 raise DiscourseApi::UnprocessableEntity.new(response.env[:body], response.env) when 429 raise DiscourseApi::TooManyRequests.new(response.env[:body], response.env) when 500...600 raise DiscourseApi::Error.new(response.env[:body]) end end
request(method, path, params = {})
click to toggle source
# File lib/discourse_api/client.rb, line 163 def request(method, path, params = {}) unless Hash === params params = params.to_h if params.respond_to? :to_h end path = @use_relative ? path.sub(%r{^/}, "") : path response = connection.send(method.to_sym, path, params) handle_error(response) response.env rescue Faraday::ClientError, JSON::ParserError raise DiscourseApi::Error rescue Faraday::ConnectionFailed raise DiscourseApi::Timeout end