Module: WorkOS::Client
Overview
A Net::HTTP based API client for interacting with the WorkOS API
Instance Method Summary collapse
- #client ⇒ Object
- #execute_request(request:) ⇒ Object
- #get_request(path:, auth: false, params: {}) ⇒ Object
- #handle_error_response(response:) ⇒ Object
- #post_request(path:, auth: false, idempotency_key: nil, body: nil) ⇒ Object
- #user_agent ⇒ Object
Instance Method Details
#client ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/workos/client.rb', line 11 def client return @client if defined?(@client) @client = Net::HTTP.new(WorkOS::API_HOSTNAME, 443) @client.use_ssl = true @client end |
#execute_request(request:) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/workos/client.rb', line 25 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: {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/workos/client.rb', line 41 def get_request(path:, auth: false, params: {}) 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 #{WorkOS.key!}" if auth request['User-Agent'] = user_agent request end |
#handle_error_response(response:) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/workos/client.rb', line 87 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 errors = json['errors'].map do |error| "#{error['field']}: #{error['code']}" end.join('; ') = "#{json['message']} (#{errors})" raise InvalidRequestError.new( message: , http_status: http_status, request_id: response['x-request-id'], ) end end |
#post_request(path:, auth: false, idempotency_key: nil, body: nil) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/workos/client.rb', line 63 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 |
#user_agent ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/workos/client.rb', line 73 def user_agent engine = defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : 'Ruby' [ 'WorkOS', "#{engine}/#{RUBY_VERSION}", RUBY_PLATFORM, "v#{WorkOS::VERSION}" ].join('; ') end |