-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
63 lines (57 loc) · 1.43 KB
/
utils.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
package main
import (
"encoding/json"
"errors"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"io/ioutil"
"net/http"
"os"
)
//Create the InlineKeyboard
func newInlineKeyboard(data data) tgbotapi.InlineKeyboardMarkup {
MarshalData, _ := json.Marshal(data)
return tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("More Like this", string(MarshalData)),
),
)
}
//New Request
func newPhotoRequest(d data) *http.Request {
req, err := http.NewRequest("GET", "https://api.unsplash.com/photos/random", nil)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
q := req.URL.Query()
q.Add("client_id", os.Getenv("CLIENT_ID"))
q.Add("count", "1")
if !d.Random {
q.Add("query",d.Query)
}
req.URL.RawQuery = q.Encode()
return req
}
//Fetch Response Object
func getResponse(req *http.Request) ([]response, error) {
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return []response{}, errors.New("request failed")
}
data, err := ioutil.ReadAll(res.Body)
if string(data)=="Rate Limit Exceeded" {
return []response{}, errors.New("rate limit exceeded please try after sometime")
}
if err != nil {
return []response{}, errors.New("couldn't read response")
}
defer res.Body.Close()
var parsedRes []response
err = json.Unmarshal(data,&parsedRes)
if err != nil {
return []response{}, errors.New("couldn't find a image")
}
return parsedRes, nil
}