-
Notifications
You must be signed in to change notification settings - Fork 9
/
quran_strategy.go
65 lines (54 loc) · 1.49 KB
/
quran_strategy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package strategies
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
type QuranStrategy struct{}
type QuranCOMAyah struct {
Tafsir QuranCOMTafsir `json:"tafsir"`
}
type QuranCOMTafsir struct {
ResourceID int64 `json:"resource_id"`
ResourceName string `json:"resource_name"`
LanguageID int64 `json:"language_id"`
Slug string `json:"slug"`
Text string `json:"text"`
}
func (q *QuranStrategy) GetAyah(ayahNumber int, surahNumber int, tafsirNumber int) (string, error) {
quranUrl := "https://api.quran.com/api/v4"
ayahUrl := fmt.Sprintf("%s/tafsir/%d/by_ayah/%d:%d", quranUrl, tafsirNumber, surahNumber, ayahNumber)
// Make an HTTP GET request to fetch the ayah text
response, err := http.Get(ayahUrl)
if err != nil {
return "", err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(response.Body)
// Check if the response status code is not OK (200)
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP request failed with status code: %d", response.StatusCode)
}
// Read the response body
body, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
// Parse the response JSON
var quranCOMAyah QuranCOMAyah
if err := json.Unmarshal(body, &quranCOMAyah); err != nil {
return "", err
}
// Extract and return the ayah text
ayahText := quranCOMAyah.Tafsir.Text
return ayahText, nil
}
// Name returns the name of this strategy.
func (q *QuranStrategy) Name() string {
return "quran"
}