module OpConnect::Connection

Network layer for API clients.

Public Instance Methods

connection() click to toggle source

Connection object for the 1Password Connect API.

@return [Faraday::Client]

# File lib/op_connect/connection.rb, line 69
def connection
  @connection ||= Faraday.new(api_endpoint) do |http|
    http.headers[:user_agent] = user_agent

    http.request :authorization, :Bearer, access_token
    http.request :json

    http.use OpConnect::Response::RaiseError

    http.response :dates
    http.response :json, content_type: "application/json"

    http.adapter adapter, @stubs
  end
end
delete(url, params: {}, headers: {}) click to toggle source

Make an HTTP DELETE request.

@param url [String] The path, relative to {#api_endpoint}. @param params [Hash] Query params for the request. @param headers [Hash] Header params for the request.

@return [Faraday::Response]

# File lib/op_connect/connection.rb, line 61
def delete(url, params: {}, headers: {})
  request :delete, url, params, headers
end
get(url, params: {}, headers: {}) click to toggle source

Make an HTTP GET request.

@param url [String] The path, relative to {#api_endpoint}. @param params [Hash] Query parameters for the request. @param headers [Hash] Header params for the request.

@return [Faraday::Response]

# File lib/op_connect/connection.rb, line 13
def get(url, params: {}, headers: {})
  request :get, url, params, headers
end
last_response() click to toggle source

Response for the last HTTP request.

@return [Faraday::Response]

# File lib/op_connect/connection.rb, line 89
def last_response
  @last_response if defined? @last_response
end
patch(url, body:, headers: {}) click to toggle source

Make an HTTP PATCH request.

@param url [String] The path, relative to {#api_endpoint}. @param body [Hash] Body params for the request. @param headers [Hash] Header params for the request.

@return [Faraday::Response]

# File lib/op_connect/connection.rb, line 49
def patch(url, body:, headers: {})
  request :patch, url, body, headers
end
post(url, body:, headers: {}) click to toggle source

Make an HTTP POST request.

@param url [String] The path, relative to {#api_endpoint}. @param body [Hash] Body params for the request. @param headers [Hash] Header params for the request.

@return [Faraday::Response]

# File lib/op_connect/connection.rb, line 25
def post(url, body:, headers: {})
  request :post, url, body, headers
end
put(url, body:, headers: {}) click to toggle source

Make an HTTP PUT request.

@param url [String] The path, relative to {#api_endpoint}. @param body [Hash] Body params for the request. @param headers [Hash] Header params for the request.

@return [Faraday::Response]

# File lib/op_connect/connection.rb, line 37
def put(url, body:, headers: {})
  request :put, url, body, headers
end

Private Instance Methods

request(method, path, data, headers = {}) click to toggle source
# File lib/op_connect/connection.rb, line 95
def request(method, path, data, headers = {})
  @last_response = response = connection.send(method, path, data, headers)
  response
rescue OpConnect::Error => error
  @last_response = nil
  raise error
end