Class: Rubyists::Linear::GraphApi

Inherits:
Object
  • Object
show all
Includes:
SemanticLogger::Loggable
Defined in:
lib/linear/api.rb

Overview

Responsible for making requests to the Linear API

Constant Summary collapse

BASE_URI =
'https://api.linear.app/graphql'
RETRY_AFTER =
lambda do |*|
  @retries ||= 0
  @retries += 1
  seconds = @retries * 2
  logger.warn("Retry number #{@retries}, retrying after #{seconds} seconds")
  seconds
end

Instance Method Summary collapse

Instance Method Details

#api_keyObject

Raises:



53
54
55
56
57
58
# File 'lib/linear/api.rb', line 53

def api_key
  return @api_key if @api_key
  raise NoKeyError, 'No LINEAR_API_KEY found in Environment variables.' unless ENV.key?('LINEAR_API_KEY')

  @api_key = ENV.fetch('LINEAR_API_KEY')
end

#call(body) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
# File 'lib/linear/api.rb', line 33

def call(body)
  res = session.post(BASE_URI, body:)
  raise SmellsBad, "Bad Response from #{BASE_URI}: #{res}" if res.error

  data = JSON.parse(res.body.read, symbolize_names: true)
  raise SmellsBad, "No Data Returned for #{body}" unless data&.key?(:data)

  data[:data]
end

#headersObject



26
27
28
29
30
31
# File 'lib/linear/api.rb', line 26

def headers
  @headers ||= {
    'Content-Type' => 'application/json',
    'Authorization' => api_key
  }
end

#query(query) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/linear/api.rb', line 43

def query(query)
  call format('{ "query": %s }', query.to_s.to_json)
rescue SmellsBad => e
  logger.error('Error in query', query:, error: e)
  raise e unless Rubyists::Linear.verbosity > 2

  require 'pry'
  binding.pry # rubocop:disable Lint/Debugger
end

#sessionObject



20
21
22
23
24
# File 'lib/linear/api.rb', line 20

def session
  return @session if @session

  @session = HTTPX.plugin(:retries, retry_after: RETRY_AFTER, max_retries: 5).with(headers:)
end