Class: HomebrewAutomation::Bintray::Client
- Inherits:
-
Object
- Object
- HomebrewAutomation::Bintray::Client
- Defined in:
- lib/homebrew_automation/bintray/client.rb
Overview
A bare-bones Bintray API client that implements only the methods needed for Homebrew things.
Instance Method Summary collapse
- #api_headers ⇒ Hash
-
#auth_headers ⇒ Hash
Implement HTTP Basich Auth, as per RFC 7617.
-
#create_version(repo_name, package_name, version_name) ⇒ RestClient::Response
POST /packages/:subject/:repo/:package/versions
. -
#get_all_files_in_version(repo_name, package_name, version_name) ⇒ RestClient::Response
GET /packages/:subject/:repo/:package/versions/:version/files[?include_unpublished=0/1]
. -
#initialize(username, api_key, http: RestClient, base_url: "https://bintray.com/api/v1") ⇒ Client
constructor
A new instance of Client.
-
#rel(slash_subpath) ⇒ String
Resolve a relative path into a URL using the current base_url.
-
#safe_username ⇒ String
Bintray username, URI-escaped.
-
#upload_file(repo_name, package_name, version_name, filename, content, publish: 1) ⇒ RestClient::Response
PUT /content/:subject/:repo/:package/:version/:file_path[?publish=0/1][?override=0/1][?explode=0/1]
.
Constructor Details
#initialize(username, api_key, http: RestClient, base_url: "https://bintray.com/api/v1") ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/homebrew_automation/bintray/client.rb', line 20 def initialize( username, api_key, http: RestClient, base_url: "https://bintray.com/api/v1" ) @username = username @api_key = api_key @base_url = base_url @http = http # allow injecting mocks for testing end |
Instance Method Details
#api_headers ⇒ Hash
109 110 111 |
# File 'lib/homebrew_automation/bintray/client.rb', line 109 def api_headers { "Content-Type" => "application/json" }.update auth_headers end |
#auth_headers ⇒ Hash
Implement HTTP Basich Auth, as per RFC 7617.
Let’s not bring in a library just for these two lines.
118 119 120 121 |
# File 'lib/homebrew_automation/bintray/client.rb', line 118 def auth_headers cred = Base64.strict_encode64("#{@username}:#{@api_key}") { Authorization: "Basic #{cred}" } end |
#create_version(repo_name, package_name, version_name) ⇒ RestClient::Response
POST /packages/:subject/:repo/:package/versions
Redundant: Bintray seems to create nonexistant versions for you if you just try to upload files into it.
41 42 43 44 45 46 47 48 49 |
# File 'lib/homebrew_automation/bintray/client.rb', line 41 def create_version(repo_name, package_name, version_name) safe_repo = URI.escape(repo_name) safe_pkg = URI.escape(package_name) @http.post( rel("/packages/#{safe_username}/#{safe_repo}/#{safe_pkg}/versions"), { name: version_name }.to_json, api_headers ) end |
#get_all_files_in_version(repo_name, package_name, version_name) ⇒ RestClient::Response
GET /packages/:subject/:repo/:package/versions/:version/files[?include_unpublished=0/1]
Useful when seeing what bottles have already been built.
84 85 86 87 88 89 90 91 |
# File 'lib/homebrew_automation/bintray/client.rb', line 84 def get_all_files_in_version(repo_name, package_name, version_name) safe_repo = URI.escape(repo_name) safe_pkg = URI.escape(package_name) safe_ver = URI.escape(version_name) @http.get( rel("/packages/#{safe_username}/#{safe_repo}/#{safe_pkg}/versions/#{safe_ver}/files"), auth_headers) end |
#rel(slash_subpath) ⇒ String
Resolve a relative path into a URL using the current base_url
104 105 106 |
# File 'lib/homebrew_automation/bintray/client.rb', line 104 def rel(slash_subpath) @base_url + slash_subpath end |
#safe_username ⇒ String
Bintray username, URI-escaped.
96 97 98 |
# File 'lib/homebrew_automation/bintray/client.rb', line 96 def safe_username URI.escape(@username) end |
#upload_file(repo_name, package_name, version_name, filename, content, publish: 1) ⇒ RestClient::Response
PUT /content/:subject/:repo/:package/:version/:file_path[?publish=0/1][?override=0/1][?explode=0/1]
Bintray seems to expect the byte sequence of the file to be written straight out into the HTTP request body, optionally via Transfer-Encoding: chunked
. So we pass the content
String straight through to RestClient
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/homebrew_automation/bintray/client.rb', line 63 def upload_file(repo_name, package_name, version_name, filename, content, publish: 1) safe_repo = URI.escape(repo_name) safe_pkg = URI.escape(package_name) safe_ver = URI.escape(version_name) safe_filename = URI.escape(filename) safe_publish = URI.escape(publish.to_s) @http.put( rel("/content/#{safe_username}/#{safe_repo}/#{safe_pkg}/#{safe_ver}/#{safe_filename}?publish=#{safe_publish}"), content, auth_headers ) end |