Skip to content

Commit 79efd3e

Browse files
committed
added fetch api url
1 parent db45efb commit 79efd3e

File tree

5 files changed

+66
-39
lines changed

5 files changed

+66
-39
lines changed

src/app/apifetch.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package app
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/coderj001/go-wallheven/src"
8+
)
9+
10+
func FetchAPI(url string) (src.SearchList, error) {
11+
resp, err := http.Get(url)
12+
if err != nil {
13+
return src.SearchList{}, err
14+
}
15+
defer resp.Body.Close()
16+
17+
var searchList src.SearchList
18+
19+
err = json.NewDecoder(resp.Body).Decode(&searchList)
20+
if err != nil {
21+
return src.SearchList{}, err
22+
}
23+
24+
return searchList, nil
25+
}

src/app/downloader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package app

src/app/urlparser.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,6 @@ type error interface {
1111
Error() string
1212
}
1313

14-
type MetaStruct struct {
15-
CurrentPage int `json:"current_page"`
16-
LastPage int `json:"last_page"`
17-
PerPage int `json:"per_page"`
18-
Total int `json:"total"`
19-
}
20-
21-
type ThumbsStruct struct {
22-
Large string `json:"large"`
23-
Original string `json:"original"`
24-
Small string `json:"small"`
25-
}
26-
27-
type ImageInfo struct {
28-
Id string `json:"id"`
29-
Url string `json:"url"`
30-
ShortUrl string `json:"short_url"`
31-
Views int `json:"views"`
32-
Favorites int `json:"favorites"`
33-
Source string `json:"source"`
34-
Purity string `json:"purity"`
35-
Category string `json:"category"`
36-
DimensionX int `json:"dimension_x"`
37-
DimensionY int `json:"dimension_y"`
38-
Resolution string `json:"resolution"`
39-
Ratio float64 `json:"ratio"`
40-
FileSize int64 `json:"file_size"`
41-
FileType string `json:"file_type"`
42-
CreatedAt string `json:"created_at"`
43-
Colors []string
44-
Path string `json:"path"`
45-
Thumbs ThumbsStruct `json:"thumbs"`
46-
}
47-
48-
type SearchList struct {
49-
Data []ImageInfo
50-
Meta MetaStruct
51-
}
52-
5314
type Resolution struct {
5415
Width int
5516
Height int

src/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var rootCmd = &cobra.Command{
2828
Run: func(cmd *cobra.Command, args []string) {
2929
url, _ := app.GetFullURL(page, categories, purity, sorting, colors, query)
3030
fmt.Println(url)
31+
fmt.Println(app.FetchAPI(url))
3132
},
3233
}
3334

src/config.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,45 @@ var SORTING = map[string]string{
4646
"hot": "hot",
4747
}
4848

49+
type MetaStruct struct {
50+
CurrentPage int `json:"current_page"`
51+
LastPage int `json:"last_page"`
52+
PerPage string `json:"per_page"`
53+
Total int `json:"total"`
54+
}
55+
56+
type ThumbsStruct struct {
57+
Large string `json:"large"`
58+
Original string `json:"original"`
59+
Small string `json:"small"`
60+
}
61+
62+
type ImageInfo struct {
63+
Id string `json:"id"`
64+
Url string `json:"url"`
65+
ShortUrl string `json:"short_url"`
66+
Views int `json:"views"`
67+
Favorites int `json:"favorites"`
68+
Source string `json:"source"`
69+
Purity string `json:"purity"`
70+
Category string `json:"category"`
71+
DimensionX int `json:"dimension_x"`
72+
DimensionY int `json:"dimension_y"`
73+
Resolution string `json:"resolution"`
74+
Ratio string `json:"ratio"`
75+
FileSize int64 `json:"file_size"`
76+
FileType string `json:"file_type"`
77+
CreatedAt string `json:"created_at"`
78+
Colors []string
79+
Path string `json:"path"`
80+
Thumbs ThumbsStruct `json:"thumbs"`
81+
}
82+
83+
type SearchList struct {
84+
Data []ImageInfo
85+
Meta MetaStruct
86+
}
87+
4988
func init() {
5089
err := godotenv.Load()
5190
if err != nil {

0 commit comments

Comments
 (0)