feat: added mid-level API for notes
Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
parent
443e7d14ed
commit
0c0a8eb8a4
58
example/midlevel/note.go
Normal file
58
example/midlevel/note.go
Normal file
@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder"
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Define the request context with essential information.
|
||||
requestContext := model.RequestContext{
|
||||
Client: http.Client{},
|
||||
Host: "https://e621.net",
|
||||
UserAgent: "Go-e621-SDK (@username)",
|
||||
Username: os.Getenv("API_USER"), // Replace with your username
|
||||
APIKey: os.Getenv("API_KEY"), // Replace with your API key
|
||||
}
|
||||
|
||||
// Log: Getting a single note.
|
||||
log.Println("Getting single note: ")
|
||||
|
||||
// Specify the note ID for retrieval.
|
||||
noteID := 36957 // Replace with the desired note's ID.
|
||||
|
||||
// Call the GetNote function to retrieve the specified note.
|
||||
getNote := builder.NewGetNoteBuilder(requestContext)
|
||||
note, err := getNote.SetNoteID(noteID).Execute()
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
// Log the body of the retrieved note.
|
||||
log.Println(note.Body)
|
||||
}
|
||||
log.Println("----------")
|
||||
|
||||
// Log: Getting a list of notes.
|
||||
log.Println("Getting a list of notes: ")
|
||||
|
||||
// Call the GetNotes function to retrieve a list of notes based on the query parameters.
|
||||
getNotes := builder.NewGetNotesBuilder(requestContext)
|
||||
notes, err := getNotes.SetLimit(5).Active(true).SearchInBody("furry").Execute()
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
// Log the number of notes retrieved.
|
||||
log.Println(len(notes))
|
||||
for _, note := range notes {
|
||||
log.Printf("Note by %s - %s : %s", note.CreatorName, note.Body, strconv.FormatInt(note.ID, 10))
|
||||
}
|
||||
}
|
||||
log.Println("----------")
|
||||
}
|
38
pkg/e621/builder/note.go
Normal file
38
pkg/e621/builder/note.go
Normal file
@ -0,0 +1,38 @@
|
||||
package builder
|
||||
|
||||
import (
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints"
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
||||
"log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type NoteBuilder interface {
|
||||
SetNoteID(noteID int) NoteBuilder
|
||||
Execute() (*model.Note, error)
|
||||
}
|
||||
|
||||
func NewGetNoteBuilder(requestContext model.RequestContext) NoteBuilder {
|
||||
return &getNote{requestContext: requestContext}
|
||||
|
||||
}
|
||||
|
||||
type getNote struct {
|
||||
requestContext model.RequestContext
|
||||
noteID int
|
||||
}
|
||||
|
||||
func (g *getNote) SetNoteID(noteID int) NoteBuilder {
|
||||
g.noteID = noteID
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *getNote) Execute() (*model.Note, error) {
|
||||
note, err := endpoints.GetNote(g.requestContext, strconv.Itoa(g.noteID))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return ¬e, nil
|
||||
|
||||
}
|
58
pkg/e621/builder/note_test.go
Normal file
58
pkg/e621/builder/note_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package builder
|
||||
|
||||
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{
|
||||
Client: http.Client{},
|
||||
Host: "https://e621.net",
|
||||
UserAgent: "Go-e621-SDK (@username)",
|
||||
Username: "memo",
|
||||
APIKey: "123456",
|
||||
}
|
||||
|
||||
getNote := NewGetNoteBuilder(requestContext)
|
||||
pool, err := getNote.SetNoteID(36957).Execute()
|
||||
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)
|
||||
|
||||
}
|
75
pkg/e621/builder/notes.go
Normal file
75
pkg/e621/builder/notes.go
Normal file
@ -0,0 +1,75 @@
|
||||
package builder
|
||||
|
||||
import (
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints"
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
||||
"log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type NotesBuilder interface {
|
||||
SearchInBody(body string) NotesBuilder
|
||||
NoteID(noteID int) NotesBuilder
|
||||
SearchTags(tags string) NotesBuilder
|
||||
SearchCreatorID(creatorID int) NotesBuilder
|
||||
searchCreatorName(creatorName string) NotesBuilder
|
||||
Active(isActive bool) NotesBuilder
|
||||
SetLimit(limitNotes int) NotesBuilder
|
||||
Execute() ([]model.Note, error)
|
||||
}
|
||||
|
||||
func NewGetNotesBuilder(requestContext model.RequestContext) NotesBuilder {
|
||||
return &getNotes{
|
||||
requestContext: requestContext,
|
||||
query: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
type getNotes struct {
|
||||
requestContext model.RequestContext
|
||||
query map[string]string
|
||||
}
|
||||
|
||||
func (g getNotes) SearchInBody(body string) NotesBuilder {
|
||||
g.query["search[body_matches]"] = body
|
||||
return g
|
||||
}
|
||||
|
||||
func (g getNotes) NoteID(noteID int) NotesBuilder {
|
||||
g.query["search[post_id]"] = strconv.Itoa(noteID)
|
||||
return g
|
||||
}
|
||||
|
||||
func (g getNotes) SearchTags(tags string) NotesBuilder {
|
||||
g.query["search[post_tags_match]"] = tags
|
||||
return g
|
||||
}
|
||||
|
||||
func (g getNotes) SearchCreatorID(creatorID int) NotesBuilder {
|
||||
g.query["search[creator_id]"] = strconv.Itoa(creatorID)
|
||||
return g
|
||||
}
|
||||
|
||||
func (g getNotes) searchCreatorName(creatorName string) NotesBuilder {
|
||||
g.query["search[creator_name]"] = creatorName
|
||||
return g
|
||||
}
|
||||
|
||||
func (g getNotes) Active(isActive bool) NotesBuilder {
|
||||
g.query["search[is_active]"] = strconv.FormatBool(isActive)
|
||||
return g
|
||||
}
|
||||
|
||||
func (g getNotes) SetLimit(limitNotes int) NotesBuilder {
|
||||
g.query["limit"] = strconv.Itoa(limitNotes)
|
||||
return g
|
||||
}
|
||||
|
||||
func (g getNotes) Execute() ([]model.Note, error) {
|
||||
notes, err := endpoints.GetNotes(g.requestContext, g.query)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return notes, nil
|
||||
}
|
90
pkg/e621/builder/notes_test.go
Normal file
90
pkg/e621/builder/notes_test.go
Normal file
@ -0,0 +1,90 @@
|
||||
package builder
|
||||
|
||||
import (
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
||||
"github.com/jarcoal/httpmock"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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{
|
||||
Client: http.Client{},
|
||||
Host: "https://e621.net",
|
||||
UserAgent: "Go-e621-SDK (@username)",
|
||||
Username: "memo",
|
||||
APIKey: "123456",
|
||||
}
|
||||
|
||||
getNotes := NewGetNotesBuilder(requestContext)
|
||||
pools, err := getNotes.SetLimit(3).Execute()
|
||||
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)
|
||||
|
||||
}
|
Reference in New Issue
Block a user