Skip to content

Commit 5c345bf

Browse files
author
Harry Lawrence
committed
Initial
0 parents  commit 5c345bf

151 files changed

Lines changed: 21611 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/httpu
2+
.env
3+

LICENSE

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
GOCMD := go
2+
GOBUILD := $(GOCMD) build
3+
GOTEST := $(GOCMD) test
4+
GOGET := $(GOCMD) get
5+
GOINSTALL := $(GOCMD) install
6+
BUILD_WD := cmd/httpu
7+
BIN_PATH := build
8+
BIN_NAME := httpu
9+
BIN_OUT := $(BIN_PATH)/$(BIN_NAME)
10+
BIN_OUT_LINUX := $(BIN_OUT)-linux
11+
GIT_COMMIT := `git rev-parse HEAD`
12+
GIT_BRANCH := `git rev-parse --abbrev-ref HEAD`
13+
META_PACKAGE := github.com/hazbo/httpu/meta.Commit
14+
15+
# Running tests on build for now as it's quick and helpful during development.
16+
all: deps build
17+
18+
build: build/httpu
19+
20+
build/httpu:
21+
cd $(BUILD_WD) && \
22+
$(GOBUILD) -v \
23+
-o ../../$(BIN_OUT) \
24+
-ldflags "-X $(META_PACKAGE)=$(GIT_COMMIT)"
25+
26+
test:
27+
$(GOTEST) -cover -v \
28+
./ \
29+
./resource \
30+
./resource/request \
31+
./utils/varparser
32+
33+
run: $(BIN_OUT)
34+
./$(BIN_OUT) version
35+
36+
clean:
37+
rm -f $(BIN_OUT)
38+
rm -f $(BIN_OUT_LINUX)
39+
40+
deps:
41+
$(GOGET) -v ./...
42+
43+
install: build
44+
cp $(BIN_OUT) $(GOPATH)/bin/$(BIN_NAME)
45+
46+
.PHONY: all build test clean deps install

README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# httpu
2+
3+
httpu is a terminal first, general purpose HTTP client, designed to get you
4+
interacting with all aspects of an HTTP API within just a few seconds. This
5+
makes it a good tool for testing out various endpoints, methods, payloads - so
6+
you are able to see what's being sent, and what response you are then getting.
7+
8+
![httpu](docs/demo.gif)
9+
10+
## Getting started
11+
12+
### Installing
13+
14+
#### macOS
15+
16+
```
17+
brew install hazbo/httpu
18+
```
19+
20+
#### Linux x86_64
21+
22+
23+
### Basic usage
24+
25+
I started writing this project whilst working with the [Moltin API][1], which is
26+
a headless commerce API, so there are a few examples using that, if you want to
27+
play around with that, just head to the website, get your API keys and you are
28+
good to go!
29+
30+
Once httpu has been installed, you can get started either by first pulling down
31+
the preconfigured packages:
32+
33+
```
34+
httpu pull
35+
```
36+
37+
And then loading the configuration of a package into httpu like so:
38+
39+
```
40+
httpu new moltin
41+
```
42+
43+
*or* by creating your own configuration. A basic project may look like the
44+
following:
45+
46+
```
47+
mkdir -p httpbin/requests
48+
touch httpbin/project.json httpbin/requests/ip.json
49+
```
50+
51+
> httpbin/project.json
52+
```
53+
{
54+
"project": {
55+
"url": "https://nghttp2.org/httpbin",
56+
"resourceFiles": [
57+
"httpbin/requests/ip.json"
58+
]
59+
}
60+
}
61+
```
62+
63+
> httpbin/requests/ip.json
64+
```
65+
{
66+
"kind": "request",
67+
"name": "ip",
68+
"spec": {
69+
"uri": "/ip",
70+
"method": "GET"
71+
}
72+
}
73+
```
74+
75+
Once you have that setup, you're ready to run httpu!
76+
77+
```
78+
httpu new httpbin
79+
```
80+
81+
When the UI has loaded, typing in `ip` (the `name` of the request) into the
82+
prompt, followed by hitting enter will run that request and you will be able to
83+
see the response body in the right-hand window. In this case, it will simply
84+
just return your IP address from which the request has been made.
85+
86+
### Advanced usage
87+
88+
httpu is able to look at a JSON response, take a given value and store it in
89+
memory, to then be used for another request. This in-memory store is called the
90+
stash. So for example, in the previous example, if you wanted to store the IP
91+
address that is returned, and use it else where, the request file may look like
92+
this:
93+
94+
> httpbin/requests/ip.json
95+
```
96+
{
97+
"kind": "request",
98+
"name": "ip",
99+
"spec": {
100+
"uri": "/ip",
101+
"method": "GET",
102+
"stashValues": [
103+
{
104+
"name": "my-ip",
105+
"jsonPath": "origin"
106+
}
107+
]
108+
}
109+
}
110+
```
111+
112+
and then in a seperate request you can then access it, once the `ip` request has
113+
been made, like so:
114+
115+
> httpbin/requests/get.json
116+
```
117+
{
118+
"kind": "request",
119+
"name": "get",
120+
"spec": {
121+
"uri": "/get?ip=${stash[ip]}",
122+
"method": "GET"
123+
}
124+
}
125+
```
126+
127+
with `${stash[ip]}` being a variable created after running the `ip` request.
128+
129+
For more examples for advanced usage including the stash, sending request data,
130+
using environment variables etc... head over to the [packages repo][2] and check
131+
out the example I've started creating for the [Moltin API][3].
132+
133+
## Coming soon
134+
- A view to display response headers (priority)
135+
- Creating a request flow
136+
- Creating tests for a request or set of requests
137+
- Various UI tweaks
138+
- An HTTP API interface
139+
140+
## Known issues
141+
142+
This project is in its very early stages, so there will be things that need
143+
fixing. One issue at the moment is a problem with parsing stashed variables
144+
into request data before a request has been made. If this happens, and you are
145+
seeing `${stash[var_name]}`, just fire the request again and it should work.
146+
147+
## Contributing
148+
- Fork httpu
149+
- Create a new branch (`git checkout -b my-feature`)
150+
- Commit your changes (`git commit`)
151+
- Push to your new branch (`git push origin my-feature`)
152+
- Create new pull request
153+
154+
[1]: https://moltin.com/
155+
[2]: https://github.com/httpu/packages
156+
[3]: https://github.com/httpu/packages/tree/master/moltin/requests

api/api.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package api
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/hazbo/httpu/api/requests"
8+
)
9+
10+
func StartServer() {
11+
http.HandleFunc("/requests", requests.GetRequests)
12+
log.Fatal(http.ListenAndServe(":3000", nil))
13+
}

api/requests/requests.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package requests
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
10+
"github.com/hazbo/httpu/resource"
11+
"github.com/hazbo/httpu/resource/request"
12+
)
13+
14+
func GetRequests(rw http.ResponseWriter, r *http.Request) {
15+
var rs request.Requests
16+
for _, r := range resource.Requests {
17+
rs = append(rs, r)
18+
}
19+
j, err := json.Marshal(rs)
20+
if err != nil {
21+
log.Fatal(err)
22+
return
23+
}
24+
25+
var out bytes.Buffer
26+
json.Indent(&out, j, "", " ")
27+
28+
fmt.Fprint(rw, string(out.String()))
29+
}

build/.gitkeep

Whitespace-only changes.

build/httpu-0.0.1.tar.gz

181 Bytes
Binary file not shown.

cmd/httpu/commands/commands.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package commands
2+
3+
// Command defines the functionality and useage text for any given subcommand.
4+
// These may be from either built-in commands, inline or external commands.
5+
type Command struct {
6+
Usage func(string)
7+
RunMethod func([]string) error
8+
}
9+
10+
// Run will run the given command with the arguments passed
11+
func (cmd *Command) Run(args []string) error {
12+
return cmd.RunMethod(args)
13+
}
14+
15+
// CommandMap defines the string key for a given command, which is then used
16+
// when invoking the cnf commands.
17+
type CommandMap map[string]*Command
18+
19+
// Commands is the list of commands within a map
20+
var Commands = CommandMap{
21+
"new": newCmd,
22+
"pull": pullCmd,
23+
"version": versionCmd,
24+
}

cmd/httpu/commands/new.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package commands
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
8+
"github.com/hazbo/httpu"
9+
"github.com/hazbo/httpu/ui"
10+
"github.com/joho/godotenv"
11+
)
12+
13+
var newFlagSet = flag.NewFlagSet("new", flag.ExitOnError)
14+
15+
var (
16+
newEnvFile = newFlagSet.String(
17+
"e", "", "Loads .env file to start httpu with environment variables")
18+
)
19+
20+
func newValue(args []string) error {
21+
newFlagSet.Parse(args)
22+
23+
// Get the first argument for the `new` command
24+
p := args[0]
25+
26+
if *newEnvFile != "" {
27+
err := godotenv.Load(*newEnvFile)
28+
if err != nil {
29+
return fmt.Errorf("Could not find .env file: %s", *newEnvFile)
30+
}
31+
32+
// If an env file has been passed, this becomes argument 2, over 0.
33+
p = args[2]
34+
}
35+
36+
if len(args) == 0 {
37+
fmt.Printf("Expecting 1 argument, 0 passed")
38+
os.Exit(1)
39+
}
40+
41+
err := httpu.ConfigureFromFile(p)
42+
if err != nil {
43+
return err
44+
}
45+
46+
// Start the terminal user interface!
47+
ui.New().Start()
48+
49+
return nil
50+
}
51+
52+
var newCmd = &Command{
53+
Usage: func(arg0 string) {
54+
fmt.Printf("Usage: %s list [<option>...]\n\nOptions:\n", arg0)
55+
newFlagSet.PrintDefaults()
56+
},
57+
RunMethod: func(args []string) error {
58+
return newValue(args)
59+
},
60+
}

0 commit comments

Comments
 (0)