Class: B2::APIConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/b2/api_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_id, secret) ⇒ APIConnection

Returns a new instance of APIConnection.



6
7
8
9
# File 'lib/b2/api_connection.rb', line 6

def initialize(key_id, secret)
  @key_id = key_id
  @key_secret = secret
end

Instance Attribute Details

#download_urlObject (readonly)

Returns the value of attribute download_url.



4
5
6
# File 'lib/b2/api_connection.rb', line 4

def download_url
  @download_url
end

#key_idObject (readonly)

Returns the value of attribute key_id.



4
5
6
# File 'lib/b2/api_connection.rb', line 4

def key_id
  @key_id
end

#key_secretObject (readonly)

Returns the value of attribute key_secret.



4
5
6
# File 'lib/b2/api_connection.rb', line 4

def key_secret
  @key_secret
end

Instance Method Details

#account_idObject



39
40
41
42
43
44
# File 'lib/b2/api_connection.rb', line 39

def 
  return @account_id if !@account_id.nil?
  
  connect!
  @account_id
end

#active?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/b2/api_connection.rb', line 65

def active?
  !@connection.nil? && @connection.active?
end

#authorization_tokenObject



58
59
60
61
62
63
# File 'lib/b2/api_connection.rb', line 58

def authorization_token
  if @auth_token_expires_at.nil? || @auth_token_expires_at <= Time.now.to_i
    reconnect!
  end
  @auth_token
end

#connect!Object



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
# File 'lib/b2/api_connection.rb', line 11

def connect!
  conn = Net::HTTP.new('api.backblazeb2.com', 443)
  conn.use_ssl = true
  
  req = Net::HTTP::Get.new('/b2api/v2/b2_authorize_account')
  req.basic_auth(@key_id, @key_secret)

  key_expiration = Time.now.to_i + 86_400 #24hr expiry
  resp = conn.start { |http| http.request(req) }
  if resp.is_a?(Net::HTTPSuccess)
    resp = JSON.parse(resp.body)
  else
    raise "Error connecting to B2 API"
  end

  uri = URI.parse(resp['apiUrl'])
  @connection = Net::HTTP.new(uri.host, uri.port)
  @connection.use_ssl = uri.scheme == 'https'
  @connection.start

  @auth_token_expires_at = key_expiration
  @account_id = resp['accountId']
  @minimum_part_size = resp['absoluteMinimumPartSize']
  @recommended_part_size = resp['recommendedPartSize']
  @auth_token = resp['authorizationToken']
  @download_url = resp['downloadUrl']
end

#connectionObject



69
70
71
72
# File 'lib/b2/api_connection.rb', line 69

def connection
  reconnect! if !active?
  @connection
end

#disconnect!Object



46
47
48
49
50
51
# File 'lib/b2/api_connection.rb', line 46

def disconnect!
  if @connection
    @connection.finish if @connection.active?
    @connection = nil
  end
end

#reconnect!Object



53
54
55
56
# File 'lib/b2/api_connection.rb', line 53

def reconnect!
  disconnect!
  connect!
end

#send_request(request, body = nil, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
# File 'lib/b2/api_connection.rb', line 74

def send_request(request, body=nil, &block)
  retries = 0
  request['Authorization'] = authorization_token
  request.body = (body.is_a?(String) ? body : JSON.generate(body)) if body
  
  return_value = nil
  close_connection = false
  begin
    connection.request(request) do |response|
      close_connection = response['Connection'] == 'close'
      
      case response
      when Net::HTTPSuccess
        if block_given?
          return_value = yield(response)
        else
          return_value = JSON.parse(response.body)
        end
      else
        body = JSON.parse(response.body)
        case body['code']
        when 'not_found'
          raise B2::NotFound(body['message'])
        when 'expired_auth_token'
          raise B2::ExpiredAuthToken(body['message'])
        else
          raise "Error connecting to B2 API #{response.body}"
        end
      end
    end
    
  # Unexpected EOF (end of file) errors can occur when streaming from a
  # remote because of Backblaze quota restrictions.
  rescue B2::ExpiredAuthToken, EOFError
    reconnect!
    retries =+ 1
    retry if retries < 2
  end
  disconnect! if close_connection

  return_value
end