From 6124a3fc61721e84e66116e97ac2fb7aa9d5fb05 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 13 Nov 2023 11:45:39 +0100 Subject: [PATCH] feat: added low level API for Database exports Signed-off-by: SoXX --- example/lowlevel/dbexport.go | 51 ++++++++++++++++++++++++++++++++++ pkg/e621/endpoints/dbexport.go | 8 +++--- 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 example/lowlevel/dbexport.go diff --git a/example/lowlevel/dbexport.go b/example/lowlevel/dbexport.go new file mode 100644 index 0000000..8b3162b --- /dev/null +++ b/example/lowlevel/dbexport.go @@ -0,0 +1,51 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "io" + "log" + "net/http" + "os" +) + +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.Println("Getting a list of DB Exports: ") + dbExportFiles, err := endpoints.GetDBExportList(requestContext) + if err != nil { + panic(err) + } + + log.Printf("%d files found", len(dbExportFiles)) + for _, v := range dbExportFiles { + log.Printf("File found: %s", v) + } + + exportFileName := dbExportFiles[0] + log.Println("Downloading DB export") + log.Printf("File to download: %s", exportFileName) + rawFile, err := endpoints.GetDBExportFile(requestContext, exportFileName) + + file, err := os.Create(exportFileName) + if err != nil { + panic(err) + } + + defer file.Close() + + _, err = io.Copy(file, rawFile) + if err != nil { + panic(err) + } + log.Printf("File %s downloaded", exportFileName) + +} diff --git a/pkg/e621/endpoints/dbexport.go b/pkg/e621/endpoints/dbexport.go index 90f94d9..a17c69a 100644 --- a/pkg/e621/endpoints/dbexport.go +++ b/pkg/e621/endpoints/dbexport.go @@ -5,6 +5,7 @@ import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "golang.org/x/net/html" + "io" "net/http" "strings" ) @@ -83,9 +84,9 @@ func GetDBExportList(requestContext model.RequestContext) ([]string, error) { // - file: The name of the file to be fetched from the database export. // // Returns: -// - *http.Response: The HTTP response containing the requested file (probably a csv.gz). +// - io.ReadCloser: The HTTP response containing the requested file (probably a csv.gz). // - error: An error, if any, encountered during the API request or response handling. -func GetDBExportFile(requestContext model.RequestContext, file string) (*http.Response, error) { +func GetDBExportFile(requestContext model.RequestContext, file string) (io.ReadCloser, error) { if file == "" { return nil, fmt.Errorf("no file specified") } @@ -111,7 +112,6 @@ func GetDBExportFile(requestContext model.RequestContext, file string) (*http.Re // If the status code is outside the 2xx range, return an error based on the status code. return nil, utils.StatusCodesToError(resp.StatusCode) } - - return resp, nil + return resp.Body, nil }