This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/internal/e621/task.go
SoXX 802764092e refactor_folder_structure_#11 (#14)
As mentioned in Issue #11, the folder structure got an overall as some file names

Co-authored-by: Fenpaws <soxx@fenpa.ws>
Reviewed-on: anthrove/e621-to-graph#14
Reviewed-by: Lennard Brinkhaus <lennard.brinkhaus@noreply.localhost>
Co-authored-by: SoXX <fenpaws@noreply.localhost>
Co-committed-by: SoXX <fenpaws@noreply.localhost>
2023-07-17 10:57:23 +00:00

58 lines
1.3 KiB
Go

package e621
import (
"encoding/json"
"git.dragse.it/anthrove/e621-to-graph/pkg/e621"
pgkError "git.dragse.it/anthrove/e621-to-graph/pkg/error"
"net/http"
)
type e621APITask[T e621.DataType] struct {
uri string
}
func NewE621ApiTask[T e621.DataType](uri string) e621.Task[T] {
return &e621APITask[T]{
uri: uri,
}
}
func (e e621APITask[T]) UriPath() string {
return e.uri
}
func (e e621APITask[T]) HandleStatusCode(statusCode int) e621.DataResponse[T] {
var err error
switch statusCode {
case 421:
err = pgkError.RateLimitReachedError{}
case 424:
err = pgkError.InvalidParametersError{}
case 520:
err = pgkError.OriginConnectionTimeOutError{}
case 522:
err = pgkError.OriginConnectionTimeOutError{}
case 524:
err = pgkError.OriginConnectionTimeOutError{}
case 525:
err = pgkError.SSLHandshakeFailedError{}
default:
err = pgkError.StatusCodesToError(statusCode)
}
return e621.DataResponse[T]{Error: err}
}
func (e e621APITask[T]) HandleResponse(responseData *http.Response) e621.DataResponse[T] {
var data T
err := json.NewDecoder(responseData.Body).Decode(&data)
defer responseData.Body.Close()
if err != nil {
return e621.DataResponse[T]{Error: err}
}
return e621.DataResponse[T]{Data: data}
}
func (e e621APITask[T]) HandleError(error error) e621.DataResponse[T] {
return e621.DataResponse[T]{Error: error}
}