Add Mastodon API service class, auth token config
Add a new REST API service class to keep things DRY
This commit is contained in:
parent
c43e43d89c
commit
f0cfde560b
@ -11,6 +11,9 @@ module Settings
|
||||
|
||||
field :mastodon_address_domain, type: :string,
|
||||
default: ENV["MASTODON_ADDRESS_DOMAIN"].presence || self.primary_domain
|
||||
|
||||
field :mastodon_auth_token, type: :string,
|
||||
default: ENV["MASTODON_AUTH_TOKEN"].presence
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,7 +1,7 @@
|
||||
#
|
||||
# API Docs: https://docs.btcpayserver.org/API/Greenfield/v1/
|
||||
#
|
||||
class BtcpayManagerService < ApplicationService
|
||||
class BtcpayManagerService < RestApiService
|
||||
private
|
||||
|
||||
def base_url
|
||||
@ -19,18 +19,4 @@ class BtcpayManagerService < ApplicationService
|
||||
"Authorization" => "token #{auth_token}"
|
||||
}
|
||||
end
|
||||
|
||||
def endpoint_url(path)
|
||||
"#{base_url}/#{path.gsub(/^\//, '')}"
|
||||
end
|
||||
|
||||
def get(path, params = {})
|
||||
res = Faraday.get endpoint_url(path), params, headers
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
|
||||
def post(path, payload)
|
||||
res = Faraday.post endpoint_url(path), payload.to_json, headers
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
end
|
||||
|
22
app/services/mastodon_manager_service.rb
Normal file
22
app/services/mastodon_manager_service.rb
Normal file
@ -0,0 +1,22 @@
|
||||
#
|
||||
# API Docs: https://docs.joinmastodon.org/methods/
|
||||
#
|
||||
class MastodonManagerService < RestApiService
|
||||
private
|
||||
|
||||
def base_url
|
||||
@base_url ||= "#{Setting.mastodon_public_url}/api"
|
||||
end
|
||||
|
||||
def auth_token
|
||||
@auth_token ||= Setting.mastodon_auth_token
|
||||
end
|
||||
|
||||
def headers
|
||||
{
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "application/json",
|
||||
"Authorization" => "Bearer #{auth_token}"
|
||||
}
|
||||
end
|
||||
end
|
27
app/services/rest_api_service.rb
Normal file
27
app/services/rest_api_service.rb
Normal file
@ -0,0 +1,27 @@
|
||||
class RestApiService < ApplicationService
|
||||
private
|
||||
|
||||
def base_url
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def headers
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def endpoint_url(path)
|
||||
"#{base_url}/#{path.gsub(/^\//, '')}"
|
||||
end
|
||||
|
||||
def get(path, params = {})
|
||||
res = Faraday.get endpoint_url(path), params, headers
|
||||
# TODO handle unsuccessful responses with no valid JSON body
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
|
||||
def post(path, payload)
|
||||
res = Faraday.post endpoint_url(path), payload.to_json, headers
|
||||
# TODO handle unsuccessful responses with no valid JSON body
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
end
|
@ -16,5 +16,10 @@
|
||||
key: :mastodon_address_domain,
|
||||
title: "User address domain"
|
||||
) %>
|
||||
<%= render FormElements::FieldsetResettableSettingComponent.new(
|
||||
key: :mastodon_auth_token,
|
||||
type: :password,
|
||||
title: "API auth token"
|
||||
) %>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
10
docs/dev/mastodon.md
Normal file
10
docs/dev/mastodon.md
Normal file
@ -0,0 +1,10 @@
|
||||
# Mastodon
|
||||
|
||||
## API access
|
||||
|
||||
(Optional)
|
||||
|
||||
Log in to your Mastodon instance with an admin account and create a new
|
||||
OAuth application:
|
||||
|
||||
https://kosmos.social/settings/applications/new
|
Loading…
x
Reference in New Issue
Block a user