新增:接口service构建

This commit is contained in:
yystopf 2024-04-13 21:09:08 +08:00
parent ad9345badb
commit 04e70b3b34
12 changed files with 359 additions and 0 deletions

View File

@ -0,0 +1,97 @@
class Reposync::ClientService < ApplicationService
attr_reader :url, :params
def initialize(options={})
@url = options[:url]
@params = options[:params]
end
def post(url, params={})
puts "[reposync][POST] request params: #{params}"
conn.post do |req|
req.url full_url(url)
req.body = params[:data].to_json
end
end
def get(url, params={})
puts "[reposync][GET] request params: #{params}"
conn.get do |req|
req.url full_url(url, 'get')
params.each_pair do |key, value|
req.params["#{key}"] = value
end
end
end
def delete(url, params={})
puts "[reposync][DELETE] request params: #{params}"
conn.delete do |req|
req.url full_url(url)
req.body = params[:data].to_json
end
end
def patch(url, params={})
puts "[reposync][PATCH] request params: #{params}"
conn.patch do |req|
req.url full_url(url)
req.body = params[:data].to_json
end
end
def put(url, params={})
puts "[reposync][PUT] request params: #{params}"
conn.put do |req|
req.url full_url(url)
req.body = params[:data].to_json
end
end
def conn
@client ||= begin
Faraday.new(url: domain) do |req|
req.request :url_encoded
req.headers['Content-Type'] = 'application/json'
req.adapter Faraday.default_adapter
req.options.timeout = 100 # open/read timeout in seconds
req.options.open_timeout = 10 # connection open timeout in seconds
end
end
@client
end
def domain
EduSetting.get("reposync_api_domain") || "http://106.75.110.152:50087"
end
def full_url(api_rest, action='post')
url = [domain, api_rest].join('').freeze
url = action === 'get' ? url : URI.escape(url)
url = URI.escape(url) unless url.ascii_only?
puts "[reposync] request url: #{url}"
return url
end
def log_error(status, body)
puts "[reposync] status: #{status}"
puts "[reposync] body: #{body}"
end
def render_response(response)
status = response.status
body = JSON.parse(response&.body)
log_error(status, body)
if status == 200
if body["code_status"].to_i == 0
return [body["code_status"], body["data"], body["msg"]]
else
puts "[reposync][ERROR] code: #{body["code_status"]}"
puts "[reposync][ERROR] message: #{body["msg"]}"
end
end
end
end

View File

@ -0,0 +1,30 @@
class Reposync::CreateSyncBranchService < Reposync::ClientService
attr_accessor :repo_name, :internal_branch_name, :external_branch_name, :enable
def initialize(repo_name, internal_branch_name, external_branch_name, enable=true)
@repo_name = repo_name
@internal_branch_name = internal_branch_name
@external_branch_name = external_branch_name
@enable = enable
end
def call
result = post(url, request_params)
response = render_response(result)
end
private
def request_params
Hash.new.merge(data: {
internal_branch_name: internal_branch_name,
external_branch_name: external_branch_name,
enable: enable
}.stringify_keys)
end
def url
"/cerobot/sync/#{repo_name}/branch".freeze
end
end

View File

@ -0,0 +1,34 @@
class Reposync::CreateSyncRepoService < Reposync::ClientService
attr_accessor :repo_name, :internal_repo_address, :external_repo_address, :sync_granularity, :sync_direction, :enable
def initialize(repo_name, internal_repo_address, external_repo_address, sync_granularity, sync_direction, enable=true)
@repo_name = repo_name
@internal_repo_address = internal_repo_address
@external_repo_address = external_repo_address
@sync_granularity = sync_granularity
@sync_direction = sync_direction
@enable = enable
end
def call
result = post(url, request_params)
response = render_response(result)
end
private
def request_params
Hash.new.merge(data: {
repo_name: repo_name,
enable: enable,
internal_repo_address: internal_repo_address,
external_repo_address: external_repo_address,
sync_granularity: sync_granularity,
sync_direction: sync_direction
}.stringify_keys)
end
def url
"/cerobot/sync/repo".freeze
end
end

View File

@ -0,0 +1,19 @@
class Reposync::DeleteBranchService < Reposync::ClientService
attr_accessor :repo_name, :branch_name
def initialize(repo_name, branch_name)
@repo_name = repo_name
@branch_name = branch_name
end
def call
result = delete(url)
response = render_response(result)
end
private
def url
"/cerobot/sync/#{repo_name}/branch/#{branch_name}"
end
end

View File

@ -0,0 +1,18 @@
class Reposync::DeleteRepoService < Reposync::ClientService
attr_accessor :repo_name
def initialize(repo_name)
@repo_name = repo_name
end
def call
result = delete(url)
response = render_response(result)
end
private
def url
"/cerobot/sync/repo/#{repo_name}"
end
end

View File

@ -0,0 +1,23 @@
class Reposync::GetLogsService < Reposync::ClientService
attr_accessor :repo_name, :branch_id
def initialize(repo_name, branch_id=nil)
@repo_name = repo_name
@branch_id = branch_id
end
def call
result = get(url, request_params)
response = render_response(result)
end
private
def request_params
branch_id.present? ? {branch_id: branch_id}.stringify_keys : {}
end
def url
"/cerobot/sync/repo/#{repo_name}/logs"
end
end

View File

@ -0,0 +1,30 @@
class Reposync::GetSyncBranchesService < Reposync::ClientService
attr_accessor :repo_name, :page, :limit, :create_sort
def initialize(repo_name, page=1, limit=10, create_sort=false)
@repo_name = repo_name
@page = page
@limit = limit
@create_sort = create_sort
end
def call
result = get(url, request_params)
response = render_response(result)
end
private
def request_params
{
page: page,
limit: limit,
create_sort: create_sort
}.stringify_keys
end
def url
"/cerobot/sync/#{repo_name}/branch".freeze
end
end

View File

@ -0,0 +1,28 @@
class Reposync::GetSyncReposService < Reposync::ClientService
attr_accessor :page, :limit, :create_sort
def initialize(page=1, limit=10, create_sort=false)
@page = page
@limit = limit
@create_sort = create_sort
end
def call
result = get(url, request_params)
response = render_response(result)
end
private
def request_params
{
page: page,
limit: limit,
create_sort: create_sort
}.stringify_keys
end
def url
"/cerobot/sync/repo".freeze
end
end

View File

@ -0,0 +1,22 @@
class Reposync::SyncBranchService < Reposync::ClientService
attr_accessor :repo_name, :branch_name, :sync_direct
def initialize(repo_name, branch_name, sync_direct)
@repo_name = repo_name
@branch_name = branch_name
@sync_direct = sync_direct
end
def call
result = post(url)
response = render_response(result)
end
private
def url
"/cerobot/sync/#{repo_name}/branch/#{branch_name}?sync_direct=#{sync_direct}"
end
end

View File

@ -0,0 +1,18 @@
class Reposync::SyncRepoService < Reposync::ClientService
attr_accessor :repo_name
def initialize(repo_name)
@repo_name = repo_name
end
def call
result = post(url)
response = render_response(result)
end
private
def url
"/cerobot/sync/repo/#{repo_name}"
end
end

View File

@ -0,0 +1,21 @@
class Reposync::UpdateBranchStatusService < Reposync::ClientService
attr_accessor :repo_name, :branch_name, :enable
def initialize(repo_name, branch_name, enable)
@repo_name = repo_name
@branch_name = branch_name
@enable = enable
end
def call
result = put(url)
response = render_response(result)
end
private
def url
"/cerobot/sync/#{repo_name}/branch/#{branch_name}?enable=#{enable}"
end
end

View File

@ -0,0 +1,19 @@
class Reposync::UpdateRepoStatusService < Reposync::ClientService
attr_accessor :repo_name, :enable
def initialize(repo_name, enable)
@repo_name = repo_name
@enable = enable
end
def call
result = put(url)
response = render_response(result)
end
private
def url
"/cerobot/sync/repo/#{repo_name}?enable=#{enable}".freeze
end
end