Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package script

import (
"bufio"
"bytes"
"container/ring"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand All @@ -17,6 +20,7 @@ import (
"strings"
"sync"
"text/template"
"time"

"bitbucket.org/creachadair/shell"
"github.com/itchyny/gojq"
Expand Down Expand Up @@ -86,6 +90,20 @@ func NewPipe() *Pipe {
}
}

type HttpRequest struct {
URL string
Method string
}

// Returns pointer to a new HttpRequest.
// Defaults to HTTP GET.
func NewHttpRequest(url string) *HttpRequest {
return &HttpRequest{
URL: url,
Method: "GET",
}
}

// Close closes the pipe's associated reader. This is a no-op if the reader is
// not also a Closer.
func (p *Pipe) Close() error {
Expand Down Expand Up @@ -545,9 +563,10 @@ func (p *Pipe) First(n int) *Pipe {
// easier to read:
//
// 10 apple
// 4 banana
// 2 orange
// 1 kumquat
//
// 4 banana
// 2 orange
// 1 kumquat
func (p *Pipe) Freq() *Pipe {
freq := map[string]int{}
type frequency struct {
Expand Down Expand Up @@ -838,3 +857,48 @@ func (p *Pipe) writeOrAppendFile(fileName string, mode int) (int64, error) {
}
return wrote, nil
}

// Instantiates an HttpRequest object, sets the target URL, and defaults
// the HTTP Method to GET.
func Http(url string) *HttpRequest {
h := NewHttpRequest(url)

return h
}

// Sends an HTTP GET request to the target resource (URL).
func (h *HttpRequest) Get() (int, []byte, error) {
return h.send()
}

// Sends HTTP request using the underlying HTTP client.
// When an error is returned, []byte (body) will be nil.
func (h *HttpRequest) send() (int, []byte, error) {
var b *bytes.Buffer = &bytes.Buffer{}

req, err := http.NewRequest(h.Method, h.URL, b)

if err != nil {
return http.StatusInternalServerError, nil, err
}

client := &http.Client{
Timeout: 30 * time.Second,
}

resp, err := client.Do(req)

if err != nil {
return resp.StatusCode, nil, err
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)

if err != nil {
return resp.StatusCode, nil, err
}

return resp.StatusCode, body, nil
}
18 changes: 18 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,24 @@ func TestReadReturnsEOFOnUninitialisedPipe(t *testing.T) {
}
}

func TestHttpGetRequest(t *testing.T) {
t.Parallel()
expectedStatusCode := 200
statusCode, body, err := script.Http("https://postman-echo.com/get?foo1=bar1&foo2=bar2").Get()

if err != nil {
t.Fatal(err)
}

if statusCode != expectedStatusCode {
t.Errorf("Expected status code %d but got %d", expectedStatusCode, statusCode)
}

if body == nil {
t.Error("Expected body to not be nil")
}
}

func ExampleArgs() {
script.Args().Stdout()
// prints command-line arguments
Expand Down