From 9d84dc35d4dd2d6285d49aaace9b1e2bc7dc3c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Fri, 1 Nov 2024 12:43:45 +0100 Subject: [PATCH] Add ChallengeNoTrack to avoid Entity Not Found when challenges get removed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaƫl Pinson --- instruqt/challenge.go | 9 +++++++++ instruqt/client.go | 17 +++++++++++++++-- instruqt/track.go | 22 +++++++++++----------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/instruqt/challenge.go b/instruqt/challenge.go index b0668e5..4d60d98 100644 --- a/instruqt/challenge.go +++ b/instruqt/challenge.go @@ -42,6 +42,15 @@ type Challenge struct { } `json:"-"` } +// ChallengeNoTrack represents the data structure for an Instruqt challenge. +type ChallengeNoTrack struct { + Id string `json:"id"` // The unique identifier for the challenge. + Slug string `json:"slug"` // The slug for the challenge, which is a human-readable identifier. + Title string `json:"title"` // The title of the challenge. + Index int `json:"index"` // The index of the challenge in the track. + Status string `json:"status"` // The status of the challenge (e.g., "unlocked", "completed"). +} + // GetChallenge retrieves a challenge from Instruqt using its unique challenge ID. // // Parameters: diff --git a/instruqt/client.go b/instruqt/client.go index 98a3c9c..a1f4b0d 100644 --- a/instruqt/client.go +++ b/instruqt/client.go @@ -18,9 +18,12 @@ import ( "context" "log" "net/http" + "net/http/httputil" "os" "github.com/shurcooL/graphql" + + loghttp "github.com/motemen/go-loghttp" ) // GraphQLClient is an interface that defines the methods for interacting with @@ -52,8 +55,18 @@ type Client struct { func NewClient(token string, teamSlug string) *Client { httpClient := &http.Client{} httpClient.Transport = &BearerTokenRoundTripper{ - Transport: http.DefaultTransport, - Token: token, + Transport: &loghttp.Transport{ + Transport: httpClient.Transport, + LogRequest: func(req *http.Request) { + b, _ := httputil.DumpRequestOut(req, true) + log.Printf("out body: %s", string(b)) + }, + LogResponse: func(resp *http.Response) { + b, _ := httputil.DumpResponse(resp, true) + log.Printf("in body: %s", string(b)) + }, + }, + Token: token, } return &Client{ GraphQLClient: graphql.NewClient("https://play.instruqt.com/graphql", httpClient), diff --git a/instruqt/track.go b/instruqt/track.go index e95daab..934bb6f 100644 --- a/instruqt/track.go +++ b/instruqt/track.go @@ -91,11 +91,11 @@ type SandboxTrack struct { TotalCount int Nodes []Review } - Challenges []Challenge // A list of challenges associated with the sandbox track. - Status string // The current status of the sandbox track. - Started time.Time // The timestamp when the sandbox track was started. - Completed time.Time // The timestamp when the sandbox track was completed. - Participant struct { // Information about the participant of the sandbox track. + Challenges []ChallengeNoTrack // A list of challenges associated with the sandbox track. + Status string // The current status of the sandbox track. + Started time.Time // The timestamp when the sandbox track was started. + Completed time.Time // The timestamp when the sandbox track was completed. + Participant struct { // Information about the participant of the sandbox track. Id string } } @@ -117,11 +117,11 @@ type SandboxTrackNoReviews struct { TrackTags []struct { // A list of tags associated with the sandbox track. Value string } - Challenges []Challenge // A list of challenges associated with the sandbox track. - Status string // The current status of the sandbox track. - Started time.Time // The timestamp when the sandbox track was started. - Completed time.Time // The timestamp when the sandbox track was completed. - Participant struct { // Information about the participant of the sandbox track. + Challenges []ChallengeNoTrack // A list of challenges associated with the sandbox track. + Status string // The current status of the sandbox track. + Started time.Time // The timestamp when the sandbox track was started. + Completed time.Time // The timestamp when the sandbox track was completed. + Participant struct { // Information about the participant of the sandbox track. Id string } } @@ -216,7 +216,7 @@ func (c *Client) GetTrackBySlug(trackSlug string) (t Track, err error) { // Returns: // - Challenge: The first unlocked challenge found. // - error: Any error encountered while retrieving the challenge. -func (c *Client) GetTrackUnlockedChallenge(userId string, trackId string) (challenge Challenge, err error) { +func (c *Client) GetTrackUnlockedChallenge(userId string, trackId string) (challenge ChallengeNoTrack, err error) { track, err := c.GetUserTrackById(userId, trackId) if err != nil { return challenge, fmt.Errorf("[instruqt.GetTrackUnlockedChallenge] failed to get user track: %v", err)