add: notice system api

This commit is contained in:
2021-09-13 17:51:34 +08:00
parent 877a20350d
commit 465a3a67a5
11 changed files with 365 additions and 102 deletions

View File

@@ -0,0 +1,29 @@
class Notice::Write::ChangeStatusService < Notice::Write::ClientService
attr_accessor :notification_ids, :receiver, :status
def initialize(notification_ids, receiver, status=2)
@notification_ids = notification_ids
@receiver = receiver
@status = status
end
def call
result = put("", request_params)
response = render_response(result)
end
private
def request_notification_ids
notification_ids.join(",")
end
def request_params
Hash.new.merge(data: {
notificationIds: request_notification_ids,
receiver: receiver,
status: status
}.stringify_keys)
end
end

View File

@@ -0,0 +1,108 @@
class Notice::Write::ClientService < ApplicationService
attr_reader :url, :params
def initialize(options={})
@url = options[:url]
@params = options[:params]
end
def post(url, params={})
puts "[notice][write][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 "[notice][write][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 "[notice][write][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 "[notice][write][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 "[notice][write][PUT] request params: #{params}"
conn.put do |req|
req.url full_url(url)
req.body = params[:data].to_json
end
end
#private
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
end
end
@client
end
def base_url
Notice.notice_config[:base_url]
end
def domain
Notice.notice_config[:write_domain]
end
def platform
Notice.notice_config[:platform]
end
def api_url
[domain, base_url, "/#{platform}"].join('')
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?
puts "[notice][write] request url: #{url}"
return url
end
def log_error(status, body)
puts "[notice][write] status: #{status}"
puts "[notice][write] 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"] == 1
return [body["code"], body["message"], body["data"]]
else
puts "[notice][write][ERROR] code: #{body["code"]}"
puts "[notice][write][ERROR] message: #{body["message"]}"
end
end
end
end

View File

@@ -0,0 +1,37 @@
class Notice::Write::CreateService < Notice::Write::ClientService
attr_accessor :receivers, :sender, :content, :notification_url, :source, :extra, :type
def initialize(receivers, content, notification_url, source, type=1, extra={},sender=-1)
@receivers = receivers
@sender = sender
@content = content
@notification_url = notification_url
@source = source
@extra = extra
@type = type
end
def call
result = post("", request_params)
response = render_response(result)
end
private
def request_receivers
receivers.join(",")
end
def request_params
Hash.new.merge(data: {
receivers: request_receivers,
sender: sender,
content: content,
notification_url: notification_url,
source: source,
extra: extra.to_json.to_s,
type: type
}.stringify_keys)
end
end

View File

@@ -0,0 +1,27 @@
class Notice::Write::DeleteService < Notice::Write::ClientService
attr_accessor :notification_ids, :receiver
def initialize(notification_ids, receiver)
@notification_ids = notification_ids
@receiver = receiver
end
def call
result = delete("", request_params)
response = render_response(result)
end
private
def request_notification_ids
notification_ids.join(",")
end
def request_params
Hash.new.merge(data: {
notificationIds: request_notification_ids,
receiver: receiver
}.stringify_keys)
end
end