#Multiple Response Types from HTTP request

27 messages · Page 1 of 1 (latest)

cobalt cedar
#

I am currently using the following library: https://github.com/opensauceryafrica/goaxios

I'm quite new to go but have managed to get the hang of a few things, although I'm struggling a bit with types.

This is my current code:

http := goaxios.GoAxios{
        Url:    fmt.Sprintf("https://example.com/test", config.APIVersion, config.Guilds[0]),
        Method: "PATCH",
        Headers: map[string]string{
            "Authorization":      token,
            "User-Agent":         config.Properties.UserAgent,
            "Content-Type":       "application/json",
        },

        ResponseStruct: FailedResponse{},
        Body: map[string]string{
            "code": "test",
        },
    }

    start := time.Now()
    res := http.RunRest()
    elapsed := time.Since(start)

    if res.Response.StatusCode == 429 {
        json := res.Body.(*RatelimitedResponse)

        ratelimit := time.Duration(json.RetryAfter) * time.Second

        logger.Warnf("Ratelimited for %vs while trying to make request", ratelimit)
        time.Sleep(ratelimit)

        if config.Retries > tries {
            tries += 1
            logger.Infof("Retrying request after %v (Retry #%v)", ratelimit, tries)
            doRequest()
            return
        } else {
            logger.Infof("Failed requesting after %v attempts.", config.Retries)
        }
    }

    if res.Response.StatusCode == 400 {
        json := res.Body.(*FailedResponse)

        logger.Warnf("Failed to to make request (Reason: %v, Time: %v)", json.Message, elapsed)
        return
    }

    if res.Response.StatusCode == 200 {
        json := res.Body.(*CodeResponse)

        logger.Infof("%v", json)
        logger.Infof("Successfully made request (Time: %v)", elapsed)
        return
    }
#

These are my structures:

type RatelimitedResponse struct {
    RetryAfter int    `json:"retry_after,omitempty"`
    Message    string `json:"message,omitempty"`
    Code       string `json:"code,omitempty"`
}

type CodeResponse struct {
    Uses int    `json:"uses,omitempty"`
    Code string `json:"code,omitempty"`
}

type FailedResponse struct {
    Code    int    `json:"code,omitempty"`
    Message string `json:"message,omitempty"`
}
#

As you may see, the library I am using has a ResponseStruct option I can pass, but how would I account for different response types such as the above structures?

#

with my current code, i am getting this:

#

Any help will be greatly appreciated, thank you!

ionic ivy
#

I would just use net/http and use an if case to call encoding/json (which you are already doing)

cobalt cedar
#

so unmarshall the body based on response statuscode?

ionic ivy
#

yeah

cobalt cedar
#

i'll give that a try, i heard net/http has some little things i might miss as a beginner

#

particularly closing the body

#

what does a correct http body use look like?

#

if you don't mind of course

#

is this good enough?

ionic ivy
#

just io.ReadAll

#

you should put a limit, because this is DOSable

cobalt cedar
#

read it somewhere, i read the deprecation notice and have switched it back

cobalt cedar
ionic ivy
#

I can send you GiB of data and you will read all of it

cobalt cedar
#

it's an official api that won't send a GiB back

#

but just for future reference, how do i set a limit

ionic ivy
cobalt cedar
#

no problem

#

good news is, i got unmarshalling working with types, thank you very much!

#

net/http isn't as bad as i thought

ionic ivy