feat: added note support
Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
parent
251647cc69
commit
a687dc84ab
40
example/lowlevel/note.go
Normal file
40
example/lowlevel/note.go
Normal file
@ -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 note: ")
|
||||||
|
client := http.Client{}
|
||||||
|
note, err := endpoints.GetNote(client, requestContext, "36957")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
log.Println(note.Body)
|
||||||
|
log.Println("----------")
|
||||||
|
|
||||||
|
log.Println("Getting list of notes: ")
|
||||||
|
query := map[string]string{
|
||||||
|
"limit": "5",
|
||||||
|
}
|
||||||
|
notes, err := endpoints.GetNotes(client, requestContext, query)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
log.Println(len(notes))
|
||||||
|
log.Println("----------")
|
||||||
|
|
||||||
|
}
|
103
pkg/e621/endpoints/note.go
Normal file
103
pkg/e621/endpoints/note.go
Normal file
@ -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 GetNote(client http.Client, requestContext model.RequestContext, ID string) (model.Note, 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/notes/%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.Note{}, 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.Note{}, 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.Note{}, utils.StatusCodesToError(resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize a User struct to store the response data.
|
||||||
|
var poolResponse model.Note
|
||||||
|
|
||||||
|
// 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.Note{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the user information and no error (nil).
|
||||||
|
return poolResponse, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNotes(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Note, error) {
|
||||||
|
// Create a new HTTP GET request.
|
||||||
|
r, err := http.NewRequest("GET", fmt.Sprintf("%s/notes.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.Note
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
}
|
138
pkg/e621/endpoints/note_test.go
Normal file
138
pkg/e621/endpoints/note_test.go
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
package endpoints
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
||||||
|
"github.com/jarcoal/httpmock"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetNote(t *testing.T) {
|
||||||
|
httpmock.Activate()
|
||||||
|
defer httpmock.DeactivateAndReset()
|
||||||
|
|
||||||
|
noteResponse := model.Note{
|
||||||
|
ID: 385777,
|
||||||
|
CreatedAt: "2023-10-19T06:42:20.171-04:00",
|
||||||
|
UpdatedAt: "2023-10-19T06:42:20.171-04:00",
|
||||||
|
CreatorID: 1472475,
|
||||||
|
X: 277,
|
||||||
|
Y: 841,
|
||||||
|
Width: 101,
|
||||||
|
Height: 176,
|
||||||
|
Version: 2,
|
||||||
|
IsActive: true,
|
||||||
|
PostID: 2624329,
|
||||||
|
Body: "Well, isn’t it just right to use them?",
|
||||||
|
CreatorName: "ponypussypounder",
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonResponser, err := httpmock.NewJsonResponder(200, noteResponse)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
httpmock.RegisterResponder("GET", "https://e621.net/notes/36957.json", jsonResponser)
|
||||||
|
|
||||||
|
requestContext := model.RequestContext{
|
||||||
|
Host: "https://e621.net",
|
||||||
|
UserAgent: "Go-e621-SDK (@username)",
|
||||||
|
Username: "memo",
|
||||||
|
APIKey: "123456",
|
||||||
|
}
|
||||||
|
|
||||||
|
client := http.Client{}
|
||||||
|
pool, err := GetNote(client, requestContext, "36957")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if pool.ID == noteResponse.ID && pool.Body == noteResponse.Body {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pool, noteResponse)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetNotes(t *testing.T) {
|
||||||
|
httpmock.Activate()
|
||||||
|
defer httpmock.DeactivateAndReset()
|
||||||
|
|
||||||
|
noteResponse := []model.Note{
|
||||||
|
{
|
||||||
|
ID: 385777,
|
||||||
|
CreatedAt: "2023-10-19T06:42:20.171-04:00",
|
||||||
|
UpdatedAt: "2023-10-19T06:42:20.171-04:00",
|
||||||
|
CreatorID: 1472475,
|
||||||
|
X: 277,
|
||||||
|
Y: 841,
|
||||||
|
Width: 101,
|
||||||
|
Height: 176,
|
||||||
|
Version: 2,
|
||||||
|
IsActive: true,
|
||||||
|
PostID: 2624329,
|
||||||
|
Body: "Well, isn’t it just right to use them?",
|
||||||
|
CreatorName: "ponypussypounder",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 56782,
|
||||||
|
CreatedAt: "2023-10-19T06:42:20.171-04:00",
|
||||||
|
UpdatedAt: "2023-10-19T06:42:20.171-04:00",
|
||||||
|
CreatorID: 1472475,
|
||||||
|
X: 277,
|
||||||
|
Y: 841,
|
||||||
|
Width: 101,
|
||||||
|
Height: 176,
|
||||||
|
Version: 2,
|
||||||
|
IsActive: true,
|
||||||
|
PostID: 2624329,
|
||||||
|
Body: "Well, isn’t it just right to use them?",
|
||||||
|
CreatorName: "ponypussypounder",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: 43847234,
|
||||||
|
CreatedAt: "2023-10-19T06:42:20.171-04:00",
|
||||||
|
UpdatedAt: "2023-10-19T06:42:20.171-04:00",
|
||||||
|
CreatorID: 1472475,
|
||||||
|
X: 277,
|
||||||
|
Y: 841,
|
||||||
|
Width: 101,
|
||||||
|
Height: 176,
|
||||||
|
Version: 2,
|
||||||
|
IsActive: true,
|
||||||
|
PostID: 2624329,
|
||||||
|
Body: "Well, isn’t it just right to use them?",
|
||||||
|
CreatorName: "ponypussypounder",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonResponser, err := httpmock.NewJsonResponder(200, noteResponse)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
httpmock.RegisterResponder("GET", "https://e621.net/notes.json", jsonResponser)
|
||||||
|
|
||||||
|
requestContext := model.RequestContext{
|
||||||
|
Host: "https://e621.net",
|
||||||
|
UserAgent: "Go-e621-SDK (@username)",
|
||||||
|
Username: "memo",
|
||||||
|
APIKey: "123456",
|
||||||
|
}
|
||||||
|
|
||||||
|
client := http.Client{}
|
||||||
|
pools, err := GetNotes(client, requestContext, map[string]string{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(pools) == 3 && pools[0].ID == noteResponse[0].ID && pools[1].Body == noteResponse[1].Body && pools[2].UpdatedAt == noteResponse[2].UpdatedAt {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pools, noteResponse)
|
||||||
|
|
||||||
|
}
|
@ -1 +0,0 @@
|
|||||||
package endpoints
|
|
17
pkg/e621/model/note.go
Normal file
17
pkg/e621/model/note.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type Note struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
CreatedAt string `json:"created_at"`
|
||||||
|
UpdatedAt string `json:"updated_at"`
|
||||||
|
CreatorID int64 `json:"creator_id"`
|
||||||
|
X int64 `json:"x"`
|
||||||
|
Y int64 `json:"y"`
|
||||||
|
Width int64 `json:"width"`
|
||||||
|
Height int64 `json:"height"`
|
||||||
|
Version int64 `json:"version"`
|
||||||
|
IsActive bool `json:"is_active"`
|
||||||
|
PostID int64 `json:"post_id"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
CreatorName string `json:"creator_name"`
|
||||||
|
}
|
Reference in New Issue
Block a user