A very simple and powerful package for making REST requests with very little effort

Welcome to KRest

KRest stands for Keep it simple REST Package.

It's a very simple and powerful package wrapper over the standard http package for making requests in an easier and less verbose way.

Sample requests

Simple requests using krest look like this:

func main() {
  // Build the client with a maximum request timeout limit of 2 seconds
  // You may specify a shorter timeout on each request using the context.
  rest := krest.New(2*time.Second)

  user, err := getUser(rest)
  if err != nil {
    log.Fatalf("unable to get user: %s", err)
  }

  err := sendUser(rest, user)
  if err != nil {
    log.Fatalf("unable to send user: %s", err)
  }
}

func getUser(rest krest.Provider) (model.User, error) {
  resp, err := rest.Get("https://example.com/user", krest.RequestData{})
  if err != nil {
    // An error is returned for any status not in range 200-299,
    // and it is safe to use the `resp` value even when there are errors.
    if resp.StatusCode == 404 {
      log.Fatalf("example.com was not found!")
    }
    // The error message contains all the information you'll need to understand
    // the error, such as Method, Request URL, response status code and even
    // the raw Payload from the error response:
    log.Fatalf("unexpected error when fetching example.com: %s", err)
  }

  // Using intermediary structs for decoding payloads like this one
  // is recomended for decoupling your internal models from the external
  // payloads:
  var parsedUser struct{
    Name string     `json:"name"`
    Age string      `json:"age"`
    Address Address `json:"address"`
  }
  err := json.Unmarshal(resp.Body, &parsedUser)
  if err != nil {
    return model.User{}, fmt.Errorf("unable to parse example user response as JSON: %s", err)
  }

  // Decode the age that was passed as string to an internal
  // format that is easier to manipulate:
  age, _ := strconv.Atoi(parsedUser.Age)

  return model.User{
    Name:    parsedUser.Name,
    Age:     age,
    Address: parsedUser.Address,
  }, nil
}

func sendUser(rest krest.Provider, user model.User) error {
  resp, err := rest.Post("https://other.example.com", krest.RequestData{
    Headers: map[string]string{
      "Authorization": "Bearer some-valid-jwt-token-goes-here",
    },

    // Using the optional retry feature:
    MaxRetries: 3,

    // Again using intermediary structs (or in this case a map) is also recommended
    // for encoding messages to match other APIs so you can keep your internal models
    // decoupled from any external dependencies:
    Body: map[string]interface{}{
      "fullname": user.Name,
      "address": user.Address,
    }
  })
  if err != nil {
    // Again this error message will already contain the info you might need to debug
    // but it is always a good idea to add more information when available:
    return fmt.Errorf("error sending user to example.com: %s", err)
  }

  return nil
}
Owner
Similar Resources

Simple REST-API implementation using Golang with several packages (Echo, GORM) and Docker

Simple REST-API Boilerplate This is a simple implementation of REST-API using Golang and several packages (Echo and GORM). By default, I use PostgreSQ

Sep 13, 2022

A Simple REST API Build Using Golang

gorestapi A Simple REST API Build Using Golang About gorestapi: a simple music restapi that retrives info about the author, album name, price of it ge

Nov 21, 2021

Simple REST API to get time from many different timezones

Timezone API Simple REST API for getting current time in different timezones. This is the first assignment from Rest-Based Microservices API Developme

Oct 31, 2021

Example of REST API to collect simple data

Statistics Collector An example of REST API to collect views statistics How to build There is Docker file you can set TARGET_OS and TARGET_ARCH to com

Dec 6, 2021

🥒 Simple REST-API product service (download products in .csv file)

🥒 Simple REST-API product service (download products in .csv file)

Nov 28, 2021

Go fiber simple rest API

Go fiber simple rest API

Go Fiber Gorm CRUD Example Go 1.17 Fiber v2 Gorm with sqlite database driver Swa

Jul 15, 2022

A simple REST API - Go

go-rest-api A simple REST API Features GET /movies returns list of movies GET /movies/{id} returns details of specific movie as JSON POST /movies acce

Dec 4, 2022

GateCracker-REST - A RESTful API example for simple lock model information application with Go

GateCracker-REST - A RESTful API example for simple lock model information application with Go

Go Lock Models REST API Example A RESTful API example for simple lock model info

Jun 11, 2022

REST api using fiber framework written in golang and using firebase ecosystem to authentication, storage and firestore as a db and use clean architecture as base

REST api using fiber framework written in golang and using firebase ecosystem to authentication, storage and firestore as a db and use clean architecture as base

Backend API Example FiberGo Framework Docs : https://github.com/gofiber Info This application using firebase ecosystem Firebase Auth Cloud Storage Fir

May 31, 2022
Related tags
Best simple, lightweight, powerful and really fast Api with Golang (Fiber, REL, Dbmate) PostgreSqL Database and Clean Architecture

GOLANG FIBER API (CLEAN ARCHITECTURE) Best simple, lightweight, powerful and really fast Api with Golang (Fiber, REL, Dbmate) PostgreSqLDatabase using

Sep 2, 2022
REST Layer, Go (golang) REST API framework
REST Layer, Go (golang) REST API framework

REST Layer REST APIs made easy. REST Layer is an API framework heavily inspired by the excellent Python Eve. It helps you create a comprehensive, cust

Dec 16, 2022
An app skeleton for very simple golang web applications

Golang App Skeleton This is a skeleton for a golang web application optimized for simplicity and rapid development. Prerequisites Go 1.15 or greater O

Oct 16, 2022
GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.

GoFrame English | 简体中文 GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang. If you're a

Jan 2, 2023
A powerful go web framework for highly scalable and resource efficient web application

webfr A powerful go web framework for highly scalable and resource efficient web application Installation: go get -u github.com/krishpranav/webfr Exa

Nov 28, 2021
A streamlined and powerful golang framework

A streamlined and powerful golang framework

Jan 11, 2022
A powerful go web framework for highly scalable and resource efficient web application

A powerful go web framework for highly scalable and resource efficient web application

Oct 3, 2022
skr: The lightweight and powerful web framework using the new way for Go.Another go the way.
skr: The lightweight and powerful web framework using the new way for Go.Another go the way.

skr Overview Introduction Documents Features Install Quickstart Releases Todo Pull Request Issues Thanks Introduction The lightweight and powerful web

Jan 11, 2022
package for building REST-style Web Services using Go

go-restful package for building REST-style Web Services using Google Go Code examples using v3 REST asks developers to use HTTP methods explicitly and

Jan 1, 2023