forgeplus/app/services/notice/write/client_service.rb

108 lines
2.4 KiB
Ruby
Raw Normal View History

2021-09-13 17:51:34 +08:00
class Notice::Write::ClientService < ApplicationService
2021-09-09 16:32:13 +08:00
attr_reader :url, :params
def initialize(options={})
@url = options[:url]
@params = options[:params]
end
def post(url, params={})
2021-09-13 17:51:34 +08:00
puts "[notice][write][POST] request params: #{params}"
2021-09-09 16:32:13 +08:00
conn.post do |req|
2021-09-13 17:51:34 +08:00
req.url full_url(url)
2021-09-09 16:32:13 +08:00
req.body = params[:data].to_json
end
end
def get(url, params={})
2021-09-13 17:51:34 +08:00
puts "[notice][write][GET] request params: #{params}"
2021-09-09 16:32:13 +08:00
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={})
2021-09-13 17:51:34 +08:00
puts "[notice][write][DELETE] request params: #{params}"
2021-09-09 16:32:13 +08:00
conn.delete do |req|
req.url full_url(url)
2021-09-13 17:51:34 +08:00
req.body = params[:data].to_json
2021-09-09 16:32:13 +08:00
end
end
def patch(url, params={})
2021-09-13 17:51:34 +08:00
puts "[notice][write][PATCH] request params: #{params}"
2021-09-09 16:32:13 +08:00
conn.patch do |req|
req.url full_url(url)
2021-09-13 17:51:34 +08:00
req.body = params[:data].to_json
2021-09-09 16:32:13 +08:00
end
end
def put(url, params={})
2021-09-13 17:51:34 +08:00
puts "[notice][write][PUT] request params: #{params}"
2021-09-09 16:32:13 +08:00
conn.put do |req|
req.url full_url(url)
2021-09-13 17:51:34 +08:00
req.body = params[:data].to_json
2021-09-09 16:32:13 +08:00
end
end
2021-09-22 14:25:56 +08:00
def platform
Notice.notice_config[:platform]
end
private
2021-09-13 17:51:34 +08:00
def conn
2021-09-09 16:32:13 +08:00
@client ||= begin
Faraday.new(url: domain) do |req|
req.request :url_encoded
req.headers['Content-Type'] = 'application/json'
req.adapter Faraday.default_adapter
end
end
@client
end
def base_url
Notice.notice_config[:base_url]
end
def domain
2021-09-13 17:51:34 +08:00
Notice.notice_config[:write_domain]
2021-09-09 16:32:13 +08:00
end
def api_url
2021-09-22 14:25:56 +08:00
[domain, base_url].join('')
2021-09-09 16:32:13 +08:00
end
def full_url(api_rest, action='post')
url = [api_url, api_rest].join('').freeze
url = action === 'get' ? url : URI.escape(url)
url = URI.escape(url) unless url.ascii_only?
2021-09-13 17:51:34 +08:00
puts "[notice][write] request url: #{url}"
2021-09-09 16:32:13 +08:00
return url
end
def log_error(status, body)
2021-09-13 17:51:34 +08:00
puts "[notice][write] status: #{status}"
puts "[notice][write] body: #{body}"
2021-09-09 16:32:13 +08:00
end
def render_response(response)
status = response.status
2021-09-13 17:51:34 +08:00
body = JSON.parse(response&.body)
2021-09-09 16:32:13 +08:00
log_error(status, body)
if status == 200
if body["code"] == 1
return [body["code"], body["message"], body["data"]]
else
2021-09-13 17:51:34 +08:00
puts "[notice][write][ERROR] code: #{body["code"]}"
puts "[notice][write][ERROR] message: #{body["message"]}"
2021-09-09 16:32:13 +08:00
end
end
end
end