Class: HomebrewAutomation::Bintray::Client

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(username, api_key, http: RestClient, base_url: "https://bintray.com/api/v1") ⇒ Client

Returns a new instance of Client.

Parameters:

  • username (String)

    Bintray username; for me this was not my email address

  • api_key (String)

    Bearer-token-like key; generated in the Bintray web UI

  • http (RestClient.Class) (defaults to: RestClient)

    The RestClient class itself

  • base_url (String) (defaults to: "https://bintray.com/api/v1")

    Include the https://; exclude the trailing slash.



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_headersHash

Returns:

  • (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_headersHash

Implement HTTP Basich Auth, as per RFC 7617.

Let’s not bring in a library just for these two lines.

Returns:

  • (Hash)


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.

Parameters:

  • repo_name (String)
  • package_name (String)
  • version_name (String)

Returns:

  • (RestClient::Response)


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.

Parameters:

  • repo_name (String)
  • package_name (String)
  • version_name (String)

Returns:

  • (RestClient::Response)


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

Parameters:

  • slash_subpath (String)

Returns:

  • (String)


104
105
106
# File 'lib/homebrew_automation/bintray/client.rb', line 104

def rel(slash_subpath)
  @base_url + slash_subpath
end

#safe_usernameString

Bintray username, URI-escaped.

Returns:

  • (String)


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

Parameters:

  • repo_name (String)
  • package_name (String)
  • version_name (String)
  • filename (String)

    The filename within one Bintray repository, e.g. hack-assembler-0.1.1.17.high_sierra.bottle.tar.gz

  • content (String)

    The bytes for the file, e.g. from a File.read

Returns:

  • (RestClient::Response)


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