Skip to content

aanzolaavila/splitwise.go

Repository files navigation

splitwise.go

Quality Gate Status Coverage Reliability Rating Code Smells Vulnerabilities Bugs Security Rating Maintainability Rating Lines of Code Technical Debt Duplicated Lines (%) GitHub repo size GitHub

Yet another community driven Golang SDK for Splitwise 3rd-party APIs made in Go. Inspired from the work done by anvari1313.

How do I start to use it?

  1. Get this package into your project's dependencies
$ go get -u github.com/aanzolaavila/splitwise.go
  1. Get you application credentials. To get them, register you app in here.

    You can either use ApiKeyAuth, recommended for testing.

    Or, you can use the much more secure option with OAuth, in this example you can see how you can set it up.

Examples

Get your app credentials, to use the examples, you can run them like this

With API key

$ TOKEN="<your-api-token>" make examples

With OAuth 2.0

For this method, make sure that the Callback URI is the same one as this line

$ CLIENT_ID="<client-id>" CLIENT_SECRET="<client-secret>" make examples
# The app will ask you to open a link in the web browser,
# it will redirect you to a page with a URI that looks
# like https://localhost:17000/oauth/redirect/?code=YOUR_CODE&state=state

# copy YOUR_CODE into the terminal and it will execute the examples

With third party JSON library

By default, the used library for marshaling and unmarshaling JSON is the native one, but in case you want to include another such as go-json, jsoniter, sonic, among others that might exist, you can specify another one.

For this you just need to point out the marshaling and unmarshaling methods like:

import (
   	gojson "github.com/goccy/go-json"
)

// ...

client.JsonMarshaler = gojson.Marshal
client.JsonUnmarshaler = gojson.Unmarshal

Both fields use different interfaces, which are the same as the ones in the standard Go library.

type jsonMarshaler func(interface{}) ([]byte, error)
type jsonUnmarshaler func([]byte, interface{}) error

type Client struct {
   // ...
   JsonMarshaler   jsonMarshaler
   JsonUnmarshaler jsonUnmarshaler
}

See a concrete example here.

With custom logger

You can specify your desired logger implementation, it just has to fulfill the following interface.

type logger interface {
	Printf(string, ...interface{})
}

You can find an example in here.

You can also use another logging solution such as logrus. See this example for reference:

import (
   "log"

   logrus "github.com/sirupsen/logrus"
)

// ...

logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}

// Use logrus for standard log output
// Note that `log` here references stdlib's log
// Not logrus imported under the name `log`.
stdlog := log.New(os.Stdout, "", log.Lshortfile)
stdlog.SetOutput(logger.Writer())

splitwiseClient.Logger = stdlog

Based from a logrus example.

Disable logging

If you wish to disable logging, you can do this

import "log"

// ...

nopLogger := log.New(io.Discard, "", log.LstdFlags)
splitwiseClient.Logger = nopLogger

Or you can implement a nopLogger struct that fulfills the logger interface

type nopLogger struct {}

func (l nopLogger) Printf(s string, args ...interface{}) {
   return // nothing to do here
}

// ...

splitwiseClient.Logger = nopLogger

How to collaborate?

You can create a PR to this repo for corrections or improvements. One of the main guidelines for approving them is that all tests must pass.

Execute the tests

Every test uses either a mock or a httptest server for responding with custom responses.

$ make test
# or
$ go test .

Show coverage information

To generate and open coverage reports you can do

$ make coverage
# or
$ go test -v -cover -coverprofile=coverage.out .
$ go tool cover -html=coverage.out

This will open the web browser with the coverage report.

About

Golang SDK for Splitwise 3rd party integration

Resources

License

Stars

Watchers

Forks

Packages

No packages published