Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup: Initial files #1

Merged
merged 52 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
aa6870a
wip: add initial go-sdk files
LucJosin Jul 3, 2024
d8fff00
refactor: update structure
LucJosin Jul 3, 2024
72f8c92
chore: update idea config
LucJosin Jul 6, 2024
36c218c
feat: add new request methods to service
LucJosin Jul 6, 2024
a57b3b2
feat: add response models
LucJosin Jul 6, 2024
deb64be
feat: update actor and add create and delete methods
LucJosin Jul 6, 2024
3ecb0c3
refactor: update basic example
LucJosin Jul 6, 2024
b1b6db0
feat: add page and filters options and patch request
LucJosin Jul 7, 2024
5bcdd7f
feat: verify out param before doRequest
LucJosin Jul 9, 2024
fa82eef
feat: add default pageable
LucJosin Jul 9, 2024
34f493e
feat: add overview request
LucJosin Jul 9, 2024
4599fc5
feat: add info request
LucJosin Jul 9, 2024
bcc5d12
refactor: change newListOptions to return a value instead of a pointer
LucJosin Jul 9, 2024
3858855
feat: add NewListOptions method
LucJosin Jul 9, 2024
922a17f
docs: update README
LucJosin Jul 9, 2024
e0e8efc
docs: update README example code
LucJosin Jul 9, 2024
f7d2d44
feat: add ErrorResponse
LucJosin Jul 9, 2024
a8e2a47
docs: update README
LucJosin Jul 9, 2024
a3a0647
docs: update README
LucJosin Jul 9, 2024
1e924c1
refactor: replace time.Time with string in CreatedAt and UpdatedAt
LucJosin Jul 9, 2024
cbdae34
refactor: update character methods
LucJosin Jul 9, 2024
55361d9
refactor: update CreateCharacter struct
LucJosin Jul 9, 2024
8e61f68
feat: add episode methods
LucJosin Jul 9, 2024
744edee
feat: add game methods
LucJosin Jul 9, 2024
79b881d
feat: add location methods
LucJosin Jul 9, 2024
2b321aa
feat: add season methods
LucJosin Jul 9, 2024
618e5b9
fix: out must be a pointer
LucJosin Jul 9, 2024
a6f1fa7
feat: add soundtrack methods
LucJosin Jul 9, 2024
6845f4d
refactor: rename ListOptions to QueryOptions
LucJosin Jul 9, 2024
a2a30cf
fix: add default query options to requests that uses translation
LucJosin Jul 9, 2024
a6d820a
refactor: newQueryOptions now only creates a default queryOptions
LucJosin Jul 10, 2024
0b0eb0e
refactor: update options handling
LucJosin Jul 12, 2024
e8b98bb
revert: example file (0b0eb0e)
LucJosin Jul 12, 2024
847acb2
test: add tests for buildUrl
LucJosin Jul 12, 2024
645b852
test: add tests for extractHeaders and doRequest
LucJosin Jul 12, 2024
85ead52
test: add tests for handlePagination
LucJosin Jul 13, 2024
8752847
feat: add in memory cache
LucJosin Jul 18, 2024
dd0d4a5
fix: wrong options validation in WithOpts
LucJosin Jul 18, 2024
dc437c0
refactor: update NewClient and NewClientWithOpts
LucJosin Jul 18, 2024
f49be2f
fix: field `Status` in wrong struct
LucJosin Jul 18, 2024
375e93d
fix: missing `Status` field in BaseResponse
LucJosin Jul 18, 2024
27b8d94
test: add tests for doGetRequest
LucJosin Jul 18, 2024
a437e13
feat: default and custom logger
LucJosin Jul 18, 2024
d5faac0
feat: add more logging
LucJosin Jul 18, 2024
bc932a4
fix: wrong LogLevel validation
LucJosin Jul 18, 2024
798ce95
feat: warn the change of UseInMemoryCache value when using WithOpts o…
LucJosin Jul 18, 2024
bb197fa
refactor: change logging from info to debug
LucJosin Jul 18, 2024
bca8940
refactor update basic example
LucJosin Jul 18, 2024
964c88e
ci: add coverage and release workflows
LucJosin Jul 19, 2024
dd4442f
refactor: rename basic.go into list.go
LucJosin Jul 19, 2024
730bfb4
feat: add create.go example
LucJosin Jul 19, 2024
5c97adc
docs: update README
LucJosin Jul 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: "Go Coverage"

on:
workflow_dispatch:
push:
branches:
- main
pull_request:

jobs:
coverage:
# Ignore drafts
if: github.event.pull_request.draft == false
name: Go test with coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 10

- name: Set up Golang
uses: actions/setup-go@v5
with:
go-version: '1.22'

- name: Install dependencies
run: go mod tidy

- uses: gwatts/go-coverage-action@v2
id: coverage
with:
coverage-threshold: 60
cover-pkg: ./...
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Create Release

on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Golang
uses: actions/setup-go@v5
with:
go-version: '1.22'

- name: Install dependencies
run: go mod tidy

- name: Testing
run: go test -v ./...

- name: Publish to pkg.go.dev
run: |
echo "VERSION=${{ github.ref_name }}"
GOPROXY=proxy.golang.org go list -m github.com/HawAPI/go-sdk@${{ github.ref_name }}
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/go-sdk.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/markdown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 116 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,117 @@
# go-sdk
# HawAPI - go-sdk

HawAPI SDK for Golang

- [API Docs](https://hawapi.theproject.id/docs/)
- [SDK Docs](https://pkg.go.dev/github.com/HawAPI/go-sdk)

## Topics

- [Installation](#installation)
- [Usage](#usage)
- [Init client](#init-client)
- [Fetch information](#fetch-information)
- [Error handling](#error-handling)

## Installation

```
go get github.com/HawAPI/go-sdk@latest
```

## Usage

- [See examples](./examples)

### Init client

```go
package main

import (
"fmt"

"github.com/HawAPI/go-sdk"
)

func main() {
// Create a new client with default options
client := hawapi.NewClient()

// Create client with custom options
client = hawapi.NewClientWithOpts(hawapi.Options{
Endpoint: "http://localhost:8080/api",
// When using 'WithOpts' or 'NewClientWithOpts' the value of
// 'UseInMemoryCache' will be set to false
UseInMemoryCache: true,
// Version
// Language
// Token
// ...
})

// You can also change the options later
client.WithOpts(hawapi.Options{
Language: "pt-BR",
// When using 'WithOpts' or 'NewClientWithOpts' the value of
// 'UseInMemoryCache' will be set to false
UseInMemoryCache: true,
})
}
```

### Fetch information

```go
package main

import (
"fmt"

"github.com/HawAPI/go-sdk"
)

func main() {
client := hawapi.NewClient()

res, err := client.ListActors()
if err != nil {
panic(err)
}

fmt.Println(res)
}
```

### Error handling

- Check out the [hawapi.ErrorResponse](./pkg/hawapi/error.go)

```go
package main

import (
"fmt"

"github.com/HawAPI/go-sdk"
"github.com/google/uuid"
)

func main() {
client := hawapi.NewClient()

id, _ := uuid.Parse("<unknown uuid>")
res, err := client.FindActor(id)
if err != nil {
// If the error is coming from the API request,
// it'll be of type hawapi.ErrorResponse.
if resErr, ok := err.(hawapi.ErrorResponse); ok {
fmt.Printf("API error %d Message: %s\n", resErr.Code, resErr.Message)
} else {
fmt.Println("SDK error:", err)
}
}

fmt.Println(res)
}
```
25 changes: 25 additions & 0 deletions examples/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"

"github.com/HawAPI/go-sdk/pkg/hawapi"
)

func main() {
client := hawapi.NewClient()
client.WithOpts(hawapi.Options{
// JWT auth is required when performing any POST, PATCH and DELETE requests
Token: "<JWT>",
})

actor := hawapi.CreateActor{
// ...
}
res, err := client.CreateActor(actor)
if err != nil {
panic(err)
}

fmt.Println(res.FirstName)
}
28 changes: 28 additions & 0 deletions examples/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"

"github.com/HawAPI/go-sdk/pkg/hawapi"
)

func main() {
// Create a new client with default options
client := hawapi.NewClient()

// Override options
client.WithOpts(hawapi.Options{
Endpoint: "http://localhost:8080/api",
// When using 'WithOpts' or 'NewClientWithOpts' the value of
// 'UseInMemoryCache' will be set to false
UseInMemoryCache: true,
})

res, err := client.ListActors()
if err != nil {
panic(err)
}

fmt.Println(res)
fmt.Println(len(res.Data))
}
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/HawAPI/go-sdk

go 1.22.0

require (
github.com/fatih/color v1.17.0
github.com/google/uuid v1.6.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.18.0 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
49 changes: 49 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cache

// Cache is a simple key / value cache
type Cache interface {
Get(key string) (any, bool)
Set(key string, value any)
Del(key string)
Size() int
Clear() int
}

type memoryCache struct {
cache map[string]any
}

// NewMemoryCache creates a new Cache
func NewMemoryCache() Cache {
return &memoryCache{
cache: make(map[string]any),
}
}

// Get will try to get associated with a key from the cache, if present
func (c *memoryCache) Get(key string) (any, bool) {
v, ok := c.cache[key]
return v, ok
}

// Set will store a key-value pair in the cache
func (c *memoryCache) Set(key string, value any) {
c.cache[key] = value
}

// Del will remove a key and its associated value from the cache.
func (c *memoryCache) Del(key string) {
delete(c.cache, key)
}

// Size will return the current number of entries in the cache.
func (c *memoryCache) Size() int {
return len(c.cache)
}

// Clear will empty the cache, removing all stored key-value pairs.
func (c *memoryCache) Clear() int {
count := len(c.cache)
c.cache = make(map[string]any)
return count
}
Loading
Loading