-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
profile_image.go
42 lines (36 loc) · 1021 Bytes
/
profile_image.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
package faker
import (
"fmt"
"io"
"log"
"os"
)
const (
profileImageBaseURL = "https://randomuser.me"
portraitsEndpoint = "api/portraits"
)
// ProfileImage is a faker struct for ProfileImage
type ProfileImage struct {
faker *Faker
HTTPClient HTTPClient
TempFileCreator TempFileCreator
}
// Image generates a *os.File with a random profile image using the thispersondoesnotexist.com service
func (pi ProfileImage) Image() *os.File {
gender := pi.faker.RandomStringElement([]string{"women", "men"})
profileId := pi.faker.IntBetween(1, 99)
url := fmt.Sprintf("%s/%s/%s/%d.jpg", profileImageBaseURL, portraitsEndpoint, gender, profileId)
resp, err := pi.HTTPClient.Get(url)
if err != nil {
log.Println("Error while requesting", profileImageBaseURL, ":", err)
panic(err)
}
f, err := pi.TempFileCreator.TempFile("profile-picture-img-*.jpg")
if err != nil {
log.Println("Error while creating a temp file:", err)
panic(err)
}
io.Copy(f, resp.Body)
resp.Body.Close()
return f
}