Class: B2::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_id, application_key) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(, application_key)
  @account_id = 
  @application_key = application_key
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



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

def 
  @account_id
end

#application_keyObject (readonly)

Returns the value of attribute application_key.



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

def application_key
  @application_key
end

#download_urlObject (readonly)

Returns the value of attribute download_url.



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

def download_url
  @download_url
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/b2/connection.rb', line 57

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

#authorization_tokenObject



50
51
52
53
54
55
# File 'lib/b2/connection.rb', line 50

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

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

  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
  @minimum_part_size = resp['absoluteMinimumPartSize']
  @recommended_part_size = resp['recommendedPartSize']
  @auth_token = resp['authorizationToken']
  @download_url = resp['downloadUrl']
end

#disconnect!Object



38
39
40
41
42
43
# File 'lib/b2/connection.rb', line 38

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

#get(path, body = nil, &block) ⇒ Object



86
87
88
89
90
# File 'lib/b2/connection.rb', line 86

def get(path, body=nil, &block)
  request = Net::HTTP::Get.new(path)
  
  send_request(request, body, &block)
end

#post(path, body = nil, &block) ⇒ Object



92
93
94
95
96
# File 'lib/b2/connection.rb', line 92

def post(path, body=nil, &block)
  request = Net::HTTP::Post.new(path)
  
  send_request(request, body, &block)
end

#reconnect!Object



45
46
47
48
# File 'lib/b2/connection.rb', line 45

def reconnect!
  disconnect!
  connect!
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/b2/connection.rb', line 61

def send_request(request, body=nil, &block)
  request['Authorization'] = authorization_token
  request.body = (body.is_a?(String) ? body : JSON.generate(body)) if body
  
  return_value = nil
  close_connection = false
  @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
      raise "Error connecting to B2 API #{response.body}"
    end
  end
  @connection.finish if close_connection

  return_value
end