diff --git a/README.md b/README.md index 13bb507..82f357d 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ _Low Level API_ | Tag aliases | :x: | | | Tag implications | :x: | | | Notes | :x: | | -| Pools | :x: | | +| Pools | :heavy_check_mark: | | | Users | :heavy_check_mark: | | | Favorites | :x: | | | DB export | :x: | | \ No newline at end of file diff --git a/example/lowlevel/pool.go b/example/lowlevel/pool.go new file mode 100644 index 0000000..2b6948b --- /dev/null +++ b/example/lowlevel/pool.go @@ -0,0 +1,40 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "log" + "net/http" +) + +func main() { + requestContext := model.RequestContext{ + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "", + APIKey: "", + } + + log.Println("Getting single pool: ") + client := http.Client{} + pool, err := endpoints.GetPool(client, requestContext, "36957") + + if err != nil { + log.Println(err) + } + log.Println(pool.Name) + log.Println("----------") + + log.Println("Getting list of pools: ") + query := map[string]string{ + "limit": "5", + } + pools, err := endpoints.GetPools(client, requestContext, query) + + if err != nil { + log.Println(err) + } + log.Println(len(pools)) + log.Println("----------") + +} diff --git a/pkg/e621/endpoints/pool.go b/pkg/e621/endpoints/pool.go new file mode 100644 index 0000000..07baee6 --- /dev/null +++ b/pkg/e621/endpoints/pool.go @@ -0,0 +1,103 @@ +package endpoints + +import ( + "encoding/json" + "fmt" + "git.dragse.it/anthrove/e621-to-graph/internal/utils" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "log" + "net/http" +) + +func GetPool(client http.Client, requestContext model.RequestContext, ID string) (model.Pool, error) { + // Create a new HTTP GET request to fetch user information from the specified 'host' and 'username'. + r, err := http.NewRequest("GET", fmt.Sprintf("%s/pools/%s.json", requestContext.Host, ID), nil) + if err != nil { + // Log the error and return an empty User struct and the error. + log.Println(err) + return model.Pool{}, err + } + + r.Header.Set("User-Agent", requestContext.UserAgent) + r.Header.Add("Accept", "application/json") + r.SetBasicAuth(requestContext.Username, requestContext.APIKey) + + // Send the request using the provided HTTP client. + resp, err := client.Do(r) + if err != nil { + // Log the error and return an empty User struct and the error. + log.Println(err) + return model.Pool{}, err + } + + // Check if the HTTP response status code indicates success (2xx range). + if resp.StatusCode < 200 || resp.StatusCode > 300 { + // If the status code is outside the 2xx range, return an error based on the status code. + return model.Pool{}, utils.StatusCodesToError(resp.StatusCode) + } + + // Initialize a User struct to store the response data. + var poolResponse model.Pool + + // Decode the JSON response into the model.poolResponse{} struct. + err = json.NewDecoder(resp.Body).Decode(&poolResponse) + if err != nil { + // Log the error and return an empty User struct and the error. + log.Println(err) + return model.Pool{}, err + } + + // Return the user information and no error (nil). + return poolResponse, nil +} + +func GetPools(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Pool, error) { + // Create a new HTTP GET request. + r, err := http.NewRequest("GET", fmt.Sprintf("%s/pools.json", requestContext.Host), nil) + if err != nil { + log.Print(err) + } + + // Append query parameters to the request URL. + q := r.URL.Query() + for k, v := range query { + q.Add(k, v) + } + r.URL.RawQuery = q.Encode() + + r.Header.Set("User-Agent", requestContext.UserAgent) + r.Header.Add("Accept", "application/json") + r.SetBasicAuth(requestContext.Username, requestContext.APIKey) + + // Send the request using the provided HTTP client. + resp, err := client.Do(r) + if err != nil { + log.Print(err) + } + + // Send the request using the provided HTTP client. + resp, err = client.Do(r) + if err != nil { + log.Print(err) + } + + // Check if the HTTP response status code indicates success (2xx range). + if resp.StatusCode < 200 || resp.StatusCode > 300 { + // If the status code is outside the 2xx range, return an error based on the status code. + return nil, utils.StatusCodesToError(resp.StatusCode) + } + + // Initialize a User struct to store the response data. + var poolResponse []model.Pool + + // Decode the JSON response into the user struct. + err = json.NewDecoder(resp.Body).Decode(&poolResponse) + if err != nil { + // Log the error and return an empty User struct and the error. + log.Println(err) + return nil, err + } + + return poolResponse, nil + +} diff --git a/pkg/e621/endpoints/pool_test.go b/pkg/e621/endpoints/pool_test.go new file mode 100644 index 0000000..9758d84 --- /dev/null +++ b/pkg/e621/endpoints/pool_test.go @@ -0,0 +1,143 @@ +package endpoints + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "github.com/jarcoal/httpmock" + "net/http" + "testing" +) + +func TestGetPool(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + poolResponse := model.Pool{ + ID: 36957, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + } + + jsonResponser, err := httpmock.NewJsonResponder(200, poolResponse) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/pools/36957.json", jsonResponser) + + requestContext := model.RequestContext{ + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "memo", + APIKey: "123456", + } + + client := http.Client{} + pool, err := GetPool(client, requestContext, "36957") + if err != nil { + t.Error(err) + return + } + + if pool.ID == poolResponse.ID && pool.Name == poolResponse.Name { + return + } + + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pool, poolResponse) + +} + +func TestGetPools(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + poolResponse := []model.Pool{ + { + ID: 36957, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + }, + { + ID: 5772, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + }, + { + ID: 453687, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + }, + { + ID: 3456876, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + }, + } + + jsonResponser, err := httpmock.NewJsonResponder(200, poolResponse) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/pools.json", jsonResponser) + + requestContext := model.RequestContext{ + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "memo", + APIKey: "123456", + } + + client := http.Client{} + pools, err := GetPools(client, requestContext, map[string]string{}) + if err != nil { + t.Error(err) + return + } + + if len(pools) == 4 && pools[0].ID == poolResponse[0].ID && pools[1].Name == poolResponse[1].Name && pools[2].UpdatedAt == poolResponse[2].UpdatedAt { + return + } + + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pools, poolResponse) + +} diff --git a/pkg/e621/endpoints/pools.go b/pkg/e621/endpoints/pools.go deleted file mode 100644 index a29b68f..0000000 --- a/pkg/e621/endpoints/pools.go +++ /dev/null @@ -1 +0,0 @@ -package endpoints diff --git a/pkg/e621/model/pool.go b/pkg/e621/model/pool.go new file mode 100644 index 0000000..26879d1 --- /dev/null +++ b/pkg/e621/model/pool.go @@ -0,0 +1,15 @@ +package model + +type Pool struct { + ID int64 `json:"id"` + Name string `json:"name"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + CreatorID int64 `json:"creator_id"` + Description string `json:"description"` + IsActive bool `json:"is_active"` + Category string `json:"category"` + PostIDS []int64 `json:"post_ids"` + CreatorName string `json:"creator_name"` + PostCount int64 `json:"post_count"` +}