abs-lang / abs

Home of the ABS programming language: the joy of shell scripting.
https://www.abs-lang.org
MIT License
516 stars 35 forks source link

Add a function for GET requests? #469

Open Iskander0 opened 2 years ago

Iskander0 commented 2 years ago

Now you can do curl '$url', but it's faster to have a function for GETting built into ABS :

import "github.com/valyala/fasthttp"

// httpGET(string:"https://www.example.com")
func httpGETFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
    err := validateArgs(tok, "httpGET", args, 1, [][]string{{object.STRING_OBJ}})
    if err != nil { return err }

    url := args[0].(*object.String)

    readTimeout, _ := time.ParseDuration("1000ms")
    writeTimeout, _ := time.ParseDuration("1000ms")
    maxIdleConnDuration, _ := time.ParseDuration("1h")
    var client *fasthttp.Client
    client = &fasthttp.Client{
        ReadTimeout:                   readTimeout,
        WriteTimeout:                  writeTimeout,
        MaxIdleConnDuration:           maxIdleConnDuration,
        NoDefaultUserAgentHeader:      true, // Don't send: User-Agent: fasthttp
        DisableHeaderNamesNormalizing: true, // If you set the case on your headers correctly you can enable this
        DisablePathNormalizing:        true,
        // increase DNS cache time to an hour instead of default minute
        Dial: (&fasthttp.TCPDialer{ Concurrency:      4096, DNSCacheDuration: time.Hour, }).Dial,
    }

    req := fasthttp.AcquireRequest()
    req.SetRequestURI(url.Value)
    req.Header.SetMethod(fasthttp.MethodGet)
    resp := fasthttp.AcquireResponse()
    err1 := client.Do(req, resp)
    if err1 != nil { return &object.String{Token: tok, Value: err1.Error() } }
    fasthttp.ReleaseRequest(req)
    defer fasthttp.ReleaseResponse(resp)

    return &object.String{Token: tok, Value: string(resp.Body())}       
}
odino commented 2 years ago

I don't really mind getting some sort of basic http client into th standard library -- I'd be open to RFC / proposals / PRs around it :)

On Sat, Mar 5, 2022, 4:32 AM Agathon @.***> wrote:

Now you can do curl '$url', but it's faster to have a function for GETting built into ABS :

`import "github.com/valyala/fasthttp"

........

// httpGET(string:"https://www.example.com") func httpGETFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object { err := validateArgs(tok, "httpGET", args, 1, [][]string{{object.STRING_OBJ}}) if err != nil { return err }

url := args[0].(*object.String)

readTimeout, := time.ParseDuration("1000ms") writeTimeout, := time.ParseDuration("1000ms") maxIdleConnDuration, _ := time.ParseDuration("1h") var client *fasthttp.Client client = &fasthttp.Client{ ReadTimeout: readTimeout, WriteTimeout: writeTimeout, MaxIdleConnDuration: maxIdleConnDuration, NoDefaultUserAgentHeader: true, // Don't send: User-Agent: fasthttp DisableHeaderNamesNormalizing: true, // If you set the case on your headers correctly you can enable this DisablePathNormalizing: true, // increase DNS cache time to an hour instead of default minute Dial: (&fasthttp.TCPDialer{ Concurrency: 4096, DNSCacheDuration: time.Hour, }).Dial, }

req := fasthttp.AcquireRequest() req.SetRequestURI(url.Value) req.Header.SetMethod(fasthttp.MethodGet) resp := fasthttp.AcquireResponse() err1 := client.Do(req, resp) if err1 != nil { return &object.String{Token: tok, Value: err1.Error() } } fasthttp.ReleaseRequest(req) defer fasthttp.ReleaseResponse(resp)

return &object.String{Token: tok, Value: string(resp.Body())}

}

`

— Reply to this email directly, view it on GitHub https://github.com/abs-lang/abs/issues/469, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACQFZCXW3A75XJB6BCFMKTU6KTS3ANCNFSM5P64WECA . You are receiving this because you are subscribed to this thread.Message ID: @.***>