-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
public_profile.go
77 lines (64 loc) · 2.15 KB
/
public_profile.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
package paymail
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
/*
Default:
{
"avatar": "https://<domain><image>",
"name": "<name>"
}
*/
// PublicProfileResponse is the result returned from GetPublicProfile()
type PublicProfileResponse struct {
StandardResponse
PublicProfilePayload
}
// PublicProfilePayload is the payload from the response
type PublicProfilePayload struct {
Avatar string `json:"avatar"` // A URL that returns a 180x180 image. It can accept an optional parameter `s` to return an image of width and height `s`. The image should be JPEG, PNG, or GIF.
Name string `json:"name"` // A string up to 100 characters long. (name or nickname)
}
// GetPublicProfile will return a valid public profile
//
// Specs: https://github.com/bitcoin-sv-specs/brfc-paymail/pull/7/files
func (c *Client) GetPublicProfile(publicProfileURL, alias, domain string) (response *PublicProfileResponse, err error) {
// Require a valid url
if len(publicProfileURL) == 0 || !strings.Contains(publicProfileURL, "https://") {
err = fmt.Errorf("invalid url: %s", publicProfileURL)
return
}
// Basic requirements for request
if len(alias) == 0 {
err = fmt.Errorf("missing alias")
return
} else if len(domain) == 0 {
err = fmt.Errorf("missing domain")
return
}
// Set the base url and path, assuming the url is from the prior GetCapabilities() request
// https://<host-discovery-target>/public-profile/{alias}@{domain.tld}
reqURL := replaceAliasDomain(publicProfileURL, alias, domain)
// Fire the GET request
var resp StandardResponse
if resp, err = c.getRequest(reqURL); err != nil {
return
}
// Start the response
response = &PublicProfileResponse{StandardResponse: resp}
// Test the status code (200 or 304 is valid)
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotModified {
serverError := &ServerError{}
if err = json.Unmarshal(resp.Body, serverError); err != nil {
return
}
err = fmt.Errorf("bad response from paymail provider: code %d, message: %s", response.StatusCode, serverError.Message)
return
}
// Decode the body of the response
err = json.Unmarshal(resp.Body, &response)
return
}