Class: B2::Connection
- Inherits:
-
Object
- Object
- B2::Connection
- Defined in:
- lib/b2/connection.rb
Instance Attribute Summary collapse
-
#account_id ⇒ Object
readonly
Returns the value of attribute account_id.
-
#application_key ⇒ Object
readonly
Returns the value of attribute application_key.
-
#download_url ⇒ Object
readonly
Returns the value of attribute download_url.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #authorization_token ⇒ Object
- #buckets ⇒ Object
- #connect! ⇒ Object
- #disconnect! ⇒ Object
- #download(bucket, key, to = nil, &block) ⇒ Object
- #get(path, body = nil, &block) ⇒ Object
- #get_download_url(bucket, filename, expires_in: 3_600) ⇒ Object
-
#initialize(account_id, application_key) ⇒ Connection
constructor
A new instance of Connection.
- #lookup_bucket_id(name) ⇒ Object
- #post(path, body = nil, &block) ⇒ Object
- #reconnect! ⇒ Object
- #send_request(request, body = nil, &block) ⇒ Object
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(account_id, application_key) @account_id = account_id @application_key = application_key end |
Instance Attribute Details
#account_id ⇒ Object (readonly)
Returns the value of attribute account_id.
4 5 6 |
# File 'lib/b2/connection.rb', line 4 def account_id @account_id end |
#application_key ⇒ Object (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_url ⇒ Object (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
58 59 60 |
# File 'lib/b2/connection.rb', line 58 def active? !@connection.nil? && @connection.active? end |
#authorization_token ⇒ Object
51 52 53 54 55 56 |
# File 'lib/b2/connection.rb', line 51 def if @auth_token_expires_at.nil? || @auth_token_expires_at <= Time.now.to_i reconnect! end @auth_token end |
#buckets ⇒ Object
87 88 89 90 91 |
# File 'lib/b2/connection.rb', line 87 def buckets post('/b2api/v1/b2_list_buckets', {accountId: @account_id})['buckets'].map do |b| B2::Bucket.new(b, self) end 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/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(account_id, 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'] @buckets_cache = [] end |
#disconnect! ⇒ Object
39 40 41 42 43 44 |
# File 'lib/b2/connection.rb', line 39 def disconnect! if @connection @connection.finish if @connection.active? @connection = nil end end |
#download(bucket, key, to = nil, &block) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/b2/connection.rb', line 110 def download(bucket, key, to=nil, &block) opened_file = (to && to.is_a?(String)) to = ::File.open(to, 'wb') if to.is_a?(String) digestor = Digest::SHA1.new data = "" uri = URI.parse(@download_url) conn = Net::HTTP.new(uri.host, uri.port) conn.use_ssl = uri.scheme == 'https' req = Net::HTTP::Get.new("/file/#{bucket}/#{key}") req['Authorization'] = conn.start do |http| http.request(req) do |response| case response when Net::HTTPSuccess response.read_body do |chunk| digestor << chunk if to to << chunk elsif block block(chunk) else data << chunk end end if digestor.hexdigest != response['X-Bz-Content-Sha1'] raise 'file error' end when Net::HTTPNotFound raise B2::NotFound.new(JSON.parse(response.body)['message']) else begin body = JSON.parse(response.body) if body['code'] == 'not_found' raise B2::NotFound(body['message']) else raise "#{body['code']} (#{body['message']})" end rescue raise response.body end end end end if opened_file to.close elsif to to.flush end block.nil? && to.nil? ? data : nil end |
#get(path, body = nil, &block) ⇒ Object
165 166 167 168 169 |
# File 'lib/b2/connection.rb', line 165 def get(path, body=nil, &block) request = Net::HTTP::Get.new(path) send_request(request, body, &block) end |
#get_download_url(bucket, filename, expires_in: 3_600) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/b2/connection.rb', line 101 def get_download_url(bucket, filename, expires_in: 3_600) response = post("/b2api/v1/b2_get_download_authorization", { bucketId: lookup_bucket_id(bucket), fileNamePrefix: filename, validDurationInSeconds: expires_in }) @download_url + '/file/' + bucket + '/' + filename + "?Authorization=" + response['authorizationToken'] end |
#lookup_bucket_id(name) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/b2/connection.rb', line 93 def lookup_bucket_id(name) bucket = @buckets_cache.find{ |b| b.name == name } return bucket.id if bucket @buckets_cache = buckets @buckets_cache.find{ |b| b.name == name }&.id end |
#post(path, body = nil, &block) ⇒ Object
171 172 173 174 175 |
# File 'lib/b2/connection.rb', line 171 def post(path, body=nil, &block) request = Net::HTTP::Post.new(path) send_request(request, body, &block) end |
#reconnect! ⇒ Object
46 47 48 49 |
# File 'lib/b2/connection.rb', line 46 def reconnect! disconnect! connect! end |
#send_request(request, body = nil, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/b2/connection.rb', line 62 def send_request(request, body=nil, &block) request['Authorization'] = 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 |