Skip to content

Commit 80ceb95

Browse files
committed
add tinify
1 parent 30632d4 commit 80ceb95

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

tinify/source.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tinify
22

33
import (
44
"errors"
5+
"fmt"
56
"io"
67
"io/ioutil"
78
"net/http"
@@ -48,7 +49,13 @@ func FromFile(path string) (s *Source, err error) {
4849
}
4950

5051
func FromBuffer(buf []byte) (s *Source, err error) {
51-
response, err := GetClient().Request(http.MethodPost, "/shrink", buf)
52+
ct := GetClient()
53+
if ct == nil {
54+
55+
return nil, fmt.Errorf("tinify client not initialized check api key")
56+
}
57+
58+
response, err := ct.Request(http.MethodPost, "/shrink", buf)
5259
if err != nil {
5360
return
5461
}
@@ -60,7 +67,12 @@ func FromBuffer(buf []byte) (s *Source, err error) {
6067
func FromUrl(url string) (s *Source, err error) {
6168
if len(url) == 0 {
6269
err = errors.New("url is required")
63-
return
70+
return nil, err
71+
}
72+
ct := GetClient()
73+
if ct == nil {
74+
75+
return nil, fmt.Errorf("tinify client not initialized check api key")
6476
}
6577

6678
body := map[string]interface{}{
@@ -69,13 +81,13 @@ func FromUrl(url string) (s *Source, err error) {
6981
},
7082
}
7183

72-
response, err := GetClient().Request(http.MethodPost, "/shrink", body)
84+
response, err := ct.Request(http.MethodPost, "/shrink", body)
7385
if err != nil {
74-
return
86+
return nil, err
7587
}
7688

7789
s, err = getSourceFromResponse(response)
78-
return
90+
return s, err
7991
}
8092

8193
func getSourceFromResponse(response *http.Response) (s *Source, err error) {
@@ -121,7 +133,12 @@ func (s *Source) toResult() (r *Result, err error) {
121133
// return
122134
// }
123135
//}
124-
response, err := GetClient().Request(http.MethodGet, s.url, s.commands)
136+
ct := GetClient()
137+
if ct == nil {
138+
139+
return nil, fmt.Errorf("tinify client not initialized check api key")
140+
}
141+
response, err := ct.Request(http.MethodGet, s.url, s.commands)
125142
if err != nil {
126143
return
127144
}

tinify/tinify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func GetClient() *Client {
2020

2121
if len(key) == 0 {
2222

23-
key = os.Getenv("TINIIFY_API_KEY")
23+
key = os.Getenv("TINIFY_API_KEY")
2424
}
2525

2626
if len(key) == 0 {

0 commit comments

Comments
 (0)