-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement project detail fetch end-point
- Loading branch information
İsmail Akbudak
committed
Mar 1, 2018
1 parent
eb039a1
commit 4651bac
Showing
15 changed files
with
248 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'smartcat_sdk' | ||
|
||
ACCOUNT_ID = 'ACCOUNT_ID'.freeze | ||
API_KEY = 'API_KEY'.freeze | ||
|
||
# Project API end-point | ||
# ------------------------------------------------------------- | ||
project = SmartcatSDK::REST::Project.new(ACCOUNT_ID, API_KEY) | ||
# Get project details | ||
puts project.get('EXAMPLE_PROJECT_ID') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
require 'smartcat_sdk/version' | ||
require 'net/http' | ||
require 'net/https' | ||
require 'net/http/post/multipart' | ||
|
||
module SmartcatSdk | ||
# JSON parser | ||
require 'multi_json' | ||
|
||
# Smartcat files | ||
require 'smartcat_sdk/version' unless defined?(SmartcatSDK::VERSION) | ||
require 'smartcat_sdk/util/client_config' | ||
require 'smartcat_sdk/rest/errors' | ||
require 'smartcat_sdk/rest/project' | ||
|
||
module SmartcatSDK | ||
# Your code goes here... | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
module SmartcatSDK | ||
module REST | ||
class BaseRequest | ||
RUBY_INFO = "(#{RUBY_ENGINE}/#{RUBY_PLATFORM} #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})".freeze | ||
HTTP_HEADERS = { | ||
'Accept' => 'application/json', | ||
'Accept-Charset' => 'utf-8', | ||
'User-Agent' => "smartcat_sdk/#{SmartcatSDK::VERSION} #{RUBY_INFO}" | ||
}.freeze | ||
|
||
def initialize(*args) | ||
options = args.last.is_a?(Hash) ? args.pop : {} | ||
@config = SmartcatSDK::Util::ClientConfig.new options | ||
@user = args[0] || nil | ||
@password = args[1] || nil | ||
raise ArgumentError, 'Account ID is required' if @user.nil? | ||
raise ArgumentError, 'API key is required' if @password.nil? | ||
set_up_connection | ||
end | ||
|
||
protected | ||
|
||
## | ||
# Prepare http request | ||
# rubocop:disable Metrics/AbcSize | ||
# rubocop:disable Metrics/MethodLength | ||
def prepare_request(method, path, params: {}) | ||
request_path = @config.host | ||
request_path += "/api/integration/v1/#{path}" | ||
uri = URI.parse(request_path) | ||
uri.query = URI.encode_www_form(params) if %w[get delete].include?(method) | ||
method_class = Net::HTTP.const_get method.to_s.capitalize | ||
request = method_class.new(uri.to_s, HTTP_HEADERS) | ||
request.form_data = params if %w[post put].include?(method) | ||
request.basic_auth(@user, @password) | ||
connect_and_send(request) | ||
end | ||
|
||
private | ||
|
||
## | ||
# Set up and cache a Net::HTTP object to use when making requests. | ||
def set_up_connection | ||
uri = URI.parse(@config.host) | ||
@http = Net::HTTP.new( | ||
uri.host, | ||
uri.port, | ||
@config.proxy_addr, | ||
@config.proxy_port, | ||
@config.proxy_user, | ||
@config.proxy_pass | ||
) | ||
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
@http.use_ssl = @config.use_ssl | ||
if @config.ssl_verify_peer | ||
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER | ||
@http.ca_file = @config.ssl_ca_file | ||
else | ||
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
end | ||
@http.open_timeout = @config.timeout | ||
@http.read_timeout = @config.timeout | ||
end | ||
|
||
## | ||
# Send an HTTP request using the cached <tt>@http</tt> object and | ||
# return the JSON response body parsed into a hash. Also save the raw | ||
# Net::HTTP::Request and Net::HTTP::Response objects as | ||
# <tt>@last_request</tt> and <tt>@last_response</tt> to allow for | ||
# inspection later. | ||
def connect_and_send(request, is_file = false) | ||
@last_request = request | ||
retries_left = @config.retry_limit | ||
begin | ||
response = @http.request request | ||
@last_response = response | ||
raise SmartcatSDK::REST::ServerError if response.is_a?(Net::HTTPServerError) | ||
rescue StandardError => _error | ||
raise if request.class == Net::HTTP::Post | ||
raise if retries_left <= 0 | ||
retries_left -= 1 | ||
retry | ||
end | ||
handle_response_errors(handle_response(is_file, response), response) | ||
end | ||
|
||
def handle_response_errors(object, response) | ||
return object unless response.is_a?(Net::HTTPClientError) | ||
raise SmartcatSDK::REST::RequestError.new(object['error'], object['code']) | ||
end | ||
|
||
def handle_response(is_file, response) | ||
if response.body && !response.body.empty? | ||
return response.body if is_file | ||
MultiJson.load(response.body) | ||
elsif response.is_a?(Net::HTTPBadRequest) | ||
{ | ||
message: 'Bad request', | ||
code: 400 | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module SmartcatSDK | ||
module REST | ||
class ServerError < StandardError; end | ||
|
||
class RequestError < StandardError | ||
attr_reader :code | ||
|
||
def initialize(message, code=nil) | ||
super message | ||
@code = code | ||
end | ||
end | ||
|
||
class SDKError < StandardError | ||
attr_reader :code | ||
|
||
def initialize(message, code=nil) | ||
super message | ||
@code = code | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'smartcat_sdk/rest/base_request' | ||
|
||
module SmartcatSDK | ||
module REST | ||
class Project < BaseRequest | ||
|
||
def initialize(*args) | ||
super(*args) | ||
@resource = 'project' | ||
end | ||
|
||
def get(project_id) | ||
prepare_request(:get, "#{@resource}/#{project_id}") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module SmartcatSDK | ||
module Util | ||
class ClientConfig | ||
DEFAULTS = { | ||
host: 'https://smartcat.ai', | ||
port: 80, | ||
use_ssl: true, | ||
ssl_verify_peer: false, | ||
ssl_ca_file: File.dirname(__FILE__) + '/../../../conf/cert.pem', | ||
timeout: 30, | ||
proxy_addr: nil, | ||
proxy_port: nil, | ||
proxy_user: nil, | ||
proxy_pass: nil, | ||
retry_limit: 2 | ||
}.freeze | ||
|
||
DEFAULTS.each_key do |attribute| | ||
attr_accessor attribute | ||
end | ||
|
||
def initialize(opts={}) | ||
DEFAULTS.each do |attribute, value| | ||
send("#{attribute}=".to_sym, opts.fetch(attribute, value)) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module SmartcatSdk | ||
module SmartcatSDK | ||
VERSION = '0.1.0'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'smartcat_sdk/rest/base_request' | ||
|
||
RSpec.describe SmartcatSDK::REST::BaseRequest do | ||
context '#errors' do | ||
it 'valid arguments' do | ||
expect(described_class.new('account_id', 'api_key').is_a?(described_class)).to eq(true) | ||
end | ||
end | ||
|
||
context '#errors' do | ||
it 'mission api key error' do | ||
expect do | ||
described_class.new | ||
end.to raise_error(ArgumentError, 'Account ID is required') | ||
end | ||
|
||
it 'mission account id error' do | ||
expect do | ||
described_class.new('account_id') | ||
end.to raise_error(ArgumentError, 'API key is required') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
RSpec.describe SmartcatSDK do | ||
it 'has a version number' do | ||
expect(SmartcatSDK::VERSION).not_to be nil | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters