58 lines
1.3 KiB
Go
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}
|
||
|
}
|