Module: Pcloud::Client

Defined in:
lib/pcloud/client.rb

Defined Under Namespace

Classes: ConfigurationError, ErrorResponse

Constant Summary collapse

VALID_DATA_REGIONS =
[
  EU_DATA_REGION = "EU".freeze,
  US_DATA_REGION = "US".freeze
].freeze
US_API_BASE =
"api.pcloud.com".freeze
EU_API_BASE =
"eapi.pcloud.com".freeze
DEFAULT_TIMEOUT_SECONDS =
8.freeze

Class Method Summary collapse

Class Method Details

.configure(access_token: nil, data_region: nil, timeout_seconds: nil) ⇒ Object



15
16
17
18
19
20
# File 'lib/pcloud/client.rb', line 15

def configure(access_token: nil, data_region: nil, timeout_seconds: nil)
  @@access_token = access_token
  @@data_region = data_region
  @@timeout_seconds = timeout_seconds.nil? ? nil : timeout_seconds.to_i
  true # Don't accidentally return any secrets from the configure method
end

.execute(method, query: {}, body: {}) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pcloud/client.rb', line 22

def execute(method, query: {}, body: {})
  verb = ["uploadfile"].include?(method) ? :post : :get
  options = {
    headers: { "Authorization" => "Bearer #{access_token_from_config_or_env}" },
    timeout: timeout_seconds_from_config_or_env # this sets both the open and read timeouts to the same value
  }
  options[:query] = query unless query.empty?
  options[:body] = body unless body.empty?
  response = HTTParty.public_send(verb, "https://#{closest_server}/#{method}", options)
  json_response = JSON.parse(response.body)
  raise ErrorResponse.new(json_response["error"]) if json_response["error"]
  json_response
end