-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpublic_profile.go
More file actions
88 lines (75 loc) · 2.81 KB
/
public_profile.go
File metadata and controls
88 lines (75 loc) · 2.81 KB
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
package paymail
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)
var (
// ErrPublicProfileMissingAlias is returned when alias is missing
ErrPublicProfileMissingAlias = errors.New("missing alias")
// ErrPublicProfileMissingDomain is returned when domain is missing
ErrPublicProfileMissingDomain = errors.New("missing domain")
// ErrPublicProfileInvalidURL is returned when URL is invalid
ErrPublicProfileInvalidURL = errors.New("invalid url")
// ErrPublicProfileBadResponse is returned when paymail provider returns bad response
ErrPublicProfileBadResponse = errors.New("bad response from paymail provider")
)
/*
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("url %s: %w", publicProfileURL, ErrPublicProfileInvalidURL)
return response, err
}
// Basic requirements for request
if len(alias) == 0 {
err = ErrPublicProfileMissingAlias
return response, err
} else if len(domain) == 0 {
err = ErrPublicProfileMissingDomain
return response, err
}
// 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 response, err
}
// 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 response, err
}
err = fmt.Errorf("code %d, message: %s: %w", response.StatusCode, serverError.Message, ErrPublicProfileBadResponse)
return response, err
}
// Decode the body of the response
err = json.Unmarshal(resp.Body, &response)
return response, err
}