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

.generate_access_tokenObject



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
61
62
63
64
65
66
67
68
69
# File 'lib/pcloud/client.rb', line 36

def generate_access_token
  puts "=== Follow these steps to generate a pCloud app and access token ==="
  puts "1. Register an app at `https://docs.pcloud.com/my_apps/`"

  puts "2. Enter the client id and secret for the app:"
  print "Client ID > "
  client_id = $stdin.gets.chomp

  print "Client Secret > "
  client_secret = $stdin.gets.chomp

  puts "3. Enter the data region of your pCloud account [EU/US]:"
  print "> "
  region_specific_api_base = $stdin.gets.chomp == "EU" ? "eapi.pcloud.com" : "api.pcloud.com"

  puts "4. Navigate to this URL to start the access code flow:"
  puts "`https://my.pcloud.com/oauth2/authorize?client_id=#{client_id}&response_type=code`"
  puts "5. After logging in, enter the access code provided below:"
  print "> "
  access_code = $stdin.gets.chomp

  puts "6. Requesting access token from pCloud..."
  query = { client_id: client_id, client_secret: client_secret, code: access_code }
  response = HTTParty.post(
    "https://#{region_specific_api_base}/oauth2_token?#{URI.encode_www_form(query)}",
    headers: { "Accept" => "application/json" }
  )

  json_response = JSON.parse(response.body)
  raise json_response["error"] if json_response["error"]
  puts "Done! Your access token is: \n#{json_response["access_token"]}"
  puts "\nStore this value somewhere secure as it can be used to access your"
  puts "pCloud account data and it will not be shown again."
end