Skip to content

Commit dc589d1

Browse files
cosrnicmworzala
authored andcommitted
Make requested changes
1 parent 12fbe4c commit dc589d1

File tree

7 files changed

+79
-175
lines changed

7 files changed

+79
-175
lines changed

cmd/mc/skin/add.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ func (o *addSkinOpts) execute(args []string) error {
8787
return err
8888
}
8989

90-
client := mojang.NewProfileClient(o.app.Build.Version)
90+
client := mojang.NewProfileClient(o.app.Build.Version, token)
9191
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
9292
defer cancel()
9393

94-
info, err := client.ProfileInformation(ctx, token)
94+
info, err := client.ProfileInformation(ctx)
9595
if err != nil {
9696
return err
9797
}
@@ -112,14 +112,14 @@ func (o *addSkinOpts) execute(args []string) error {
112112

113113
skinData := args[0]
114114

115-
skin, err := o.app.SkinManager().CreateSkin(o.name, o.variant, skinData, o.cape, client, ctx)
115+
skin, err := o.app.SkinManager().CreateSkin(ctx, client, o.name, o.variant, skinData, o.cape)
116116
if err != nil {
117117
return err
118118
}
119119

120120
if o.apply {
121121

122-
err = o.app.SkinManager().ApplySkin(skin, client, ctx, token)
122+
err = o.app.SkinManager().ApplySkin(ctx, client, skin)
123123
if err != nil {
124124
return err
125125
}

cmd/mc/skin/apply.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ func (o *applySkinOpts) execute(args []string) error {
6767
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
6868
defer cancel()
6969

70-
client := mojang.NewProfileClient(o.app.Build.Version)
70+
client := mojang.NewProfileClient(o.app.Build.Version, token)
7171

72-
err = o.app.SkinManager().ApplySkin(skin, client, ctx, token)
72+
err = o.app.SkinManager().ApplySkin(ctx, client, skin)
7373
if err != nil {
7474
return err
7575
}

internal/pkg/modrinth/modrinth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"net/http"
99
"net/url"
1010
"time"
11+
12+
"github.com/mworzala/mc/internal/pkg/util"
1113
)
1214

1315
const (
14-
userAgentFormat = "mworzala/mc/%s"
15-
1616
fabricApiProjectId = "P7dR8mSH"
1717
)
1818

@@ -31,7 +31,7 @@ type Client struct {
3131
func NewClient(idVersion string) *Client {
3232
return &Client{
3333
baseUrl: prodUrl,
34-
userAgent: fmt.Sprintf(userAgentFormat, idVersion),
34+
userAgent: util.MakeUserAgent(idVersion),
3535
httpClient: http.DefaultClient,
3636
timeout: 10 * time.Second,
3737
}
@@ -40,7 +40,7 @@ func NewClient(idVersion string) *Client {
4040
func NewStagingClient() *Client {
4141
return &Client{
4242
baseUrl: stagingUrl,
43-
userAgent: fmt.Sprintf(userAgentFormat, "dev"),
43+
userAgent: util.MakeUserAgent("dev"),
4444
httpClient: http.DefaultClient,
4545
timeout: 10 * time.Second,
4646
}

internal/pkg/mojang/mojang.go

Lines changed: 36 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,44 @@ import (
88
"io"
99
"net/http"
1010
"time"
11-
)
1211

13-
const (
14-
userAgentFormat = "mworzala/mc/%s"
12+
"github.com/mworzala/mc/internal/pkg/util"
1513
)
1614

1715
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"
2220
)
2321

2422
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
2928
}
3029

31-
func NewProfileClient(idVersion string) *Client {
30+
func NewProfileClient(idVersion string, accountToken string) *Client {
3231
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,
3737
}
3838
}
3939

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) {
4141
ctx, cancel := context.WithTimeout(ctx, c.timeout)
4242
defer cancel()
4343

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)
4645
if err != nil {
4746
return nil, err
4847
}
48+
4949
req.Header = headers
5050
req.Header.Set("User-Agent", c.userAgent)
5151

@@ -61,6 +61,12 @@ func get[T any](c *Client, ctx context.Context, endpoint string, headers http.He
6161
return nil, fmt.Errorf("failed to decode response body: %w", err)
6262
}
6363
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)
6470
} else if res.StatusCode == http.StatusNotFound {
6571
var errorRes notFoundError
6672
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
8086
return &result, nil
8187
}
8288

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+
}
11293

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)
11897
}
11998

12099
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)
161102
}
162103

163104
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)
204107
}
205108

206109
type unauthorizedError struct {

internal/pkg/mojang/profile.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ func isURL(s string) bool {
1616
return err == nil && (u.Scheme == "http" || u.Scheme == "https")
1717
}
1818

19-
func (c *Client) ProfileInformation(ctx context.Context, accountToken string) (*ProfileInformationResponse, error) {
19+
func (c *Client) ProfileInformation(ctx context.Context) (*ProfileInformationResponse, error) {
2020

2121
headers := http.Header{}
2222

23-
headers.Set("Authorization", "Bearer "+accountToken)
23+
headers.Set("Authorization", "Bearer "+c.accountToken)
2424

25-
return get[ProfileInformationResponse](c, ctx, "", headers)
25+
return get[ProfileInformationResponse](c, ctx, "/", headers)
2626
}
2727

28-
func (c *Client) ChangeSkin(ctx context.Context, accountToken string, texture string, variant string) (*ProfileInformationResponse, error) {
28+
func (c *Client) ChangeSkin(ctx context.Context, texture string, variant string) (*ProfileInformationResponse, error) {
2929
var body *bytes.Buffer
3030
var contentType string
3131

@@ -76,15 +76,15 @@ func (c *Client) ChangeSkin(ctx context.Context, accountToken string, texture st
7676
headers := http.Header{}
7777

7878
headers.Set("Content-Type", contentType)
79-
headers.Set("Authorization", "Bearer "+accountToken)
79+
headers.Set("Authorization", "Bearer "+c.accountToken)
8080

8181
return post[ProfileInformationResponse](c, ctx, "/skins", headers, body)
8282
}
8383

84-
func (c *Client) ChangeCape(ctx context.Context, accountToken string, cape string) (*ProfileInformationResponse, error) {
85-
endpoint := "capes/active"
84+
func (c *Client) ChangeCape(ctx context.Context, cape string) (*ProfileInformationResponse, error) {
85+
endpoint := "/capes/active"
8686
headers := http.Header{}
87-
headers.Set("Authorization", "Bearer "+accountToken)
87+
headers.Set("Authorization", "Bearer "+c.accountToken)
8888
headers.Set("Content-Type", "application/json")
8989

9090
requestData := map[string]string{
@@ -99,30 +99,22 @@ func (c *Client) ChangeCape(ctx context.Context, accountToken string, cape strin
9999
return put[ProfileInformationResponse](c, ctx, endpoint, headers, bytes.NewBuffer(jsonData))
100100
}
101101

102-
func (c *Client) DeleteCape(ctx context.Context, accountToken string) (*ProfileInformationResponse, error) {
103-
endpoint := "capes/active"
102+
func (c *Client) DeleteCape(ctx context.Context) (*ProfileInformationResponse, error) {
103+
endpoint := "/capes/active"
104104
headers := http.Header{}
105-
headers.Set("Authorization", "Bearer "+accountToken)
105+
headers.Set("Authorization", "Bearer "+c.accountToken)
106106

107107
return delete[ProfileInformationResponse](c, ctx, endpoint, headers)
108108
}
109109

110110
func (c *Client) UsernameToUuid(ctx context.Context, username string) (*UsernameToUuidResponse, error) {
111-
oldUrl := c.baseUrl
112-
c.baseUrl = mojangApiUrl // i dont like this but i cant think of any other way atm :(
113-
endpoint := "users/profiles/minecraft/" + username
111+
url := mojangApiUrl + "/users/profiles/minecraft/" + username
114112

115-
response, err := get[UsernameToUuidResponse](c, ctx, endpoint, http.Header{})
116-
c.baseUrl = oldUrl
117-
return response, err
113+
return do[UsernameToUuidResponse](c, ctx, http.MethodGet, url, http.Header{}, nil)
118114
}
119115

120116
func (c *Client) UuidToProfile(ctx context.Context, uuid string) (*UuidToProfileResponse, error) {
121-
oldUrl := c.baseUrl
122-
c.baseUrl = sessionserverUrl // i dont like this but i cant think of any other way atm :(
123-
endpoint := "session/minecraft/profile/" + uuid
117+
url := sessionserverUrl + "/session/minecraft/profile/" + uuid
124118

125-
response, err := get[UuidToProfileResponse](c, ctx, endpoint, http.Header{})
126-
c.baseUrl = oldUrl
127-
return response, err
119+
return do[UuidToProfileResponse](c, ctx, http.MethodGet, url, http.Header{}, nil)
128120
}

0 commit comments

Comments
 (0)