Skip to content

Commit 1c09119

Browse files
Pull request for issue #22 (#23)
* Refactor codebase for modern Go standards This commit introduces a comprehensive refactoring of the codebase to align with modern Go idioms and improve overall maintainability and readability. Key changes include: - Renamed the main `psn` struct to `Client` and its constructor `NewPsnApi` to `NewClient` for better clarity and adherence to Go conventions. - Introduced an `Options` struct for client initialization, replacing individual setters and simplifying the API. - Refactored the authentication logic in `auth.go`, extracting hardcoded values into constants and simplifying the control flow. - Consolidated HTTP request handling in `http.go` into a single `do` method to reduce code duplication and improve error handling. - Updated all API-facing methods to use the new `Client` struct and its associated methods. - Added a unit test for the `NewClient` function to ensure correct initialization. - Updated the `README.md` example to reflect the new, refactored API. * Fix: Handle error returns from response body Close This commit addresses linting errors by ensuring that the error returned from `resp.Body.Close()` is properly handled in `auth.go` and `http.go`. This prevents potential resource leaks and aligns with Go's error handling best practices. * fix auth * fix auth --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Ilya Sizov <sizov.ilya@gmail.com>
1 parent 3f9c134 commit 1c09119

File tree

9 files changed

+315
-343
lines changed

9 files changed

+315
-343
lines changed

README.md

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -55,72 +55,68 @@ Copy this js code:
5555
package main
5656

5757
import (
58-
"fmt"
59-
"github.com/sizovilya/go-psn-api"
58+
"context"
59+
"fmt"
60+
"github.com/sizovilya/go-psn-api"
6061
)
6162

6263
func main() {
63-
ctx := context.Background()
64-
lang := "ru" // known list here https://github.com/sizovilya/go-psn-api/blob/main/langs.go, some languages in list are wrong and unsupported now, feel free to investigate for your own and add it to list
65-
region := "ru" // known list here https://github.com/sizovilya/go-psn-api/blob/main/regions.go, some regions in list are wrong and unsupported now, feel free to investigate for your own and add it to list
66-
npsso := "<your npsso>"
67-
psnApi, err := psn.NewPsnApi(
68-
lang,
69-
region,
70-
)
71-
if err != nil {
72-
panic(err)
73-
}
74-
75-
// This request will get access and refresh tokens from Sony's servers
76-
err = psnApi.AuthWithNPSSO(ctx, npsso)
77-
if err != nil {
78-
panic(err)
79-
}
80-
81-
// If you obtain refresh token you may use it for next logins.
82-
// Next logins should be like this:
83-
// refreshToken, _ := psnApi.GetRefreshToken() // store refresh token somewhere for future logins by psnApi.AuthWithRefreshToken method
84-
err = psnApi.AuthWithRefreshToken(ctx, "<your token>") // will get new access token, feel free to manage tokens by yourself
85-
if err != nil {
86-
panic(err)
87-
}
88-
89-
// How to get user's profile info
90-
profile, err := psnApi.GetProfileRequest(ctx, "geeek_52rus")
91-
if err != nil {
92-
panic(err)
93-
}
94-
fmt.Print(profile)
95-
96-
// How to get trophy titles
97-
trophyTitles, err := psnApi.GetTrophyTitles(ctx, "geeek_52rus", 50, 0)
98-
if err != nil {
99-
panic(err)
100-
}
101-
fmt.Print(trophyTitles)
102-
103-
// How to get trophy group by trophy title
104-
trophyTitleId := trophyTitles.TrophyTitles[0].NpCommunicationID // get first of them
105-
trophyGroups, err := psnApi.GetTrophyGroups(ctx, trophyTitleId, "geeek_52rus")
106-
if err != nil {
107-
panic(err)
108-
}
109-
fmt.Println(trophyGroups)
110-
111-
// How to get trophies in certain trophy title and trophy group
112-
trophies, err := psnApi.GetTrophies(
113-
ctx,
114-
"NPWR13348_00", // The Last of Us 2
115-
"001", // trophy group with id = 001
116-
"geeek_52rus",
117-
)
118-
if err != nil {
119-
panic(err)
120-
}
121-
fmt.Println(trophies)
64+
ctx := context.Background()
65+
opts := &psn.Options{
66+
Lang: "ru", // See https://github.com/sizovilya/go-psn-api/blob/main/langs.go
67+
Region: "ru", // See https://github.com/sizovilya/go-psn-api/blob/main/regions.go
68+
Npsso: "<your_npsso_code>",
69+
}
70+
71+
client, err := psn.NewClient(opts)
72+
if err != nil {
73+
panic(err)
74+
}
75+
76+
// Authenticate to get access and refresh tokens.
77+
err = client.AuthWithNPSSO(ctx, opts.Npsso)
78+
if err != nil {
79+
panic(err)
80+
}
81+
82+
// You can also use a refresh token for subsequent authentications.
83+
// refreshToken, _ := client.RefreshToken()
84+
// err = client.AuthWithRefreshToken(ctx, refreshToken)
85+
// if err != nil {
86+
// panic(err)
87+
// }
88+
89+
// Get user profile information.
90+
profile, err := client.GetProfile(ctx, "geeek_52rus")
91+
if err != nil {
92+
panic(err)
93+
}
94+
fmt.Printf("Profile: %+v\n", profile)
95+
96+
// Get trophy titles.
97+
trophyTitles, err := client.GetTrophyTitles(ctx, "geeek_52rus", 50, 0)
98+
if err != nil {
99+
panic(err)
100+
}
101+
fmt.Printf("Trophy Titles: %+v\n", trophyTitles)
102+
103+
// Get trophy groups for a specific title.
104+
if len(trophyTitles.TrophyTitles) > 0 {
105+
trophyTitleID := trophyTitles.TrophyTitles[0].NpCommunicationID
106+
trophyGroups, err := client.GetTrophyGroups(ctx, trophyTitleID, "geeek_52rus")
107+
if err != nil {
108+
panic(err)
109+
}
110+
fmt.Printf("Trophy Groups: %+v\n", trophyGroups)
111+
}
112+
113+
// Get trophies for a specific title and group.
114+
trophies, err := client.GetTrophies(ctx, "NPWR13348_00", "001", "geeek_52rus")
115+
if err != nil {
116+
panic(err)
117+
}
118+
fmt.Printf("Trophies: %+v\n", trophies)
122119
}
123-
124120
```
125121
This project highly inspired by https://github.com/Tustin/psn-php. Some useful things like auth headers and params found in `Tustin/psn-php`.
126122
<p align="center"> <img src="assets/gopher-dance.gif"> </p>

0 commit comments

Comments
 (0)