Skip to content

Commit

Permalink
Merge pull request #3 from mycolorway/dingnan/http_proxy
Browse files Browse the repository at this point in the history
ADD: 接口调用支持 HTTP 代理
  • Loading branch information
cod7ce authored Jun 9, 2023
2 parents e6fe1a4 + 1fea192 commit 9edc86d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/ones/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ class Api
api_mount 'wiki/space'
api_mount 'wiki/page'

attr_reader :client_id, :client_secret, :mode, :options
attr_reader :client_id, :client_secret, :mode, :options, :http_proxy

def initialize(options = {})
@client_id = options.delete(:client_id) || Ones.client_id
@client_secret = options.delete(:client_secret) || Ones.client_secret
@mode = options.delete(:mode) || :app_center
@http_proxy = options.delete(:http_proxy) || Ones.http_proxy
@options = options
end

def request
@request ||= "Ones::Requests::#{@mode.to_s.camelize}Request".constantize.new(self)
@request ||= "Ones::Requests::#{@mode.to_s.camelize}Request".constantize.new(self, http_proxy: http_proxy)
end

def get(path, headers = {})
Expand Down
12 changes: 11 additions & 1 deletion lib/ones/config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'logger'
require 'uri'

module Ones
class << self
Expand Down Expand Up @@ -45,9 +46,18 @@ def app_center_base_url

@app_center_base_url = config.app_center_base_url.presence || raise(AppNotConfigError)
end

def http_proxy
return @http_proxy if defined?(@http_proxy)

@http_proxy = if config.http_proxy_uri.present?
uri = URI.parse(config.http_proxy_uri)
{ host: uri.host, port: uri.port, user: uri.user, password: uri.password }.compact
end
end
end

class Config
attr_accessor :default_client_id, :default_client_secret, :app_center_base_url, :api_base_url, :http_timeout_options, :logger
attr_accessor :default_client_id, :default_client_secret, :app_center_base_url, :api_base_url, :http_timeout_options, :logger, :http_proxy_uri
end
end
4 changes: 2 additions & 2 deletions lib/ones/requests/app_center_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
module Ones
module Requests
class AppCenterRequest < Base
def initialize(client, skip_verify_ssl = true)
super(client, skip_verify_ssl)
def initialize(client, skip_verify_ssl: true, http_proxy: nil)
super(client, skip_verify_ssl: skip_verify_ssl, http_proxy: http_proxy)
@http = @http.basic_auth(user: client.client_id, pass: client.client_secret)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/ones/requests/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class Base

attr_reader :client, :ssl_context, :http

def initialize(client, skip_verify_ssl = true)
def initialize(client, skip_verify_ssl: true, http_proxy: nil)
@client = client
@http = HTTP.timeout(**Ones.http_timeout_options)
@http = @http.via(*http_proxy.values) if http_proxy.is_a?(Hash)
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE if skip_verify_ssl
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ones/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ones
VERSION = '1.2.0'.freeze
VERSION = '1.2.1'.freeze
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
config.api_base_url = 'http://localhost:3002/project/api/'

config.http_timeout_options = { write: 2, connect: 5, read: 15 }
config.http_proxy_uri = 'https://username:[email protected]:8080'
end

$ones_api = Ones::Api.new(client_id: :user_id, client_secret: :user_token, mode: :api)

0 comments on commit 9edc86d

Please sign in to comment.