Skip to content

Simple web api client framework for go programming language

Notifications You must be signed in to change notification settings

rikkkky/go-httpflow

 
 

Repository files navigation

httpflow

Build Status codecov GoDoc

Simple web api client framework for go programming language.

Example:

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"strconv"

	"github.com/k0kubun/pp"
	httpflow "github.com/karupanerura/go-httpflow"
)

type User struct {
	ID   json.Number `json:"id"`
	Name string      `json:"name"`
}

type UsersGetInstancesSession struct {
	httpflow.NobodyRequestBuilder
	httpflow.JSONResponseHandler
}

func NewUsersGetInstancesSession(id int) *UsersGetInstancesSession {
	netURL, err := url.Parse("https://jsonplaceholder.typicode.com/users/" + strconv.Itoa(id))
	if err != nil {
		panic(err)
	}

	return &UsersGetInstancesSession{
		NobodyRequestBuilder: httpflow.NobodyRequestBuilder{
			RequestMethod: http.MethodGet,
			RequestURL:    netURL,
		},
		JSONResponseHandler: httpflow.JSONResponseHandler{},
	}
}

func (r *UsersGetInstancesSession) ParseBody() (*User, error) {
	var body User
	err := r.DecodeJSON(&body)
	if err != nil {
		return nil, err
	}

	return &body, err
}

func main() {
	agent := httpflow.NewAgent(http.DefaultClient)
	session := NewUsersGetInstancesSession(1)
	err := agent.RunSession(session)
	if err != nil {
		log.Fatal(err)
	}

	res, err := session.ParseBody()
	if err != nil {
		log.Fatal(err)
	}

	pp.Print(res)
}

About

Simple web api client framework for go programming language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%