added get loop and new key type xor
This commit is contained in:
parent
8f350b4b02
commit
9e68b46346
8 changed files with 122 additions and 24 deletions
|
|
@ -15,6 +15,8 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.giftfish.de/ston1th/keyctl/pkg/api/types"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
|
|
@ -181,14 +183,47 @@ func (c *Client) Do(r *http.Request, v any) (resp *http.Response, err error) {
|
|||
}
|
||||
|
||||
func (c *Client) FollowRedirect(r *http.Request, v any, delay time.Duration) (resp *http.Response, err error) {
|
||||
rr := r
|
||||
ctx := r.Context()
|
||||
rr := r.Clone(ctx)
|
||||
for {
|
||||
resp, err = c.Do(rr, v)
|
||||
if err == nil && resp.StatusCode == http.StatusOK {
|
||||
return
|
||||
}
|
||||
rdr, ok := err.(Redirect)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
rr, err = c.NewRequest(r.Context(), "GET", rdr.Location, nil)
|
||||
rr, err = c.NewRequest(ctx, "GET", rdr.Location, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(delay)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) FollowRedirectLoop(r *http.Request, v any, delay time.Duration) (resp *http.Response, err error) {
|
||||
ctx := r.Context()
|
||||
rr := r.Clone(ctx)
|
||||
for {
|
||||
resp, err = c.Do(rr, v)
|
||||
rdr, ok := err.(Redirect)
|
||||
if err != nil && !ok {
|
||||
if errors.Is(err, types.ErrISE) ||
|
||||
errors.Is(err, types.ErrKeyNotFound) {
|
||||
return
|
||||
}
|
||||
if errors.Is(err, types.ErrReqNotFound) ||
|
||||
errors.Is(err, types.ErrKeyRequestRejected) {
|
||||
rr = r.Clone(ctx)
|
||||
}
|
||||
time.Sleep(delay)
|
||||
continue
|
||||
}
|
||||
if err == nil && resp.StatusCode == http.StatusOK {
|
||||
return
|
||||
}
|
||||
rr, err = c.NewRequest(ctx, "GET", rdr.Location, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue