mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
refactor(http): api key cache handling (#1632)
This commit is contained in:
parent
0d53f7e5fc
commit
d13b421c42
6 changed files with 117 additions and 50 deletions
|
@ -25,9 +25,8 @@ func NewAPIRepo(log logger.Logger, db *DB) domain.APIRepo {
|
|||
}
|
||||
|
||||
type APIRepo struct {
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
cache map[string]domain.APIKey
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
}
|
||||
|
||||
func (r *APIRepo) Store(ctx context.Context, key *domain.APIKey) error {
|
||||
|
@ -57,9 +56,7 @@ func (r *APIRepo) Store(ctx context.Context, key *domain.APIKey) error {
|
|||
}
|
||||
|
||||
func (r *APIRepo) Delete(ctx context.Context, key string) error {
|
||||
queryBuilder := r.db.squirrel.
|
||||
Delete("api_key").
|
||||
Where(sq.Eq{"key": key})
|
||||
queryBuilder := r.db.squirrel.Delete("api_key").Where(sq.Eq{"key": key})
|
||||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
|
@ -76,14 +73,9 @@ func (r *APIRepo) Delete(ctx context.Context, key string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (r *APIRepo) GetKeys(ctx context.Context) ([]domain.APIKey, error) {
|
||||
func (r *APIRepo) GetAllAPIKeys(ctx context.Context) ([]domain.APIKey, error) {
|
||||
queryBuilder := r.db.squirrel.
|
||||
Select(
|
||||
"name",
|
||||
"key",
|
||||
"scopes",
|
||||
"created_at",
|
||||
).
|
||||
Select("name", "key", "scopes", "created_at").
|
||||
From("api_key")
|
||||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
|
@ -116,3 +108,35 @@ func (r *APIRepo) GetKeys(ctx context.Context) ([]domain.APIKey, error) {
|
|||
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func (r *APIRepo) GetKey(ctx context.Context, key string) (*domain.APIKey, error) {
|
||||
queryBuilder := r.db.squirrel.
|
||||
Select("name", "key", "scopes", "created_at").
|
||||
From("api_key").
|
||||
Where(sq.Eq{"key": key})
|
||||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
row := r.db.handler.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, domain.ErrRecordNotFound
|
||||
}
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var apiKey domain.APIKey
|
||||
|
||||
var name sql.NullString
|
||||
|
||||
if err := row.Scan(&name, &apiKey.Key, pq.Array(&apiKey.Scopes), &apiKey.CreatedAt); err != nil {
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
apiKey.Name = name.String
|
||||
|
||||
return &apiKey, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue