# encoding: utf-8 # require 'cgi' require 'net/http' require 'uri' module Mode module Sdk # The Client class provides an interface for common Mode API requests # class Client class << self # Make an HTTP HEAD request with the Mode API # # @param path [String] the request path # @param options [optional, Hash] hash of # {Mode::Sdk::Client::Request#initialize request options} # # @return [Mode::Sdk::Client::Response] the response # def head(path, options = {}) request(Net::HTTP::Head.new(path), nil, options) end # Make an HTTP GET request with the Mode API # # @param path [String] the request path # @param options [optional, Hash] hash of # {Mode::Sdk::Client::Request#initialize request options} # # @return [Mode::Sdk::Client::Response] the response # def get(path, options = {}) request(Net::HTTP::Get.new(path), nil, options) end # Make an HTTP POST request with the Mode API # # The body param may be either: # # * a String containing raw CSV, or # * an IO-like object containing the raw CSV for streaming requests # # @param path [String] the request path # @param body [String, #read] the request body # @param options [optional, Hash] hash of # {Mode::Sdk::Client::Request#initialize request options} # # @return [Mode::Sdk::Client::Response] the response # def post(path, body, options = {}) request(Net::HTTP::Post.new(path), body, options) end # Make an HTTP PUT request with the Mode API # # @param path [String] the request path # @param body [String, #read] the request body # @param options [optional, Hash] hash of # {Mode::Sdk::Client::Request#initialize request options} # # @return [Mode::Sdk::Client::Response] the response # def put(path, body, options = {}) request(Net::HTTP::Put.new(path), body, options) end # Get the Mode API representation of the authenticated account # # @return [Mode::Sdk::Client::Response] the response # def account get('/api/account', expect: [200]).body end # Test authentication credentials # # @return [true, false] whether the provided authentication credentials # are valid # def authenticated? get('/api/account', expect: [200, 401]).code == 200 end private def request(request, body, options = {}) Mode::Sdk::Client::Request.new(request, body, options).response end end end end end require 'mode/sdk/client/request' require 'mode/sdk/client/response'