Class: ProxyCheapClient::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_cheap_client/models/base.rb

Direct Known Subclasses

Balance, Client, Configuration, Order, Proxy

Instance Method Summary collapse

Instance Method Details

#extract_error_body(error) ⇒ Object

request



55
56
57
# File 'lib/proxy_cheap_client/models/base.rb', line 55

def extract_error_body(error)
  error.response&.dig(:body)
end

#extract_error_message(error) ⇒ Object

extract_error_body



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/proxy_cheap_client/models/base.rb', line 59

def extract_error_message(error)
  body = extract_error_body(error)
  return error.message unless body

  begin
    parsed = JSON.parse(body)
    parsed["message"] || parsed["error"] || body
  rescue JSON::ParserError
    body
  end
end

#parse_response(response) ⇒ Object

extract_error_message



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/proxy_cheap_client/models/base.rb', line 71

def parse_response(response)
  return {} if response.body.nil? || response.body.empty?

  begin
    data = JSON.parse(response.body)
  rescue JSON::ParserError
    raise InvalidResponseError, "Response is not valid JSON"
  end

  if response.status >= 400
    raise RequestError, "API error: #{data}"
  end

  data
end

#request(method, path, body = nil) ⇒ Object

Note:

Some API endpoints (whitelist, extend-period, buy-bandwidth) use GET requests with JSON body, which is non-standard but matches the official API behavior.

Make an HTTP request to the API.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/proxy_cheap_client/models/base.rb', line 6

def request(method, path, body = nil)
  response = case method.to_s.downcase.to_sym
             when :get
               if body
                 @conn.get(path) { |r| r.body = body.to_json }
               else
                 @conn.get(path)
               end
             when :post
               @conn.post(path) { |r| r.body = body.to_json if body }
             when :patch
               @conn.patch(path) { |r| r.body = body.to_json if body }
             else
               raise RequestError, "Unsupported HTTP method: #{method}"
             end

  parse_response(response)
rescue Faraday::UnauthorizedError => e
  raise AuthenticationError.new(
    "Unauthorized - check API key and secret",
    status_code: 401,
    response_body: extract_error_body(e)
  )
rescue Faraday::ResourceNotFound => e
  raise NotFoundError.new(
    "Resource not found: #{path}",
    status_code: 404,
    response_body: extract_error_body(e)
  )
rescue Faraday::BadRequestError => e
  raise BadRequestError.new(
    "Bad request: #{extract_error_message(e)}",
    status_code: 400,
    response_body: extract_error_body(e)
  )
rescue Faraday::ServerError => e
  raise ServerError.new(
    "Server error: #{extract_error_message(e)}",
    status_code: e.response&.dig(:status) || 500,
    response_body: extract_error_body(e)
  )
rescue Faraday::ClientError => e
  raise RequestError.new(
    "API request failed: #{extract_error_message(e)}",
    status_code: e.response&.dig(:status),
    response_body: extract_error_body(e)
  )
end