Skip to content

Commit

Permalink
Merge pull request #5 from waifuvault/3-context-for-requests
Browse files Browse the repository at this point in the history
context for requests
  • Loading branch information
VictoriqueMoe authored Mar 14, 2024
2 parents 903e4b6 + 7fbb450 commit fc9844a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 52 deletions.
21 changes: 16 additions & 5 deletions pkg/mod/waifu_vault.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package mod

import "context"

type Waifuvalt interface {
UploadFile(options WaifuvaultPutOpts) (*WaifuResponse[string], error)
FileInfo(token string) (*WaifuResponse[int], error)
FileInfoFormatted(token string) (*WaifuResponse[string], error)
DeleteFile(token string) (bool, error)
GetFile(options GetFileInfo) ([]byte, error)
// UploadFile - Upload a file using a byte array, url or file
UploadFile(ctx context.Context, options WaifuvaultPutOpts) (*WaifuResponse[string], error)

// FileInfo - Obtain file info such as URL and retention period (as an epoch timestamp)
FileInfo(ctx context.Context, token string) (*WaifuResponse[int], error)

// FileInfoFormatted - Same as FileInfo, but instead returns the retention period as a human-readable string
FileInfoFormatted(ctx context.Context, token string) (*WaifuResponse[string], error)

// DeleteFile - Delete a file given a token
DeleteFile(ctx context.Context, token string) (bool, error)

// GetFile - Download the file given options and return a byte array of said file
GetFile(ctx context.Context, options GetFileInfo) ([]byte, error)
}
55 changes: 30 additions & 25 deletions pkg/waifu_vault_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package waifuVault

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -15,16 +16,20 @@ import (

const baseUrl = "https://waifuvault.moe"

var client = &http.Client{}
type api struct {
client http.Client
}

type Api struct {
func NewWaifuvaltApi(client http.Client) mod.Waifuvalt {
return &api{
client: client,
}
}

func (re *Api) UploadFile(options mod.WaifuvaultPutOpts) (*mod.WaifuResponse[string], error) {
func (re *api) UploadFile(ctx context.Context, options mod.WaifuvaultPutOpts) (*mod.WaifuResponse[string], error) {
if options.File != nil && options.Bytes != nil && options.Url != "" || options.File == nil && options.Bytes == nil && options.Url == "" {
return nil, errors.New("you can only supply buffer, file or url")
}

body := bytes.Buffer{}
var writer *multipart.Writer
if options.File != nil || options.Bytes != nil {
Expand Down Expand Up @@ -70,19 +75,21 @@ func (re *Api) UploadFile(options mod.WaifuvaultPutOpts) (*mod.WaifuResponse[str
"password": options.Password,
}, "")

r, err := createRequest(http.MethodPut, uploadUrl, &body, writer)
r, err := re.createRequest(ctx, http.MethodPut, uploadUrl, &body, writer)
if err != nil {
return nil, err
}
resp, err := client.Do(r)
resp, err := re.client.Do(r)
if err != nil {
return nil, err
}
return getResponse[string](resp)
}

func createRequest(method, url string, body io.Reader, writer *multipart.Writer) (*http.Request, error) {
r, err := http.NewRequest(method, url, body)
func (re *api) createRequest(ctx context.Context, method, url string, body io.Reader, writer *multipart.Writer) (*http.Request, error) {

r, err := http.NewRequestWithContext(ctx, method, url, body)

if err != nil {
return nil, err
}
Expand All @@ -91,45 +98,43 @@ func createRequest(method, url string, body io.Reader, writer *multipart.Writer)
} else {
r.Header.Set("Content-Type", "application/json")
}

return r, nil
}

func (re *Api) FileInfo(token string) (*mod.WaifuResponse[int], error) {
resp, err := re.crateGetRequestForFileInfo(token, false)
func (re *api) FileInfo(ctx context.Context, token string) (*mod.WaifuResponse[int], error) {
resp, err := re.createGetRequestForFileInfo(ctx, token, false)
if err != nil {
return nil, err
}
return getResponse[int](resp)
}

func (re *Api) FileInfoFormatted(token string) (*mod.WaifuResponse[string], error) {
resp, err := re.crateGetRequestForFileInfo(token, true)
func (re *api) FileInfoFormatted(ctx context.Context, token string) (*mod.WaifuResponse[string], error) {
resp, err := re.createGetRequestForFileInfo(ctx, token, true)
if err != nil {
return nil, err
}
return getResponse[string](resp)
}

func (re *Api) crateGetRequestForFileInfo(token string, isFormatted bool) (*http.Response, error) {
func (re *api) createGetRequestForFileInfo(ctx context.Context, token string, isFormatted bool) (*http.Response, error) {
getUrl := getUrl(map[string]any{"formatted": isFormatted}, token)
r, err := createRequest(http.MethodGet, getUrl, nil, nil)
if err != nil {
return nil, err
}
r, err := re.createRequest(ctx, http.MethodGet, getUrl, nil, nil)
if err != nil {
return nil, err
}
return client.Do(r)
return re.client.Do(r)
}

func (re *Api) DeleteFile(token string) (bool, error) {
func (re *api) DeleteFile(ctx context.Context, token string) (bool, error) {
deleteUrl := getUrl(nil, token)

r, err := createRequest(http.MethodDelete, deleteUrl, nil, nil)
r, err := re.createRequest(ctx, http.MethodDelete, deleteUrl, nil, nil)
if err != nil {
return false, err
}
resp, err := client.Do(r)
resp, err := re.client.Do(r)
defer resp.Body.Close()

err = checkError(resp)
Expand All @@ -145,7 +150,7 @@ func (re *Api) DeleteFile(token string) (bool, error) {
return bodyString == "true", nil
}

func (re *Api) GetFile(options mod.GetFileInfo) ([]byte, error) {
func (re *api) GetFile(ctx context.Context, options mod.GetFileInfo) ([]byte, error) {

if options.Filename == "" && options.Token == "" {
return nil, errors.New("please supply a token or a filename")
Expand All @@ -154,14 +159,14 @@ func (re *Api) GetFile(options mod.GetFileInfo) ([]byte, error) {
if options.Filename != "" {
fileUrl = fmt.Sprintf("%s/f/%s", baseUrl, options.Filename)
} else {
fileInfo, err := re.FileInfo(options.Token)
fileInfo, err := re.FileInfo(ctx, options.Token)
if err != nil {
return nil, err
}
fileUrl = fileInfo.URL
}

r, err := createRequest(http.MethodGet, fileUrl, nil, nil)
r, err := re.createRequest(ctx, http.MethodGet, fileUrl, nil, nil)
if err != nil {
return nil, err
}
Expand All @@ -170,7 +175,7 @@ func (re *Api) GetFile(options mod.GetFileInfo) ([]byte, error) {
r.Header.Set("x-password", options.Password)
}

resp, err := client.Do(r)
resp, err := re.client.Do(r)
defer resp.Body.Close()

if err != nil {
Expand Down
79 changes: 57 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,33 @@ This API contains 4 interactions:

The package is namespaced to `waifuVault`, so to import it, simply:

Each function takes a context, if you are unsure what context to use, you can use `context.TODO()`

```go
import "github.com/waifuvault/waifuVault-go-api/pkg"
package main

// then init the API
import (
"context"
"fmt"
waifuVault "github.com/waifuvault/waifuVault-go-api/pkg"
waifuMod "github.com/waifuvault/waifuVault-go-api/pkg/mod" // namespace mod
"net/http"
)

api := waifuVault.Api{}
func main() {
cx, cancel := context.WithCancel(context.Background())
api := waifuVault.NewWaifuvaltApi(http.Client{})
file, err := api.UploadFile(cx, waifuMod.WaifuvaultPutOpts{
Url: "https://waifuvault.moe/assets/custom/images/08.png",
})
if err != nil {
return
}
fmt.Printf(file.URL) // the URL
}
```


### Upload File

To Upload a file, use the `UploadFile` function. This function takes the following options as struct:
Expand All @@ -48,14 +67,16 @@ Using a URL:
package main

import (
"context"
"fmt"
"github.com/waifuvault/waifuVault-go-api/pkg"
waifuMod "github.com/waifuvault/waifuVault-go-api/pkg/mod" // namespace mod
"net/http"
)

func main() {
api := waifuVault.Api{}
file, err := api.UploadFile(waifuMod.WaifuvaultPutOpts{
api := waifuVault.NewWaifuvaltApi(http.Client{})
file, err := api.UploadFile(context.TODO(), waifuMod.WaifuvaultPutOpts{
Url: "https://waifuvault.moe/assets/custom/images/08.png",
})
if err != nil {
Expand All @@ -71,21 +92,23 @@ Using Bytes:
package main

import (
"context"
"fmt"
"github.com/waifuvault/waifuVault-go-api/pkg"
waifuMod "github.com/waifuvault/waifuVault-go-api/pkg/mod"
"net/http"
"os"
)

func main() {
api := waifuVault.Api{}
api := waifuVault.NewWaifuvaltApi(http.Client{})

b, err := os.ReadFile("myCoolFile.jpg")
if err != nil {
fmt.Print(err)
}

file, err := api.UploadFile(waifuMod.WaifuvaultPutOpts{
file, err := api.UploadFile(context.TODO(), waifuMod.WaifuvaultPutOpts{
Bytes: &b,
FileName: "myCoolFile.jpg", // make sure you supply the extension
})
Expand All @@ -102,21 +125,23 @@ Using a file:
package main

import (
"context"
"fmt"
"github.com/waifuvault/waifuVault-go-api/pkg"
waifuMod "github.com/waifuvault/waifuVault-go-api/pkg/mod"
"net/http"
"os"
)

func main() {
api := waifuVault.Api{}
api := waifuVault.NewWaifuvaltApi(http.Client{})

fileStruc, err := os.Open("myCoolFile.jpg")
if err != nil {
fmt.Print(err)
}

file, err := api.UploadFile(waifuMod.WaifuvaultPutOpts{
file, err := api.UploadFile(context.TODO(), waifuMod.WaifuvaultPutOpts{
File: fileStruc,
})
if err != nil {
Expand Down Expand Up @@ -148,18 +173,20 @@ takes the same parameters
package main

import (
"context"
"fmt"
"github.com/waifuvault/waifuVault-go-api/pkg"
"net/http"
)

func main() {
api := waifuVault.Api{}
info, err := api.FileInfo("token")
api := waifuVault.NewWaifuvaltApi(http.Client{})
info, err := api.FileInfo(context.TODO(), "token")
if err != nil {
return
return
}
fmt.Print(info.RetentionPeriod) // the retention period as epoch number
fmt.Print(info.URL) // the URL
fmt.Print(info.URL) // the URL
}
```

Expand All @@ -169,13 +196,15 @@ Human-readable timestamp:
package main

import (
"context"
"fmt"
"github.com/waifuvault/waifuVault-go-api/pkg"
"net/http"
)

func main() {
api := waifuVault.Api{}
info, err := api.FileInfoFormatted("token")
api := waifuVault.NewWaifuvaltApi(http.Client{})
info, err := api.FileInfoFormatted(context.TODO(), "token")
if err != nil {
return
}
Expand All @@ -198,13 +227,15 @@ This function takes the following options as parameters:
package main

import (
"context"
"fmt"
"github.com/waifuvault/waifuVault-go-api/pkg"
"net/http"
)

func main() {
api := waifuVault.Api{}
success, err := api.DeleteFile("token")
api := waifuVault.NewWaifuvaltApi(http.Client{})
success, err := api.DeleteFile(context.TODO(), "token")
if err != nil {
return
}
Expand Down Expand Up @@ -237,22 +268,24 @@ Obtain an encrypted file
package main

import (
"context"
"fmt"
"github.com/waifuvault/waifuVault-go-api/pkg"
waifuMod "github.com/waifuvault/waifuVault-go-api/pkg/mod"
"net/http"
)

func main() {
api := waifuVault.Api{}
api := waifuVault.NewWaifuvaltApi(http.Client{})

// upload the file
file, err := api.UploadFile(waifuMod.WaifuvaultPutOpts{
file, err := api.UploadFile(context.TODO(), waifuMod.WaifuvaultPutOpts{
Url: "https://waifuvault.moe/assets/custom/images/08.png",
Password: "foobar",
})

// download the file
bytes, err := api.GetFile(waifuMod.GetFileInfo{
bytes, err := api.GetFile(context.TODO(), waifuMod.GetFileInfo{
Password: "foobar",
Token: file.Token,
})
Expand All @@ -269,15 +302,17 @@ Obtain a file from Unique identifier
package main

import (
"context"
"fmt"
"github.com/waifuvault/waifuVault-go-api/pkg"
waifuMod "github.com/waifuvault/waifuVault-go-api/pkg/mod"
"net/http"
)

func main() {
api := waifuVault.Api{}
api := waifuVault.NewWaifuvaltApi(http.Client{})

bytes, err := api.GetFile(waifuMod.GetFileInfo{
bytes, err := api.GetFile(context.TODO(), waifuMod.GetFileInfo{
Filename: "/1710111505084/08.png",
})
if err != nil {
Expand Down

0 comments on commit fc9844a

Please sign in to comment.