Module: WorkOS::Client
- Includes:
- Kernel
- Included in:
- AuditLogs, DirectorySync, Events, MFA, Organizations, Passwordless, Portal, SSO, UserManagement, Widgets
- Defined in:
- lib/workos/client.rb
Overview
A Net::HTTP based API client for interacting with the WorkOS API
Instance Method Summary collapse
- #client ⇒ Object
- #delete_request(path:, auth: false, params: {}) ⇒ Object
- #execute_request(request:) ⇒ Object
- #get_request(path:, auth: false, params: {}, access_token: nil) ⇒ Object
- 
  
    
      #handle_error_response(response:)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity:. 
- #post_request(path:, auth: false, idempotency_key: nil, body: nil) ⇒ Object
- #put_request(path:, auth: false, idempotency_key: nil, body: nil) ⇒ Object
- #user_agent ⇒ Object
Instance Method Details
#client ⇒ Object
| 8 9 10 11 12 13 14 15 | # File 'lib/workos/client.rb', line 8 def client Net::HTTP.new(WorkOS.config.api_hostname, 443).tap do |http_client| http_client.use_ssl = true http_client.open_timeout = WorkOS.config.timeout http_client.read_timeout = WorkOS.config.timeout http_client.write_timeout = WorkOS.config.timeout if RUBY_VERSION >= '2.6.0' end end | 
#delete_request(path:, auth: false, params: {}) ⇒ Object
| 55 56 57 58 59 60 61 62 63 64 65 66 67 | # File 'lib/workos/client.rb', line 55 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.config.key!}" if auth request['User-Agent'] = user_agent request end | 
#execute_request(request:) ⇒ Object
| 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # File 'lib/workos/client.rb', line 17 def execute_request(request:) begin response = client.request(request) rescue Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout raise TimeoutError.new( message: 'API Timeout Error', ) end 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) ⇒ Object
| 32 33 34 35 36 37 38 39 40 41 42 43 44 | # File 'lib/workos/client.rb', line 32 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.config.key!}" if auth request['User-Agent'] = user_agent request end | 
#handle_error_response(response:) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity:
| 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | # File 'lib/workos/client.rb', line 90 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'], code: json['code'], errors: json['errors'], error: json['error'], error_description: json['error_description'], data: json, ) when 401 raise AuthenticationError.new( message: json['message'], http_status: http_status, request_id: response['x-request-id'], ) when 403 raise ForbiddenRequestError.new( message: json['message'], http_status: http_status, request_id: response['x-request-id'], code: json['code'], data: json, ) when 404 raise NotFoundError.new( message: json['message'], http_status: http_status, request_id: response['x-request-id'], ) when 422 = json['message'] code = json['code'] errors = extract_error(json['errors']) if json['errors'] += " (#{errors})" if errors raise UnprocessableEntityError.new( message: , http_status: http_status, request_id: response['x-request-id'], error: json['error'], errors: errors, code: code, ) when 429 raise RateLimitExceededError.new( message: json['message'], http_status: http_status, request_id: response['x-request-id'], retry_after: response['Retry-After'], ) else raise APIError.new( message: json['message'], http_status: http_status, request_id: response['x-request-id'], ) end end | 
#post_request(path:, auth: false, idempotency_key: nil, body: nil) ⇒ Object
| 46 47 48 49 50 51 52 53 | # File 'lib/workos/client.rb', line 46 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.config.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) ⇒ Object
| 69 70 71 72 73 74 75 76 | # File 'lib/workos/client.rb', line 69 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.config.key!}" if auth request['Idempotency-Key'] = idempotency_key if idempotency_key request['User-Agent'] = user_agent request end |