Coder Social home page Coder Social logo

simpleresty's Introduction

Actions Status Go Report Card License

A simple wrapper around go-resty.

Background

Having used go-resty to create clients for various service APIs, I noticed a common set of methods/functions I would define in each API client. I extracted those methods/functions and moved them into this separate library. This way, all my clients could benefit from using a single library to interface with the APIs.

I have embedded resty.Client into simpleresty.Client so all of resty's functions/methods are available to the user. In fact, simpleresty.New() returns a resty.Client.

Example

package main

import (
	"fmt"
	"github.com/davidji99/simpleresty"
)

type GithubAPIResponse struct {
	CurrentUserURL string `json:"current_user_url,omitempty"`
}

func main() {
	c := simpleresty.New()

	var result *GithubAPIResponse
	response, getErr := c.Get("https://api.github.com", &result, nil)
	if getErr != nil {
		panic(getErr)
	}

	fmt.Println(response.StatusCode) // Returns 200
	fmt.Println(result.CurrentUserURL) // Returns 'https://api.github.com/user'
}

Additional examples can be found in the /examples folder.

You can also check out rollrest-go, which uses this library to implement an API rest client for Rollbar.

Proxy

simpleresty respects any proxy URLs set in your environment in this order of preference:

  1. HTTPS_PROXY
  2. https_proxy
  3. HTTP_PROXY
  4. http_proxy

Only a single value from one of the above four environment variables will be used to set the proxy URL on the Client.

simpleresty will also respect domains (not IP addresses or CIDR ranges) defined by the NO_PROXY or no_proxy environment variable. Multiple domains must be separated by a comma.

Go-Resty

As this pkg is a thin wrapper around go-resty, all of its methods are available to use in this package. Please refer to go-resty's documentation for more information.

simpleresty's People

Contributors

davidji99 avatar dependabot[bot] avatar ybocalandro avatar

Stargazers

 avatar

Watchers

 avatar

simpleresty's Issues

Something wrong with url setting

Describe the bug
I used your example from the Readme to try out your lib but could not get it to work. After debugging, I found out that the URL is not set appropriate. After removing the c.SetBaseURL("https://api.github.com") line it worked.

To Reproduce
Steps to reproduce the behaviour just run

package main

import (
	"fmt"

	"github.com/davidji99/simpleresty"
)

type GithubAPIResponse struct {
	CurrentUserURL string `json:"current_user_url,omitempty"`
}

func main() {
	c := simpleresty.New()
	c.SetBaseURL("https://api.github.com") // This causes the error

	var result *GithubAPIResponse
	response, getErr := c.Get("https://api.github.com", &result, nil)

	fmt.Println(getErr)

	fmt.Println(response.StatusCode)
	fmt.Println(result.CurrentUserURL)
	fmt.Println(response.Body)
}

Expected behaviour
Schema error as URL is just "" empty string

Version

module foobar

go 1.14

require github.com/davidji99/simpleresty v0.2.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.