module WorkOS::Client
A Net::HTTP based API client for interacting with the WorkOS
API
Public Instance Methods
client()
click to toggle source
# File lib/workos/client.rb, line 11 def client Net::HTTP.new(WorkOS::API_HOSTNAME, 443).tap do |http_client| http_client.use_ssl = true end end
delete_request(path:, auth: false, params: {})
click to toggle source
# File lib/workos/client.rb, line 77 def delete_request(path:, auth: false, params: {}) uri = URI(path) uri.query = URI.encode_www_form(params) if params request = Net::HTTP::Delete.new( uri.to_s, 'Content-Type' => 'application/json', ) request['Authorization'] = "Bearer #{WorkOS.key!}" if auth request['User-Agent'] = user_agent request end
execute_request(request:)
click to toggle source
# File lib/workos/client.rb, line 22 def execute_request(request:) response = client.request(request) http_status = response.code.to_i handle_error_response(response: response) if http_status >= 400 response end
get_request(path:, auth: false, params: {}, access_token: nil)
click to toggle source
# File lib/workos/client.rb, line 39 def get_request(path:, auth: false, params: {}, access_token: nil) uri = URI(path) uri.query = URI.encode_www_form(params) if params request = Net::HTTP::Get.new( uri.to_s, 'Content-Type' => 'application/json', ) request['Authorization'] = "Bearer #{access_token || WorkOS.key!}" if auth request['User-Agent'] = user_agent request end
handle_error_response(response:)
click to toggle source
# File lib/workos/client.rb, line 122 def handle_error_response(response:) http_status = response.code.to_i json = JSON.parse(response.body) case http_status when 400 raise InvalidRequestError.new( message: json['message'], http_status: http_status, request_id: response['x-request-id'], ) when 401 raise AuthenticationError.new( message: json['message'], http_status: http_status, request_id: response['x-request-id'], ) when 404 raise APIError.new( message: json['message'], http_status: http_status, request_id: response['x-request-id'], ) when 422 message = json['message'] errors = extract_error(json['errors']) if json['errors'] message += " (#{errors})" if errors raise InvalidRequestError.new( message: message, http_status: http_status, request_id: response['x-request-id'], ) end end
post_request(path:, auth: false, idempotency_key: nil, body: nil)
click to toggle source
# File lib/workos/client.rb, line 61 def post_request(path:, auth: false, idempotency_key: nil, body: nil) request = Net::HTTP::Post.new(path, 'Content-Type' => 'application/json') request.body = body.to_json if body request['Authorization'] = "Bearer #{WorkOS.key!}" if auth request['Idempotency-Key'] = idempotency_key if idempotency_key request['User-Agent'] = user_agent request end
put_request(path:, auth: false, idempotency_key: nil, body: nil)
click to toggle source
# File lib/workos/client.rb, line 99 def put_request(path:, auth: false, idempotency_key: nil, body: nil) request = Net::HTTP::Put.new(path, 'Content-Type' => 'application/json') request.body = body.to_json if body request['Authorization'] = "Bearer #{WorkOS.key!}" if auth request['Idempotency-Key'] = idempotency_key if idempotency_key request['User-Agent'] = user_agent request end
user_agent()
click to toggle source
# File lib/workos/client.rb, line 109 def user_agent engine = defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : 'Ruby' [ 'WorkOS', "#{engine}/#{RUBY_VERSION}", RUBY_PLATFORM, "v#{WorkOS::VERSION}" ].join('; ') end
Private Instance Methods
extract_error(errors)
click to toggle source
rubocop:enable Metrics/MethodLength, Metrics/AbcSize
# File lib/workos/client.rb, line 161 def extract_error(errors) errors.map do |error| "#{error['field']}: #{error['code']}" end.join('; ') end