mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-26 22:16:28 +08:00
Extract API client constants to constants.rb
This commit is contained in:
parent
a15b1f6dfa
commit
c1cee07a63
3 changed files with 33 additions and 21 deletions
|
|
@ -6,9 +6,11 @@ module ProtocolImporters
|
|||
class ApiClient
|
||||
include HTTParty
|
||||
|
||||
base_uri 'https://www.protocols.io/api/v3/'
|
||||
default_timeout 10
|
||||
logger Rails.logger, :debug
|
||||
CONSTANTS = Constants::PROTOCOLS_IO_V3_API
|
||||
|
||||
base_uri CONSTANTS[:base_uri]
|
||||
default_timeout CONSTANTS[:default_timeout]
|
||||
logger Rails.logger, CONSTANTS[:debug_level]
|
||||
|
||||
def initialize(token = nil)
|
||||
# Currently we support public tokens only (no token needed for public data)
|
||||
|
|
@ -40,14 +42,8 @@ module ProtocolImporters
|
|||
# id of page.
|
||||
# Default is 1.
|
||||
def protocol_list(query_params = {})
|
||||
query = {
|
||||
filter: :public,
|
||||
key: '',
|
||||
order_field: :activity,
|
||||
order_dir: :desc,
|
||||
page_size: 10,
|
||||
page_id: 1
|
||||
}.merge!(query_params)
|
||||
query = CONSTANTS.dig(:endpoints, :protocols, :default_query_params)
|
||||
.merge(query_params)
|
||||
|
||||
self.class.get('/protocols', query: query)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -189,6 +189,28 @@ class Constants
|
|||
# Default user picture avatar
|
||||
DEFAULT_AVATAR_URL = '/images/:style/missing.png'.freeze
|
||||
|
||||
#=============================================================================
|
||||
# Protocol importers
|
||||
#=============================================================================
|
||||
|
||||
PROTOCOLS_IO_V3_API = {
|
||||
base_uri: 'https://www.protocols.io/api/v3/',
|
||||
default_timeout: 10,
|
||||
debug_level: :debug,
|
||||
endpoints: {
|
||||
protocols: {
|
||||
default_query_params: {
|
||||
filter: :public,
|
||||
key: '',
|
||||
order_field: :activity,
|
||||
order_dir: :desc,
|
||||
page_size: 10,
|
||||
page_id: 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}.freeze
|
||||
|
||||
#=============================================================================
|
||||
# Other
|
||||
#=============================================================================
|
||||
|
|
|
|||
|
|
@ -3,24 +3,18 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
|
||||
CONSTANTS = Constants::PROTOCOLS_IO_V3_API
|
||||
TOKEN = 'test_token'
|
||||
|
||||
describe '#protocol_list' do
|
||||
URL = 'https://www.protocols.io/api/v3/protocols'
|
||||
URL = "#{CONSTANTS[:base_uri]}protocols"
|
||||
|
||||
let(:stub_protocols) do
|
||||
stub_request(:get, URL).with(query: hash_including({}))
|
||||
end
|
||||
|
||||
let(:default_query_params) do
|
||||
{
|
||||
filter: :public,
|
||||
key: '',
|
||||
order_dir: :desc,
|
||||
order_field: :activity,
|
||||
page_id: 1,
|
||||
page_size: 10
|
||||
}
|
||||
CONSTANTS.dig(:endpoints, :protocols, :default_query_params)
|
||||
end
|
||||
|
||||
it 'returns 200 on successfull call' do
|
||||
|
|
@ -69,7 +63,7 @@ describe ProtocolImporters::ProtocolsIO::V3::ApiClient do
|
|||
|
||||
describe '#single_protocol' do
|
||||
PROTOCOL_ID = 15
|
||||
SINGLE_PROTOCOL_URL = "https://www.protocols.io/api/v3/protocols/#{PROTOCOL_ID}"
|
||||
SINGLE_PROTOCOL_URL = "#{CONSTANTS[:base_uri]}protocols/#{PROTOCOL_ID}"
|
||||
|
||||
let(:stub_single_protocol) do
|
||||
stub_request(:get, SINGLE_PROTOCOL_URL)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue