=begin

This is the bkblz ruby gem, a library for the Backblaze B2 cloud
storage API: https://www.backblaze.com/b2/docs/

Currently the gem supports the following V1 API calls:

  * b2_authorize_account
  * b2_create_bucket
  * b2_delete_bucket
  * b2_delete_file_version
  * b2_list_buckets
  * b2_list_file_names
  * b2_list_file_versions
  * b2_upload_file
  * b2_download_file_by_id
  * b2_download_file_by_name

Run `ruby README.rb` for a working demo, but first add your
application key and account id in the slots below.

Or `gem install bkblz` to begin. After install try `bkblz -h` to use
the CLI

=end


$: << 'lib'
require 'bkblz'

Bkblz.configure do |config_map|
  config_map.merge!(
    :application_key => "!!! API KEY !!!",
    :account_id => "!!! ACCOUNT ID !!!",
    :debug_http => false,
    :log_level => :info # change this to :debug for more info
  )
end

Bkblz.log.info do "# The block above configures some defaults (including the\n# logger, which is why this is after the configure block), see\n# Bkblz::Config for details. This is where to set the account_id\n# and application_key.\n"
end

if Bkblz.config..match /!!!/
  Bkblz.log.error "you didn't fill in your credentials, read the comments"
  exit 1
end

def run_readme
  Bkblz.log.info do "  # Using the config above, create an authorized session. All\n  # requests will run in the context of this session. See\n  # +Bkblz::V1::Session#authorize+.\n  EOS\n  end\n  Bkblz::V1::Session.authorize Bkblz.config do |session|\n    Bkblz.log.info \"API session => \#{session}\"\n\n    Bkblz.log.info do <<-EOS\n    # First try to find an existing bucket named my-test-bucket,\n    # we'll use that if it exists. All requests in a session are sent\n    # through the session so that the request object gets access to the\n    # auth credentials.\n    EOS\n    end\n    buckets = session.send(Bkblz::V1::ListBucketsRequest.new).buckets\n    Bkblz.log.info \"bucket list => \#{buckets}\"\n\n    new_bucket_name = \"bkblz-readme-bucket\"\n    bucket = buckets.find { |b| b.bucket_name == new_bucket_name  }\n\n    Bkblz.log.info do <<-EOS\n    # Otherwise create a new my-test-bucket\n    EOS\n    end\n\n    begin\n      unless bucket\n        bucket = Bkblz::V1::Model::Bucket.new \\\n                                            :bucket_name => new_bucket_name,\n        :bucket_type => \"allPrivate\",\n        :account_id => session.account_id\n\n        Bkblz.log.info do <<-EOS\n        # Pass a model to the CreateBucketRequest,\n        # models are just named wrappers with dynamic methods\n        # around the JSON responses provided back from the Bkblz\n        # API. See lib/bkblz/v1/models.rb for a list of defined API\n        # objects. See Bkblz::V1::Model::Base for how it work.\n        EOS\n        end\n        request = Bkblz::V1::CreateBucketRequest.new bucket\n        Bkblz.log.info \"bucket model => \#{bucket}\"\n\n        # Bkblz::V1::Response objects are returned from +send+. Some\n        # provide a to_model method if they declare the +response_model+\n        # in the class definition.\n        bucket = session.send(request).to_model\n        Bkblz.log.info \"created bucket => \#{bucket}\"\n      end\n\n      Bkblz.log.info do <<-EOS\n      # Uploading a file begins with getting a dynamic URL from the API.\n      EOS\n      end\n      upload_auth = session.send(\n        Bkblz::V1::GetUploadUrlRequest.new bucket.bucket_id).to_model\n      Bkblz.log.info \"upload file URL => \#{upload_auth.upload_url}\"\n\n\n      Bkblz.log.info do <<-EOS\n      # Use the upload_auth model (a\n      # Bkblz::V1::Model::UploadAuth) to upload some files.\n      EOS\n      end\n      5.times do |i|\n        body = \"some text \#{i}\"\n        file_name = \"some_text_\#{i}.txt\"\n        content_type = nil\n\n        upload_file_info = session.send(\n          Bkblz::V1::UploadFileRequest.new upload_auth, body, file_name,\n                                           content_type, Time.now.to_i * 1000).to_model\n        Bkblz.log.info \"uploaded file => \#{upload_file_info.file_name}\"\n      end\n\n      Bkblz.log.info do <<-EOS\n      # We uploaded 5 files above, here we'll read back out\n      # metadata from the first 2 files in the bucket.\n      EOS\n      end\n      list_files_response = session.send(\n        Bkblz::V1::ListFileVersionsRequest.new bucket, 2)\n      bucket_files_info = list_files_response.files\n      Bkblz.log.info \"first 2 files => \#{bucket_files_info.map(&:file_name).join \"\\n\"}\"\n\n      Bkblz.log.info do <<-EOS\n      # The response object returned object is a\n      # Bkblz::Api::PaginatedResponse. Use its +has_more?+ and\n      # +next_request+ methods to page through more results.\n      EOS\n      end\n      while list_files_response.has_more?\n        list_files_response = session.send list_files_response.next_request 100\n        bucket_files_info.concat list_files_response.files\n        Bkblz.log.info \"next N files => \#{list_files_response.files.map(&:file_name).join \"\\n\"}\"\n      end\n\n      Bkblz.log.info do <<-EOS\n      # Files can also be listed by name.\n      EOS\n      end\n      list_files_response = session.send(\n        Bkblz::V1::ListFileNamesRequest.new bucket, 10)\n      bucket_files_info = list_files_response.files\n      Bkblz.log.info \"files by name => \#{bucket_files_info.map(&:file_name).join \"\\n\"}\"\n\n      Bkblz.log.info do <<-EOS\n      # Files can be downloaded by file name\n      EOS\n      end\n      file_name = bucket_files_info.first.file_name\n      file_name_download = session.send(\n        Bkblz::V1::DownloadFileByNameRequest.new bucket, file_name).to_model\n      Bkblz.log.info file_name_download\n      Bkblz.log.info \"file body: \#{file_name_download.body}\"\n\n      Bkblz.log.info do <<-EOS\n      # Files can also be downloaded by file id\n      EOS\n      end\n      file_info = bucket_files_info[1]\n      file_id_download = session.send(\n        Bkblz::V1::DownloadFileByIdRequest.new file_info).to_model\n      Bkblz.log.info file_id_download\n      Bkblz.log.info \"file body: \#{file_id_download.body}\"\n\n      Bkblz.log.info do <<-EOS\n      # File byte ranges can also be downloaded\n      EOS\n      end\n      bytes = (2..8)\n      byte_range_download = session.send(\n        Bkblz::V1::DownloadFileByNameRequest.new bucket, file_name, bytes).to_model\n      Bkblz.log.info \"file bytes: \#{byte_range_download.body}\"\n    rescue => e\n      Bkblz.log.error \"there was an error: \#{e}\"\n      Bkblz.log.error e.backtrace.join \"\\n\"\n      Bkblz.log.warn \"cleaning up the bucket\"\n    ensure\n      clear_the_bucket session, bucket\n    end\n  end\nend\n\ndef clear_the_bucket(session, bucket)\n  list_files_response = session.send(\n    Bkblz::V1::ListFileVersionsRequest.new bucket)\n  bucket_files_info = list_files_response.files\n\n  Bkblz.log.info do <<-EOS\n  # Delete all the files in the bucket that we added. This is\n  # a service requirement to deleting a bucket.\n  EOS\n  end\n  bucket_files_info.each do |file_info|\n    request = Bkblz::V1::DeleteFileVersionRequest.new file_info\n    delete_file_version_response = session.send request\n    Bkblz.log.info \"deleted file => \#{delete_file_version_response.to_model.file_name}\"\n  end\n\n  Bkblz.log.info do <<-EOS\n  # Finally, delete the bucket.\n  EOS\n  end\n  request = Bkblz::V1::DeleteBucketRequest.new bucket\n  delete_bucket_response = session.send request\n  Bkblz.log.info \"deleted bucket => \#{bucket.bucket_name}/\#{bucket.bucket_id}\"\nend\n\nrun_readme\n"