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

Constant Summary collapse

@@conn =

Define a static class attribute conn

nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_error_body(error) ⇒ Object



71
72
73
# File 'lib/proxy_cheap_client/models/base.rb', line 71

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

.extract_error_message(error) ⇒ Object

self.extract_error_body



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

def self.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

self.extract_error_message



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/proxy_cheap_client/models/base.rb', line 87

def self.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.



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
54
55
56
57
58
59
60
# File 'lib/proxy_cheap_client/models/base.rb', line 13

def self.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

Instance Method Details

#connObject



6
7
8
# File 'lib/proxy_cheap_client/models/base.rb', line 6

def conn
  @@conn
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.



65
66
67
# File 'lib/proxy_cheap_client/models/base.rb', line 65

def request(method, path, body = nil)
  self.class.request(method, path, body)
end