-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kyle Jackson
committed
Sep 4, 2024
1 parent
9295439
commit e970834
Showing
6 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package artifacts | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/wizedkyle/artifactsmmo/v2/internal/models" | ||
"github.com/wizedkyle/artifactsmmo/v2/internal/utils" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
func (a *artifacts) ListEvents(params models.GetAllEventsQueryParameters) (*models.EventResponse, error) { | ||
req, err := a.generateRequest(http.MethodGet, "/events", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
q := req.URL.Query() | ||
if params.Page != 0 { | ||
q.Add("page", strconv.Itoa(params.Page)) | ||
} | ||
if params.Size != 0 { | ||
q.Add("size", strconv.Itoa(params.Size)) | ||
} else { | ||
q.Add("size", strconv.Itoa(50)) | ||
} | ||
req.URL.RawQuery = q.Encode() | ||
resp, err := a.Client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
switch resp.StatusCode { | ||
case 200: | ||
var response models.EventResponse | ||
err = json.Unmarshal(body, &response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &response, nil | ||
case 403: | ||
return nil, utils.ErrNotAuthenticated | ||
case 404: | ||
return nil, utils.ErrItemNotFound | ||
default: | ||
fmt.Println(string(body)) | ||
return nil, utils.ErrGenericError | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type EventResponse struct { | ||
Data []ActiveEvent `json:"data"` | ||
Total int `json:"total"` | ||
Page int `json:"page"` | ||
Size int `json:"size"` | ||
Pages int `json:"pages"` | ||
} | ||
|
||
type ActiveEvent struct { | ||
Name string `json:"name"` | ||
Map MapSchema `json:"map"` | ||
PreviousSkin string `json:"previous_skin"` | ||
Duration int `json:"duration"` | ||
Expiration time.Time `json:"expiration"` | ||
CreatedAt time.Time `json:"created_at"` | ||
} | ||
|
||
type MapSchema struct { | ||
Name string `json:"name"` | ||
Skin string `json:"skin"` | ||
X int `json:"x"` | ||
Y int `json:"y"` | ||
Content MapContent `json:"content"` | ||
} | ||
|
||
type MapContent struct { | ||
Type string `json:"type"` | ||
Code string `json:"code"` | ||
} | ||
|
||
type GetAllEventsQueryParameters struct { | ||
Page int `json:"page"` | ||
Size int `json:"size"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters