feat: Add Diyanet API provider
feat: Add UI
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package diyanetapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
|
||||
"prayertimes/pkg/prayer"
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
http *req.Client
|
||||
}
|
||||
|
||||
func New(c *req.Client) Provider {
|
||||
return Provider{
|
||||
http: c.Clone().SetCommonBasicAuth("diyanet", "Q6Y3vYt5F3x2txPaaMF3uPgbK99EJhpM"),
|
||||
}
|
||||
}
|
||||
|
||||
func (d Provider) GetByCoords(ctx context.Context, coords prayer.Coordinates) ([]prayer.Times, error) {
|
||||
res, err := d.http.NewRequest().
|
||||
SetContext(ctx).
|
||||
SetQueryParams(map[string]string{
|
||||
"latitude": fmt.Sprintf("%f", coords.Latitude),
|
||||
"longitude": fmt.Sprintf("%f", coords.Longitude),
|
||||
}).
|
||||
Get("https://namazvakti.diyanet.gov.tr/api/NamazVakti/Aylik")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prayer times by coords: %w", err)
|
||||
}
|
||||
|
||||
return d.parseResponse(res)
|
||||
}
|
||||
|
||||
func (d Provider) Get(ctx context.Context, locationID string) ([]prayer.Times, error) {
|
||||
res, err := d.http.NewRequest().
|
||||
SetContext(ctx).
|
||||
SetQueryParam("ilceId", locationID).
|
||||
Get("https://namazvakti.diyanet.gov.tr/api/NamazVakti/Aylik")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prayer times by location id: %w", err)
|
||||
}
|
||||
|
||||
return d.parseResponse(res)
|
||||
}
|
||||
|
||||
func (d Provider) parseResponse(res *req.Response) ([]prayer.Times, error) {
|
||||
var response struct {
|
||||
Success bool `json:"success"`
|
||||
ResultObject struct {
|
||||
Location struct {
|
||||
ID int `json:"konum_Id"`
|
||||
Timezone string `json:"timezone"`
|
||||
} `json:"konum"`
|
||||
PrayerTimes []struct {
|
||||
Date time.Time `json:"miladi_tarih_uzun_Iso8601"`
|
||||
Fajr time.Time `json:"imsak"`
|
||||
Sunrise time.Time `json:"gunes"`
|
||||
Dhuhr time.Time `json:"ogle"`
|
||||
Asr time.Time `json:"ikindi"`
|
||||
Maghrib time.Time `json:"aksam"`
|
||||
Isha time.Time `json:"yatsi"`
|
||||
} `json:"namazVakti"`
|
||||
} `json:"resultObject"`
|
||||
}
|
||||
if err := res.Unmarshal(&response); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal as json: %w", err)
|
||||
}
|
||||
if !response.Success {
|
||||
return nil, fmt.Errorf("received error: %s", res.String())
|
||||
}
|
||||
|
||||
var times []prayer.Times
|
||||
const format = "15:04"
|
||||
now := time.Now()
|
||||
for _, pt := range response.ResultObject.PrayerTimes {
|
||||
then := pt.Date.UTC().Truncate(time.Hour * 24)
|
||||
if then.Before(now) {
|
||||
continue
|
||||
}
|
||||
|
||||
times = append(times, prayer.Times{
|
||||
Date: then,
|
||||
Fajr: pt.Fajr.Format(format),
|
||||
Sunrise: pt.Sunrise.Format(format),
|
||||
Dhuhr: pt.Dhuhr.Format(format),
|
||||
Asr: pt.Asr.Format(format),
|
||||
Maghrib: pt.Maghrib.Format(format),
|
||||
Isha: pt.Isha.Format(format),
|
||||
})
|
||||
}
|
||||
return times, nil
|
||||
}
|
||||
|
||||
func (d Provider) Name() string {
|
||||
return "diyanet"
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package diyanetapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"prayertimes/internal/net"
|
||||
"prayertimes/pkg/prayer"
|
||||
)
|
||||
|
||||
func TestDiyanetAPI_GetByCoords(t *testing.T) {
|
||||
p := New(net.ReqClient)
|
||||
t.Run("by coords", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
times, err := p.GetByCoords(context.Background(), prayer.Coordinates{
|
||||
Latitude: 52.5100846,
|
||||
Longitude: 13.4518284,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, times)
|
||||
t.Logf("%#+v", times[0])
|
||||
})
|
||||
|
||||
t.Run("by id", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
times, err := p.Get(context.Background(), "11104")
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, times)
|
||||
t.Logf("%#+v", times[0])
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user