-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.rb
More file actions
64 lines (51 loc) · 1.72 KB
/
api.rb
File metadata and controls
64 lines (51 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
require 'httpx'
require 'semantic_logger'
module Rubyists
module Linear
# Responsible for making requests to the Linear API
class GraphApi
include SemanticLogger::Loggable
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
def session
return @session if @session
@session = HTTPX.plugin(:retries, retry_after: RETRY_AFTER, max_retries: 5).with(headers:)
end
def headers
@headers ||= {
'Content-Type' => 'application/json',
'Authorization' => api_key
}
end
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
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
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
end
# Acts as a singleton for a GraphApi instance
Api = Rubyists::Linear::GraphApi.new
end
end