-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcollection.go
109 lines (90 loc) · 3.06 KB
/
collection.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package pexels
import (
"context"
"encoding/json"
"github.com/google/go-querystring/query"
"net/http"
"strings"
)
type CollectionResourceResponse struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Private bool `json:"private"`
MediaCount int `json:"media_count"`
PhotosCount int `json:"photos_count"`
VideosCount int `json:"videos_count"`
}
type CollectionsResponse struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
TotalResults int `json:"total_results"`
NextPage string `json:"next_page"`
PrevPage string `json:"prev_page"`
Collections []*CollectionResourceResponse `json:"collections"`
}
type CollectionParams struct {
// Default: 1
Page int `query:"page" url:"page,omitempty"`
// Default: 15, Max: 80
PerPage int `query:"per_page" url:"per_page,omitempty"`
}
// FeaturedCollections retrieves all featured collections on Pexels.
// https://www.pexels.com/api/documentation/#collections-featured
//
//encore:api public method=GET path=/collections/featured
func FeaturedCollections(ctx context.Context, queryParams *CollectionParams) (*CollectionsResponse, error) {
v, err := query.Values(queryParams)
if err != nil {
return nil, err
}
url := "https://api.pexels.com/v1/collections/featured?" + v.Encode()
var response *CollectionsResponse
if err = makeGetRequest(url, &response); err != nil {
return nil, err
}
return response, nil
}
// MyCollections returns all of your collections.
// https://www.pexels.com/api/documentation/#collections-all
//
//encore:api public method=GET path=/collections
func MyCollections(ctx context.Context, queryParams *CollectionParams) (*CollectionsResponse, error) {
v, err := query.Values(queryParams)
if err != nil {
return nil, err
}
url := "https://api.pexels.com/videos/popular?" + v.Encode()
var response *CollectionsResponse
if err = makeGetRequest(url, &response); err != nil {
return nil, err
}
return response, nil
}
type CollectionMediaParams struct {
// Default: 1
Page int `query:"page" url:"page,omitempty"`
// Default: 15, Max: 80
PerPage int `query:"per_page" url:"per_page,omitempty"`
// "photos" or "videos"
Type string `query:"type" url:"type,omitempty"`
}
// CollectionMedia returns all the media (photos and videos) within a single collection. You can filter to only receive
// photos or videos using the type parameter: /collection/gz8lwcj?page=2&per_page=10&type=photos
// https://www.pexels.com/api/documentation/#collections-media
//
//encore:api public raw method=GET path=/collection/:id
func CollectionMedia(w http.ResponseWriter, req *http.Request) {
id := strings.TrimPrefix(req.URL.Path, "/collection/")
q := req.URL.Query()
url := "https://api.pexels.com/v1/collections/" + id + "?" + q.Encode()
var response any
if err := makeGetRequest(url, &response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
data, _ := json.Marshal(response)
w.Write(data)
}