Ink declarative HTTP client library


(Ashish Shekar) #1

Hello, I wanted to share with you guys a project that I have been brainstorming and creating a working prototype of the same. It is called ink - https://github.com/codekidX/ink.

While writing a CLI for one of my project I came to know that I was being too repetitive writing a HTTP client and it was not a good experience. I like programming in Go so I wrote a declarative client library. The idea came from React in which a component can be modularised and reused anywhere possible.

Any HTTP request in ink is a struct (extending ink.RequestEntity) defining the requirements of an API call. Let’s define a response struct too so that we can infer.

Example:

type GetPlayerEntity struct {
  ink.RequestEntity
  Id string `ink:"id|query"`
}

type GetPlayerResponse struct {
  Name string `json:"name"`
  Id string `json:"id"`
  IsLegend bool `json:"isLegend"`
}

making a request with these structs

package main

import "github.com/codekidX/ink"

var inkcl = ink.NewClient("http://localhost:3000", time.Second*30)

func main() {
  var gpeResponse GetPlayerResponse
  gpe := GetPlayerEntity{
    Id: 1,
  }
  gpe.Route("/player")
  gpe.Infer = &gpeResponse

  resp, err := inkcl.Get(gpe)
  if err == nil {
    // Do something with gpeResponse ...
  }
}

One more thing I have been writing is a server-side validation library using the same RequestEntity structs so that client and server can share the requirements. This is my first time using the reflect package, so something is bound to be wrong. Do let me know your views and what improvements can be done. Thanks for reading through.