ADD educode repo api
This commit is contained in:
parent
0192fa9f85
commit
1feb238e80
|
@ -0,0 +1,112 @@
|
|||
class Educoder::ClientService < ApplicationService
|
||||
attr_reader :url, :params
|
||||
|
||||
PAGINATE_DEFAULT_PAGE = 1
|
||||
PAGINATE_DEFAULT_LIMIT = 20
|
||||
|
||||
def initialize(options={})
|
||||
@url = options[:url]
|
||||
@params = options[:params]
|
||||
end
|
||||
|
||||
def post(url, params={})
|
||||
puts "[educoder] request params: #{params}"
|
||||
auth_token = authen_params(params[:token])
|
||||
response = conn(auth_token).post do |req|
|
||||
req.url full_url(url)
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
render_status(response)
|
||||
end
|
||||
|
||||
def get(url, params={})
|
||||
puts "[educoder] params: #{params}"
|
||||
conn(api_url(url), params)
|
||||
end
|
||||
|
||||
def delete(url, params={})
|
||||
auth_token = authen_params(params[:token])
|
||||
conn(auth_token).delete do |req|
|
||||
req.url full_url(url)
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
def patch(url, params={})
|
||||
puts "[educoder] request params: #{params}"
|
||||
auth_token = authen_params(params[:token])
|
||||
conn(auth_token).patch do |req|
|
||||
req.url full_url(url)
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
def put(url, params={})
|
||||
puts "[educoder] put request params: #{params}"
|
||||
conn(authen_params(params[:token])).put do |req|
|
||||
req.url full_url(url)
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def conn(url, hash={})
|
||||
par = []
|
||||
hash.each do |k,v|
|
||||
par << "#{k}=#{v}"
|
||||
end
|
||||
|
||||
uri = URI("#{url}.json?#{par.join('&')}&private_token=#{private_token}")
|
||||
puts "[educoder] request_url: #{uri}"
|
||||
response = Net::HTTP.get_response(uri)
|
||||
puts "[educoder] response code: #{response.code.to_i}"
|
||||
if response.code.to_i != 200
|
||||
puts "======= 接口请求失败!"
|
||||
raise '接口请求失败.'
|
||||
return nil
|
||||
end
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
|
||||
def base_url
|
||||
Rails.application.config_for(:configuration)['educoder']['base_url']
|
||||
end
|
||||
|
||||
def domain
|
||||
Rails.application.config_for(:configuration)['educoder']['main_site']
|
||||
end
|
||||
|
||||
def private_token
|
||||
Rails.application.config_for(:configuration)['educoder']['token']
|
||||
end
|
||||
|
||||
def access_key_secret
|
||||
Gitea.gitea_config[:access_key_secret]
|
||||
end
|
||||
|
||||
def api_url(url)
|
||||
[domain, base_url, url].join('/')
|
||||
end
|
||||
|
||||
def full_url(api_rest, action='post')
|
||||
url = [api_url, api_rest].join('').freeze
|
||||
url = action === 'get' ? url : URI.escape(url)
|
||||
puts "[gitea] request url: #{url}"
|
||||
return url
|
||||
end
|
||||
|
||||
|
||||
|
||||
def authen_params(token)
|
||||
(token.is_a? String) ? {token: token} : Hash(token)
|
||||
end
|
||||
|
||||
def render_data(response)
|
||||
case response.status
|
||||
when 201, 200
|
||||
JSON.parse(response.body)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,29 @@
|
|||
class Educoder::Repository::Commits::ListService < Educoder::ClientService
|
||||
attr_reader :repo_name, :args
|
||||
|
||||
# ref: The name of the commit/branch/tag. Default the repository’s default branch (usually master)
|
||||
# repo_name: the name of repository
|
||||
# Educoder::Repository::Commits::ListService.call('fessf/6hiwcb7o20200917174054')
|
||||
def initialize(repo_name, **args)
|
||||
@repo_name = repo_name
|
||||
@args = {ref: 'master'}.merge(args.compact)
|
||||
end
|
||||
|
||||
def call
|
||||
response = get(url, params)
|
||||
render_result(response)
|
||||
end
|
||||
|
||||
private
|
||||
def params
|
||||
@args.merge(repo_name: repo_name)
|
||||
end
|
||||
|
||||
def url
|
||||
"commits".freeze
|
||||
end
|
||||
|
||||
def render_result(response)
|
||||
response['commits'] || []
|
||||
end
|
||||
end
|
|
@ -0,0 +1,30 @@
|
|||
class Educoder::Repository::Entries::GetService < Educoder::ClientService
|
||||
attr_reader :repo_name, :filepath
|
||||
|
||||
# filepath: path of the dir, file, symlink or submodule in the repo
|
||||
# repo_name: the name of repository
|
||||
def initialize(repo_name, filepath)
|
||||
@repo_name = repo_name
|
||||
@filepath = filepath
|
||||
end
|
||||
|
||||
def call
|
||||
get(url, params)
|
||||
end
|
||||
|
||||
private
|
||||
def params
|
||||
Hash.new.merge(repo_name: repo_name, path: filepath)
|
||||
end
|
||||
|
||||
def url
|
||||
"file_content".freeze
|
||||
end
|
||||
|
||||
def render_result(response)
|
||||
body = JSON.parse(response.body)
|
||||
if body['status'].to_i === -1
|
||||
raise '无权限访问.'
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,39 @@
|
|||
class Educoder::Repository::Entries::ListService < Educoder::ClientService
|
||||
attr_reader :repo_name, :args
|
||||
|
||||
# ref: The name of the commit/branch/tag. Default the repository’s default branch (usually master)
|
||||
# repo_name: the name of repository
|
||||
# args:
|
||||
# {
|
||||
# "repo_name": "wmov43ez8/5xahe2t7nv20191022173304",
|
||||
# "path": 'src'
|
||||
# }
|
||||
# Educoder::Repository::Entries::ListService.new('wmov43ez8/5xahe2t7nv20191022173304', {path: 'src'}).call
|
||||
def initialize(repo_name, args={})
|
||||
@repo_name = repo_name
|
||||
@args = args.compact
|
||||
end
|
||||
|
||||
def call
|
||||
get(url, params)
|
||||
end
|
||||
|
||||
private
|
||||
def params
|
||||
@args.merge(repo_name: repo_name)
|
||||
end
|
||||
|
||||
def url
|
||||
"repository".freeze
|
||||
end
|
||||
|
||||
def render_result(response)
|
||||
body = JSON.parse(response.body)
|
||||
case response.status
|
||||
when 200
|
||||
body
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue