chore: replace graphlient with HTTParty

This commit is contained in:
Muhsin Keloth
2024-05-16 16:18:47 +05:30
parent b663f3201c
commit 31732a1a7b
5 changed files with 82 additions and 79515 deletions
-3
View File
@@ -155,9 +155,6 @@ gem 'stripe'
## to populate db with sample data
gem 'faker'
# to consume GraphQL-based APIs
gem 'graphlient'
# Include logrange conditionally in intializer using env variable
gem 'lograge', '~> 0.14.0', require: false
-9
View File
@@ -333,14 +333,6 @@ GEM
multi_json (~> 1.11)
os (>= 0.9, < 2.0)
signet (>= 0.16, < 2.a)
graphlient (0.8.0)
faraday (~> 2.0)
graphql-client
graphql (2.3.2)
base64
graphql-client (0.22.0)
activesupport (>= 3.0)
graphql (>= 1.13.0)
groupdate (6.2.1)
activesupport (>= 5.2)
grpc (1.62.0)
@@ -884,7 +876,6 @@ DEPENDENCIES
google-cloud-dialogflow-v2
google-cloud-storage
google-cloud-translate-v3
graphlient
groupdate
grpc
haikunator
+43 -36
View File
@@ -1,5 +1,3 @@
require 'graphlient'
require_relative 'linear_queries'
require_relative 'linear_mutations'
@@ -8,37 +6,44 @@ class Linear
PRIORITY_LEVELS = (0..4).to_a
def initialize(api_key)
@api_key = api_key
raise ArgumentError, 'Missing Credentials' if api_key.blank?
@client = Graphlient::Client.new(
BASE_URL,
headers: {
'Authorization' => api_key,
'Content-Type' => 'application/json'
}
)
end
def teams
execute_query(LinearQueries::TEAMS_QUERY)
query = {
query: LinearQueries::TEAMS_QUERY
}
response = post(query)
process_response(response)
end
def team_entities(team_id)
raise ArgumentError, 'Missing team id' if team_id.blank?
execute_query(LinearQueries.team_entities_query(team_id))
query = {
query: LinearQueries.team_entities_query(team_id)
}
response = post(query)
process_response(response)
end
def search_issue(term)
raise ArgumentError, 'Missing search term' if term.blank?
execute_query(LinearQueries.search_issue(term))
query = {
query: LinearQueries.search_issue(term)
}
response = post(query)
process_response(response)
end
def linked_issue(url)
raise ArgumentError, 'Missing link' if url.blank?
execute_query(LinearQueries.linked_issue(url))
query = {
query: LinearQueries.linked_issue(url)
}
response = post(query)
process_response(response)
end
def create_issue(params)
@@ -54,21 +59,30 @@ class Linear
priority: params[:priority],
labelIds: params[:label_ids]
}.compact
execute_mutation(LinearMutations.issue_create(variables))
mutation = LinearMutations.issue_create(variables)
response = post({ query: mutation })
process_response(response)
end
def link_issue(link, issue_id)
raise ArgumentError, 'Missing link' if link.blank?
raise ArgumentError, 'Missing issue id' if issue_id.blank?
execute_mutation(LinearMutations.issue_link(issue_id, link))
payload = {
query: LinearMutations.issue_link(issue_id, link)
}
response = post(payload)
process_response(response)
end
def unlink_issue(link_id)
raise ArgumentError, 'Missing link id' if link_id.blank?
execute_mutation(LinearMutations.unlink_issue(link_id))
payload = {
query: LinearMutations.unlink_issue(link_id)
}
response = post(payload)
process_response(response)
end
private
@@ -91,24 +105,17 @@ class Linear
raise ArgumentError, 'label_ids must be an array of strings.'
end
def execute_query(query)
response = @client.query(query)
log_and_return_error("Error retrieving data: #{response.errors.messages}") if response.data.nil? && response.errors.any?
response.data.to_h
rescue StandardError => e
log_and_return_error("Error: #{e.message}")
def post(payload)
HTTParty.post(
BASE_URL,
headers: { 'Authorization' => @api_key, 'Content-Type' => 'application/json' },
body: payload.to_json
)
end
def execute_mutation(query)
response = @client.query(query)
log_and_return_error("Error creating issue: #{response.errors.messages}") if response.data.nil? && response.errors.any?
response.data.to_h
rescue StandardError => e
log_and_return_error("Error: #{e.message}")
end
def process_response(response)
return response.parsed_response['data'].with_indifferent_access if response.success?
def log_and_return_error(message)
Rails.logger.error message
{ error: message }
{ error: response.parsed_response, error_code: response.code }
end
end
-79417
View File
File diff suppressed because it is too large Load Diff
+39 -50
View File
@@ -6,9 +6,6 @@ describe Linear do
let(:linear_client) { described_class.new(api_key) }
let(:headers) { { 'Content-Type' => 'application/json', 'Authorization' => api_key } }
let(:schema) { JSON.parse(File.read('spec/fixtures/linear-schema.json')) }
let(:client) { Graphlient::Client.new(url, schema: Graphlient::Schema.new({ 'data' => schema }, nil), headers: headers) }
it 'raises an exception if the API key is absent' do
expect { described_class.new(nil) }.to raise_error(ArgumentError, 'Missing Credentials')
end
@@ -16,9 +13,10 @@ describe Linear do
context 'when querying teams' do
context 'when the API response is success' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 200, body: { data: { teams: { nodes: [{ id: 'team1', name: 'Team One' }] } } }.to_json)
.to_return(status: 200,
body: { success: true, data: { teams: { nodes: [{ id: 'team1', name: 'Team One' }] } } }.to_json,
headers: headers)
end
it 'returns team data' do
@@ -29,14 +27,14 @@ describe Linear do
context 'when the API response is an error' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 422, body: { errors: [{ message: 'Error retrieving data' }] }.to_json)
.to_return(status: 422, body: { errors: [{ message: 'Error retrieving data' }] }.to_json,
headers: headers)
end
it 'raises an exception' do
response = linear_client.teams
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
expect(response).to eq({ :error => { 'errors' => [{ 'message' => 'Error retrieving data' }] }, :error_code => 422 })
end
end
end
@@ -46,14 +44,15 @@ describe Linear do
context 'when the API response is success' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 200, body: { data: {
users: { nodes: [{ id: 'user1', name: 'User One' }] },
projects: { nodes: [{ id: 'project1', name: 'Project One' }] },
workflowStates: { nodes: [] },
issueLabels: { nodes: [{ id: 'bug', name: 'Bug' }] }
} }.to_json)
.to_return(status: 200,
body: { success: true, data: {
users: { nodes: [{ id: 'user1', name: 'User One' }] },
projects: { nodes: [{ id: 'project1', name: 'Project One' }] },
workflowStates: { nodes: [] },
issueLabels: { nodes: [{ id: 'bug', name: 'Bug' }] }
} }.to_json,
headers: headers)
end
it 'returns team entities' do
@@ -69,14 +68,14 @@ describe Linear do
context 'when the API response is an error' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 422, body: { errors: [{ message: 'Error retrieving data' }] }.to_json)
.to_return(status: 422, body: { errors: [{ message: 'Error retrieving data' }] }.to_json,
headers: headers)
end
it 'raises an exception' do
response = linear_client.team_entities(team_id)
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
expect(response).to eq({ :error => { 'errors' => [{ 'message' => 'Error retrieving data' }] }, :error_code => 422 })
end
end
end
@@ -95,9 +94,8 @@ describe Linear do
context 'when the API response is success' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 200, body: { data: { issueCreate: { id: 'issue1', title: 'Title' } } }.to_json)
.to_return(status: 200, body: { success: true, data: { issueCreate: { id: 'issue1', title: 'Title' } } }.to_json, headers: headers)
end
it 'creates an issue' do
@@ -139,28 +137,26 @@ describe Linear do
context 'when the API key is invalid' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 401, body: { errors: [{ message: 'Invalid API key' }] }.to_json)
.to_return(status: 401, body: { errors: [{ message: 'Invalid API key' }] }.to_json, headers: headers)
end
it 'raises an exception' do
response = linear_client.create_issue(params)
expect(response).to eq({ :error => 'Error: the server responded with status 401' })
expect(response).to eq({ :error => { 'errors' => [{ 'message' => 'Invalid API key' }] }, :error_code => 401 })
end
end
end
context 'when the API response is an error' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 422, body: { errors: [{ message: 'Error creating issue' }] }.to_json)
.to_return(status: 422, body: { errors: [{ message: 'Error creating issue' }] }.to_json, headers: headers)
end
it 'raises an exception' do
response = linear_client.create_issue(params)
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
expect(response).to eq({ :error => { 'errors' => [{ 'message' => 'Error creating issue' }] }, :error_code => 422 })
end
end
end
@@ -171,9 +167,8 @@ describe Linear do
context 'when the API response is success' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 200, body: { data: { attachmentLinkURL: { id: 'attachment1' } } }.to_json)
.to_return(status: 200, body: { success: true, data: { attachmentLinkURL: { id: 'attachment1' } } }.to_json, headers: headers)
end
it 'links an issue' do
@@ -200,14 +195,13 @@ describe Linear do
context 'when the API response is an error' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 422, body: { errors: [{ message: 'Error linking issue' }] }.to_json)
.to_return(status: 422, body: { errors: [{ message: 'Error linking issue' }] }.to_json, headers: headers)
end
it 'raises an exception' do
response = linear_client.link_issue(link, issue_id)
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
expect(response).to eq({ :error => { 'errors' => [{ 'message' => 'Error linking issue' }] }, :error_code => 422 })
end
end
end
@@ -217,9 +211,8 @@ describe Linear do
context 'when the API response is success' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 200, body: { data: { attachmentLinkURL: { id: 'attachment1' } } }.to_json)
.to_return(status: 200, body: { success: true, data: { attachmentLinkURL: { id: 'attachment1' } } }.to_json, headers: headers)
end
it 'unlinks an issue' do
@@ -238,14 +231,13 @@ describe Linear do
context 'when the API response is an error' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 422, body: { errors: [{ message: 'Error unlinking issue' }] }.to_json)
.to_return(status: 422, body: { errors: [{ message: 'Error unlinking issue' }] }.to_json, headers: headers)
end
it 'raises an exception' do
response = linear_client.unlink_issue(link_id)
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
expect(response).to eq({ :error => { 'errors' => [{ 'message' => 'Error unlinking issue' }] }, :error_code => 422 })
end
end
end
@@ -255,9 +247,9 @@ describe Linear do
context 'when the API response is success' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 200, body: { data: { searchIssues: { nodes: [{ id: 'issue1', title: 'Title' }] } } }.to_json)
.to_return(status: 200, body: { success: true,
data: { searchIssues: { nodes: [{ id: 'issue1', title: 'Title' }] } } }.to_json, headers: headers)
end
it 'returns issues' do
@@ -268,44 +260,41 @@ describe Linear do
context 'when the API response is an error' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 422, body: { errors: [{ message: 'Error retrieving data' }] }.to_json)
.to_return(status: 422, body: { errors: [{ message: 'Error retrieving data' }] }.to_json,
headers: headers)
end
it 'raises an exception' do
response = linear_client.search_issue(term)
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
expect(response).to eq({ :error => { 'errors' => [{ 'message' => 'Error retrieving data' }] }, :error_code => 422 })
end
end
end
context 'when querying linked issues' do
let(:url) { 'https://example.com' }
context 'when the API response is success' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 200, body: { data: { linkedIssue: { id: 'issue1', title: 'Title' } } }.to_json)
.to_return(status: 200, body: { success: true, data: { linkedIssue: { id: 'issue1', title: 'Title' } } }.to_json, headers: headers)
end
it 'returns linked issues' do
response = linear_client.linked_issue(url)
response = linear_client.linked_issue('app.chatwoot.com')
expect(response).to eq({ 'linkedIssue' => { 'id' => 'issue1', 'title' => 'Title' } })
end
end
context 'when the API response is an error' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 422, body: { errors: [{ message: 'Error retrieving data' }] }.to_json)
.to_return(status: 422, body: { errors: [{ message: 'Error retrieving data' }] }.to_json,
headers: headers)
end
it 'raises an exception' do
response = linear_client.linked_issue(url)
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
response = linear_client.linked_issue('app.chatwoot.com')
expect(response).to eq({ :error => { 'errors' => [{ 'message' => 'Error retrieving data' }] }, :error_code => 422 })
end
end
end