-
Notifications
You must be signed in to change notification settings - Fork 0
/
etsy_data_manager.go
122 lines (110 loc) · 4.1 KB
/
etsy_data_manager.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"net/http"
"os"
"strconv"
"github.com/dghubble/oauth1"
)
const (
etsyBaseURL = "https://openapi.etsy.com/v2/"
)
type etsyDataManager struct {
config oauth1.Config
requestSecret string
}
func newEtsyDataManager() *etsyDataManager {
edm := new(etsyDataManager)
edm.config = oauth1.Config{
ConsumerKey: os.Getenv("ETSY_CONSUMER_KEY"),
ConsumerSecret: os.Getenv("ETSY_CONSUMER_SECRET"),
CallbackURL: os.Getenv("HOST_URL") + "callback-etsy",
Endpoint: oauth1.Endpoint{
AccessTokenURL: etsyBaseURL + "oauth/access_token",
AuthorizeURL: "https://www.etsy.com/oauth/signin?oauth_consumer_key=" + os.Getenv("ETSY_CONSUMER_KEY") + "&service=v2_prod",
RequestTokenURL: etsyBaseURL + "oauth/request_token?scope=email_r%20listings_r%20transactions_r",
},
}
return edm
}
func (edm *etsyDataManager) getAuthorizationURL() string {
requestToken, requestSecret, err := edm.config.RequestToken()
if err != nil {
Error(err)
}
edm.requestSecret = requestSecret
authorizationURL, err := edm.config.AuthorizationURL(requestToken)
return authorizationURL.String() + "&oauth_token=" + requestToken
}
func (edm *etsyDataManager) getAndPopulateEtsyDetails(r *http.Request) (*userInfo, error) {
requestToken, verifier, err := oauth1.ParseAuthorizationCallback(r)
accessToken, accessSecret, err := edm.config.AccessToken(requestToken, edm.requestSecret, verifier)
if err != nil {
return nil, err
}
userInfo, err := edm.getUserProfileInfo(accessToken, accessSecret)
if err != nil {
return nil, err
}
return userInfo, nil
}
func (edm *etsyDataManager) getUserProfileInfo(accessToken string, accessSecret string) (*userInfo, error) {
path := etsyBaseURL + "users/__SELF__"
result := etsyProfileResponse{}
httpOAuthClient := newHTTPOAuthClient(accessToken, accessSecret, edm.config)
httpOAuthClient.getMarshalledAPIResponse(path, &result)
store := newDataStore()
info, err := store.getUserInfo(result.Results[0].EtsyUserID)
if err != nil || info == nil {
info = &userInfo{
EmailID: result.Results[0].EmailID,
UserID: result.Results[0].EtsyUserID,
EtsyDetails: etsyDetails{
EtsyAccessToken: accessToken,
EtsyAccessSecret: accessSecret,
},
CurrentStep: 1,
}
}
edm.getProfileDetails(info)
return info, nil
}
func (edm *etsyDataManager) getShops(info *userInfo) error {
path := etsyBaseURL + "/users/" + strconv.Itoa(info.UserID) + "/shops"
var result etsyShopResponse
httpOAuthClient := newHTTPOAuthClient(info.EtsyDetails.EtsyAccessToken,
info.EtsyDetails.EtsyAccessSecret, edm.config)
httpOAuthClient.getMarshalledAPIResponse(path, &result)
info.EtsyDetails.UserShopDetails = result.Results[0]
return nil
}
func (edm *etsyDataManager) getTransactionList(info userInfo) (*etsyTransactionResponse, error) {
path := etsyBaseURL + "shops/" + strconv.Itoa(info.EtsyDetails.UserShopDetails.ShopID) + "/transactions"
var result etsyTransactionResponse
httpOAuthClient := newHTTPOAuthClient(info.EtsyDetails.EtsyAccessToken,
info.EtsyDetails.EtsyAccessSecret, edm.config)
err := httpOAuthClient.getMarshalledAPIResponse(path, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (edm *etsyDataManager) getProfileDetails(info *userInfo) error {
path := etsyBaseURL + "users/" + strconv.Itoa(info.UserID) + "/profile"
var result etsyProfileResponse
httpOAuthClient := newHTTPOAuthClient(info.EtsyDetails.EtsyAccessToken,
info.EtsyDetails.EtsyAccessSecret, edm.config)
httpOAuthClient.getMarshalledAPIResponse(path, &result)
info.EtsyDetails.UserProfileURL = result.Results[0].UserProfileURL
info.EtsyDetails.UserName = result.Results[0].UserName
return nil
}
func (edm *etsyDataManager) getImageDetails(info *userInfo,
tranDetails etsyTransactionDetails) (etsyImageDetails, error) {
path := etsyBaseURL + "listings/" + strconv.Itoa(tranDetails.ListingID) +
"/images/" + strconv.Itoa(tranDetails.ImageListingID)
var result etsyImageResponse
httpOAuthClient := newHTTPOAuthClient(info.EtsyDetails.EtsyAccessToken,
info.EtsyDetails.EtsyAccessSecret, edm.config)
httpOAuthClient.getMarshalledAPIResponse(path, &result)
return result.Results[0], nil
}