Class: Prefab::HttpConnection
- Inherits:
-
Object
- Object
- Prefab::HttpConnection
- Defined in:
- lib/prefab/http_connection.rb
Constant Summary collapse
- AUTH_USER =
'authuser'
- PROTO_HEADERS =
{ 'Content-Type' => 'application/x-protobuf', 'Accept' => 'application/x-protobuf', 'X-PrefabCloud-Client-Version' => "prefab-cloud-ruby-#{Prefab::VERSION}" }.freeze
Instance Method Summary collapse
- #connection(headers = {}) ⇒ Object
- #get(path, headers = {}) ⇒ Object
-
#initialize(uri, api_key) ⇒ HttpConnection
constructor
A new instance of HttpConnection.
- #post(path, body) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(uri, api_key) ⇒ HttpConnection
Returns a new instance of HttpConnection.
12 13 14 15 |
# File 'lib/prefab/http_connection.rb', line 12 def initialize(uri, api_key) @uri = uri @api_key = api_key end |
Instance Method Details
#connection(headers = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/prefab/http_connection.rb', line 29 def connection(headers = {}) if Faraday::VERSION[0].to_i >= 2 Faraday.new(@uri) do |conn| conn.request :authorization, :basic, AUTH_USER, @api_key conn.headers.merge!(headers) end else Faraday.new(@uri) do |conn| conn.request :basic_auth, AUTH_USER, @api_key conn.headers.merge!(headers) end end end |
#get(path, headers = {}) ⇒ Object
21 22 23 |
# File 'lib/prefab/http_connection.rb', line 21 def get(path, headers = {}) connection(PROTO_HEADERS.merge(headers)).get(path) end |
#post(path, body) ⇒ Object
25 26 27 |
# File 'lib/prefab/http_connection.rb', line 25 def post(path, body) connection(PROTO_HEADERS).post(path, body.to_proto) end |
#uri ⇒ Object
17 18 19 |
# File 'lib/prefab/http_connection.rb', line 17 def uri @uri end |