@@ -8,44 +8,44 @@ import (
8
8
"io"
9
9
"net/http"
10
10
"time"
11
- )
12
11
13
- const (
14
- userAgentFormat = "mworzala/mc/%s"
12
+ "github.com/mworzala/mc/internal/pkg/util"
15
13
)
16
14
17
15
var (
18
- mojangApiUrl = "https://api.mojang.com/ "
19
- sessionserverUrl = "https://sessionserver.mojang.com/ "
20
- servicesApiUrl = "https://api.minecraftservices.com/ "
21
- profileApiUrl = servicesApiUrl + "minecraft/profile"
16
+ mojangApiUrl = "https://api.mojang.com"
17
+ sessionserverUrl = "https://sessionserver.mojang.com"
18
+ servicesApiUrl = "https://api.minecraftservices.com"
19
+ profileApiUrl = servicesApiUrl + "/ minecraft/profile"
22
20
)
23
21
24
22
type Client struct {
25
- baseUrl string
26
- userAgent string
27
- httpClient * http.Client
28
- timeout time.Duration
23
+ baseUrl string
24
+ userAgent string
25
+ accountToken string
26
+ httpClient * http.Client
27
+ timeout time.Duration
29
28
}
30
29
31
- func NewProfileClient (idVersion string ) * Client {
30
+ func NewProfileClient (idVersion string , accountToken string ) * Client {
32
31
return & Client {
33
- baseUrl : profileApiUrl ,
34
- userAgent : fmt .Sprintf (userAgentFormat , idVersion ),
35
- httpClient : http .DefaultClient ,
36
- timeout : 10 * time .Second ,
32
+ baseUrl : profileApiUrl ,
33
+ userAgent : util .MakeUserAgent (idVersion ),
34
+ accountToken : accountToken ,
35
+ httpClient : http .DefaultClient ,
36
+ timeout : 10 * time .Second ,
37
37
}
38
38
}
39
39
40
- func get [T any ](c * Client , ctx context.Context , endpoint string , headers http.Header ) (* T , error ) {
40
+ func do [T any ](c * Client , ctx context.Context , method string , url string , headers http.Header , body io. Reader ) (* T , error ) {
41
41
ctx , cancel := context .WithTimeout (ctx , c .timeout )
42
42
defer cancel ()
43
43
44
- fullUrl := fmt .Sprintf ("%s%s" , c .baseUrl , endpoint )
45
- req , err := http .NewRequestWithContext (ctx , http .MethodGet , fullUrl , nil )
44
+ req , err := http .NewRequestWithContext (ctx , method , url , body )
46
45
if err != nil {
47
46
return nil , err
48
47
}
48
+
49
49
req .Header = headers
50
50
req .Header .Set ("User-Agent" , c .userAgent )
51
51
@@ -61,6 +61,12 @@ func get[T any](c *Client, ctx context.Context, endpoint string, headers http.He
61
61
return nil , fmt .Errorf ("failed to decode response body: %w" , err )
62
62
}
63
63
return nil , fmt .Errorf ("401 %s: %s" , errorRes .Path , errorRes .ErrorMessage )
64
+ } else if res .StatusCode == http .StatusBadRequest {
65
+ var errorRes badRequestError
66
+ if err := json .NewDecoder (res .Body ).Decode (& errorRes ); err != nil {
67
+ return nil , fmt .Errorf ("failed to decode response body: %w" , err )
68
+ }
69
+ return nil , fmt .Errorf ("400 %s: %s" , errorRes .Path , errorRes .Error )
64
70
} else if res .StatusCode == http .StatusNotFound {
65
71
var errorRes notFoundError
66
72
if err := json .NewDecoder (res .Body ).Decode (& errorRes ); err != nil {
@@ -80,127 +86,24 @@ func get[T any](c *Client, ctx context.Context, endpoint string, headers http.He
80
86
return & result , nil
81
87
}
82
88
83
- func delete [T any ](c * Client , ctx context.Context , endpoint string , headers http.Header ) (* T , error ) {
84
- ctx , cancel := context .WithTimeout (ctx , c .timeout )
85
- defer cancel ()
86
-
87
- fullUrl := fmt .Sprintf ("%s%s" , c .baseUrl , endpoint )
88
- req , err := http .NewRequestWithContext (ctx , http .MethodDelete , fullUrl , nil )
89
- if err != nil {
90
- return nil , err
91
- }
92
- req .Header = headers
93
- req .Header .Set ("User-Agent" , c .userAgent )
94
-
95
- res , err := c .httpClient .Do (req )
96
- if err != nil {
97
- return nil , err
98
- }
99
- defer res .Body .Close ()
100
-
101
- if res .StatusCode == http .StatusUnauthorized {
102
- var errorRes unauthorizedError
103
- if err := json .NewDecoder (res .Body ).Decode (& errorRes ); err != nil {
104
- return nil , fmt .Errorf ("failed to decode response body: %w" , err )
105
- }
106
- return nil , fmt .Errorf ("401 %s: %s" , errorRes .Path , errorRes .ErrorMessage )
107
- } else if res .StatusCode == http .StatusInternalServerError {
108
- return nil , errors .New ("500 internal server error" )
109
- } else if res .StatusCode != http .StatusOK {
110
- return nil , fmt .Errorf ("unexpected response from server: %d" , res .StatusCode )
111
- }
89
+ func get [T any ](c * Client , ctx context.Context , endpoint string , headers http.Header ) (* T , error ) {
90
+ url := c .baseUrl + endpoint
91
+ return do [T ](c , ctx , http .MethodGet , url , headers , nil )
92
+ }
112
93
113
- var result T
114
- if err := json .NewDecoder (res .Body ).Decode (& result ); err != nil {
115
- return nil , fmt .Errorf ("failed to decode response body: %w" , err )
116
- }
117
- return & result , nil
94
+ func delete [T any ](c * Client , ctx context.Context , endpoint string , headers http.Header ) (* T , error ) {
95
+ url := c .baseUrl + endpoint
96
+ return do [T ](c , ctx , http .MethodDelete , url , headers , nil )
118
97
}
119
98
120
99
func post [T any ](c * Client , ctx context.Context , endpoint string , headers http.Header , body io.Reader ) (* T , error ) {
121
- ctx , cancel := context .WithTimeout (ctx , c .timeout )
122
- defer cancel ()
123
-
124
- fullUrl := fmt .Sprintf ("%s%s" , c .baseUrl , endpoint )
125
- req , err := http .NewRequestWithContext (ctx , http .MethodPost , fullUrl , body )
126
- if err != nil {
127
- return nil , err
128
- }
129
- req .Header = headers
130
- req .Header .Set ("User-Agent" , c .userAgent )
131
-
132
- res , err := c .httpClient .Do (req )
133
- if err != nil {
134
- return nil , err
135
- }
136
- defer res .Body .Close ()
137
-
138
- if res .StatusCode == http .StatusUnauthorized {
139
- var errorRes unauthorizedError
140
- if err := json .NewDecoder (res .Body ).Decode (& errorRes ); err != nil {
141
- return nil , fmt .Errorf ("failed to decode response body: %w" , err )
142
- }
143
- return nil , fmt .Errorf ("401 %s: %s" , errorRes .Path , errorRes .ErrorMessage )
144
- } else if res .StatusCode == http .StatusBadRequest {
145
- var errorRes badRequestError
146
- if err := json .NewDecoder (res .Body ).Decode (& errorRes ); err != nil {
147
- return nil , fmt .Errorf ("failed to decode response body: %w" , err )
148
- }
149
- return nil , fmt .Errorf ("400 %s: %s" , errorRes .Path , errorRes .Error )
150
- } else if res .StatusCode == http .StatusInternalServerError {
151
- return nil , errors .New ("500 internal server error" )
152
- } else if res .StatusCode != http .StatusOK {
153
- return nil , fmt .Errorf ("unexpected response from server: %d" , res .StatusCode )
154
- }
155
-
156
- var result T
157
- if err := json .NewDecoder (res .Body ).Decode (& result ); err != nil {
158
- return nil , fmt .Errorf ("failed to decode response body: %w" , err )
159
- }
160
- return & result , nil
100
+ url := c .baseUrl + endpoint
101
+ return do [T ](c , ctx , http .MethodPost , url , headers , body )
161
102
}
162
103
163
104
func put [T any ](c * Client , ctx context.Context , endpoint string , headers http.Header , body io.Reader ) (* T , error ) {
164
- ctx , cancel := context .WithTimeout (ctx , c .timeout )
165
- defer cancel ()
166
-
167
- fullUrl := fmt .Sprintf ("%s%s" , c .baseUrl , endpoint )
168
- req , err := http .NewRequestWithContext (ctx , http .MethodPut , fullUrl , body )
169
- if err != nil {
170
- return nil , err
171
- }
172
- req .Header = headers
173
- req .Header .Set ("User-Agent" , c .userAgent )
174
-
175
- res , err := c .httpClient .Do (req )
176
- if err != nil {
177
- return nil , err
178
- }
179
- defer res .Body .Close ()
180
-
181
- if res .StatusCode == http .StatusUnauthorized {
182
- var errorRes unauthorizedError
183
- if err := json .NewDecoder (res .Body ).Decode (& errorRes ); err != nil {
184
- return nil , fmt .Errorf ("failed to decode response body: %w" , err )
185
- }
186
- return nil , fmt .Errorf ("401 %s: %s" , errorRes .Path , errorRes .ErrorMessage )
187
- } else if res .StatusCode == http .StatusBadRequest {
188
- var errorRes badRequestError
189
- if err := json .NewDecoder (res .Body ).Decode (& errorRes ); err != nil {
190
- return nil , fmt .Errorf ("failed to decode response body: %w" , err )
191
- }
192
- return nil , fmt .Errorf ("400 %s: %s" , errorRes .Path , errorRes .Error )
193
- } else if res .StatusCode == http .StatusInternalServerError {
194
- return nil , errors .New ("500 internal server error" )
195
- } else if res .StatusCode != http .StatusOK {
196
- return nil , fmt .Errorf ("unexpected response from server: %d" , res .StatusCode )
197
- }
198
-
199
- var result T
200
- if err := json .NewDecoder (res .Body ).Decode (& result ); err != nil {
201
- return nil , fmt .Errorf ("failed to decode response body: %w" , err )
202
- }
203
- return & result , nil
105
+ url := c .baseUrl + endpoint
106
+ return do [T ](c , ctx , http .MethodPut , url , headers , body )
204
107
}
205
108
206
109
type unauthorizedError struct {
0 commit comments