-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC(api_key): cache api key and organization
- Loading branch information
1 parent
ae937fc
commit d262623
Showing
8 changed files
with
79 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module ApiKeys | ||
class FetchService < BaseService | ||
Result = BaseResult[:api_key, :organization] | ||
|
||
def initialize(auth_token) | ||
@auth_token = auth_token | ||
super | ||
end | ||
|
||
def call | ||
api_key_json = Rails.cache.read("api_key/#{auth_token}") | ||
if api_key_json | ||
data = JSON.parse(api_key_json) | ||
api_key = ApiKey.instantiate(data["api_key"].slice(*ApiKey.column_names)) | ||
|
||
unless api_key.expired? | ||
result.organization = Organization.instantiate(data["organization"].slice(*Organization.column_names)) | ||
result.api_key = api_key | ||
return result | ||
end | ||
end | ||
|
||
api_key = ApiKey.includes(:organization).find_by(value: auth_token) | ||
write_to_cache(api_key) if api_key | ||
|
||
result.api_key = api_key | ||
result.organization = api_key&.organization | ||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :auth_token | ||
|
||
def write_to_cache(api_key) | ||
expiration = if api_key.expires_at | ||
(api_key.expires_at - Time.current).to_i.seconds | ||
else | ||
1.hour | ||
end | ||
|
||
Rails.cache.write( | ||
"api_key/#{auth_token}", | ||
{ | ||
organization: api_key.organization.attributes, | ||
api_key: api_key.attributes | ||
}.to_json, | ||
expires_in: expiration | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters