feat: Return location info and Hijri date from the prayer times endpoint
This commit is contained in:
@@ -10,12 +10,12 @@ import (
|
||||
)
|
||||
|
||||
type APIProvider interface {
|
||||
Get(ctx context.Context, locationID string) ([]prayer.Times, error)
|
||||
GetByCoords(ctx context.Context, coords prayer.Coordinates) ([]prayer.Times, error)
|
||||
Get(ctx context.Context, locationID string) (prayer.TimesResult, error)
|
||||
GetByCoords(ctx context.Context, coords prayer.Coordinates) (prayer.TimesResult, error)
|
||||
}
|
||||
|
||||
type FallbackProvider interface {
|
||||
GetByCoords(ctx context.Context, coords prayer.Coordinates) ([]prayer.Times, error)
|
||||
GetByCoords(ctx context.Context, coords prayer.Coordinates) (prayer.TimesResult, error)
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
@@ -28,44 +28,44 @@ func New(api APIProvider, fallback FallbackProvider) Provider {
|
||||
return Provider{
|
||||
api: api,
|
||||
fallback: fallback,
|
||||
timeout: 2 * time.Second,
|
||||
timeout: 1 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
var ErrEmptyTimes = errors.New("diyanet did not return any prayer times")
|
||||
|
||||
func (p Provider) Get(ctx context.Context, locationID string) ([]prayer.Times, error) {
|
||||
func (p Provider) Get(ctx context.Context, locationID string) (prayer.TimesResult, error) {
|
||||
ctxWithTimeout, cancel := context.WithTimeout(ctx, p.timeout)
|
||||
defer cancel()
|
||||
|
||||
times, err := p.api.Get(ctxWithTimeout, locationID)
|
||||
result, err := p.api.Get(ctxWithTimeout, locationID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prayer times from api provider: %w", err)
|
||||
return prayer.TimesResult{}, fmt.Errorf("failed to get prayer times from api provider: %w", err)
|
||||
}
|
||||
if len(times) == 0 {
|
||||
return nil, ErrEmptyTimes
|
||||
if len(result.Times) == 0 {
|
||||
return prayer.TimesResult{}, ErrEmptyTimes
|
||||
}
|
||||
|
||||
return times, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p Provider) GetByCoords(ctx context.Context, coords prayer.Coordinates) ([]prayer.Times, error) {
|
||||
func (p Provider) GetByCoords(ctx context.Context, coords prayer.Coordinates) (prayer.TimesResult, error) {
|
||||
ctxWithTimeout, cancel := context.WithTimeout(ctx, p.timeout)
|
||||
defer cancel()
|
||||
|
||||
times, err := p.api.GetByCoords(ctxWithTimeout, coords)
|
||||
if err == nil && len(times) > 0 {
|
||||
return times, nil
|
||||
result, err := p.api.GetByCoords(ctxWithTimeout, coords)
|
||||
if err == nil && len(result.Times) > 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
times, err = p.fallback.GetByCoords(ctx, coords)
|
||||
result, err = p.fallback.GetByCoords(ctx, coords)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prayer times from fallback provider: %w", err)
|
||||
return prayer.TimesResult{}, fmt.Errorf("failed to get prayer times from fallback provider: %w", err)
|
||||
}
|
||||
|
||||
if len(times) == 0 {
|
||||
return nil, fmt.Errorf("fallback provider did not return any prayer times")
|
||||
if len(result.Times) == 0 {
|
||||
return prayer.TimesResult{}, fmt.Errorf("fallback provider did not return any prayer times")
|
||||
}
|
||||
|
||||
return times, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user