Skip to content

Commit

Permalink
Addeds support for API PRO and dependencies update
Browse files Browse the repository at this point in the history
  • Loading branch information
r--w committed Dec 8, 2022
1 parent fd3a8da commit b0c0377
Show file tree
Hide file tree
Showing 46 changed files with 225 additions and 8,962 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
time: "04:00"
open-pull-requests-limit: 10
40 changes: 40 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Check & test & build
on:
push:
branches:
- master
pull_request:

jobs:
check:
name: Quality & security checks
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
check-latest: true

- name: Check out code
uses: actions/checkout@v3

- name: Lint Go Code
run: make check

test:
name: Test & coverage
runs-on: ubuntu-latest

steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
check-latest: true

- name: Check out code
uses: actions/checkout@v3

- name: Run unit tests with
run: make test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
*.out

### IDE ###
.idea/
.idea/
/bin/
36 changes: 36 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
run:
concurrency: 4
timeout: 3m

linters:
disable-all: true
enable:
- asciicheck
- bodyclose
- durationcheck
- errcheck
- errorlint
- forbidigo
- goimports
- gosec
- gosimple
- govet
- nilerr
- predeclared
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- whitespace

linters-settings:
forbidigo:
forbid:
- ^(fmt\.Print.*|print|println)$
- ^spew\.Dump$
- ^pp\.(P|PP|Sp|Fp)rint?$

issues:
exclude:
- (.*)should have comment(.+)or be unexported|comment(.+)should be of the form
6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SHELL := /bin/bash
export GO111MODULE=on
export GOPROXY=https://proxy.golang.org

.PHONY: check format help test tidy

format: ## Format go code with goimports
@go install golang.org/x/tools/cmd/goimports@latest
@goimports -l -w .

test: ## Run tests
@go test -race ./...

tidy: ## Run go mod tidy
@go mod tidy

check: ## Linting and static analysis
@if test ! -e ./bin/golangci-lint; then \
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh; \
fi

@./bin/golangci-lint run -c .golangci.yml

help: ## Show help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,19 @@ customClient := &http.Client{Timeout: 10 * time.Second}
paprikaClient := coinpaprika.NewClient(customClient)
```

## Setting API key for and enabling access to Coinpaprika Pro API
Key can be obtained from [Coinpaprika API](https://coinpaprika.com/api/)

```go
paprikaClient := coinpaprika.NewClient(nil, coinpaprika.WithAPIKey("your_api_key_goes_here"))
```

## Examples

Check out the [`./examples`](./examples) directory.


## Implementation status (API v1.4.3)
## Implementation status

### Global
- [x] Get market overview data
Expand Down
79 changes: 61 additions & 18 deletions coinpaprika/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package coinpaprika

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"reflect"
Expand All @@ -11,29 +11,50 @@ import (
)

const (
userAgent = "Coinpaprika API Client - Go"
baseURL = "https://api.coinpaprika.com/v1"
userAgent = "Coinpaprika API Client - Go"
baseFreeURL = "https://api.coinpaprika.com/v1"
baseProURL = "https://api-pro.coinpaprika.com/v1"
)

var baseURL = baseFreeURL

// Client can be used to get data from coinpaprika API.
type Client struct {
httpClient *http.Client
Tickers TickersService
Search SearchService
PriceConverter PriceConverterService
Coins CoinsService
Global GlobalService
Tags TagsService
People PeopleService
Exchanges ExchangesService
}
type (
Client struct {
httpClient *http.Client
Tickers TickersService
Search SearchService
PriceConverter PriceConverterService
Coins CoinsService
Global GlobalService
Tags TagsService
People PeopleService
Exchanges ExchangesService
}
)

type (
service struct {
httpClient *http.Client
}
)

type (
authTransport struct {
baseTransport http.RoundTripper
apiKey string
}
)

type service struct {
httpClient *http.Client
func (t *authTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", t.apiKey)
return t.baseTransport.RoundTrip(req)
}

type ClientOptions func(a *Client)

// NewClient creates a new client to work with coinpaprika API.
func NewClient(httpClient *http.Client) *Client {
func NewClient(httpClient *http.Client, opts ...ClientOptions) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
Expand All @@ -42,6 +63,10 @@ func NewClient(httpClient *http.Client) *Client {
httpClient: httpClient,
}

for _, opt := range opts {
opt(c)
}

c.Tickers.httpClient = c.httpClient
c.Search.httpClient = c.httpClient
c.PriceConverter.httpClient = c.httpClient
Expand All @@ -54,6 +79,24 @@ func NewClient(httpClient *http.Client) *Client {
return c
}

// WithAPIKey sets API key enabling access to Coinpaprika Pro API.
// https://api-pro.coinpaprika.com is used.
func WithAPIKey(apiKey string) ClientOptions {
return func(a *Client) {
baseURL = baseProURL

baseTransport := http.DefaultTransport
if a.httpClient.Transport != nil {
baseTransport = a.httpClient.Transport
}

a.httpClient.Transport = &authTransport{
apiKey: apiKey,
baseTransport: baseTransport,
}
}
}

func constructURL(rawURL string, options interface{}) (string, error) {
if v := reflect.ValueOf(options); v.Kind() == reflect.Ptr && v.IsNil() {
return rawURL, nil
Expand Down Expand Up @@ -87,7 +130,7 @@ func sendGET(client *http.Client, url string) ([]byte, error) {

defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion coinpaprika/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ func (s *GlobalService) Get() (g *GlobalStats, err error) {

err = json.Unmarshal(body, &g)
return g, err

}
27 changes: 27 additions & 0 deletions examples/pro_api/pro_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"

"github.com/coinpaprika/coinpaprika-api-go-client/v2/coinpaprika"
)

func main() {
paprikaClient := coinpaprika.NewClient(nil, coinpaprika.WithAPIKey("your_api_key_goes_here"))

tickers, err := paprikaClient.Tickers.List(nil)
if err != nil {
panic(err)
}

for _, t := range tickers {
if t.Name == nil || t.Symbol == nil || t.Rank == nil {
continue
}

fmt.Println("Name:", *t.Name)
fmt.Println("Symbol:", *t.Symbol)
fmt.Println("Rank:", *t.Rank)
fmt.Println("----")
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module github.com/coinpaprika/coinpaprika-api-go-client/v2

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-querystring v1.0.0
github.com/stretchr/testify v1.3.0
github.com/google/go-querystring v1.1.0
github.com/stretchr/testify v1.8.1
)

go 1.13
22 changes: 17 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 0 additions & 15 deletions vendor/github.com/davecgh/go-spew/LICENSE

This file was deleted.

Loading

0 comments on commit b0c0377

Please sign in to comment.