Skip to content

Commit bdce10f

Browse files
authored
Setup: Initial files (#1)
1 parent 2494ea1 commit bdce10f

32 files changed

+2549
-1
lines changed

.github/workflows/coverage.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: "Go Coverage"
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
pull_request:
9+
10+
jobs:
11+
coverage:
12+
# Ignore drafts
13+
if: github.event.pull_request.draft == false
14+
name: Go test with coverage
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 10
20+
21+
- name: Set up Golang
22+
uses: actions/setup-go@v5
23+
with:
24+
go-version: '1.22'
25+
26+
- name: Install dependencies
27+
run: go mod tidy
28+
29+
- uses: gwatts/go-coverage-action@v2
30+
id: coverage
31+
with:
32+
coverage-threshold: 60
33+
cover-pkg: ./...

.github/workflows/release.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Create Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v[0-9]+.[0-9]+.[0-9]+'
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
15+
- name: Set up Golang
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version: '1.22'
19+
20+
- name: Install dependencies
21+
run: go mod tidy
22+
23+
- name: Testing
24+
run: go test -v ./...
25+
26+
- name: Publish to pkg.go.dev
27+
run: |
28+
echo "VERSION=${{ github.ref_name }}"
29+
GOPROXY=proxy.golang.org go list -m github.com/HawAPI/go-sdk@${{ github.ref_name }}

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dataSources.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/go-sdk.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/markdown.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+116-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,117 @@
1-
# go-sdk
1+
# HawAPI - go-sdk
2+
23
HawAPI SDK for Golang
4+
5+
- [API Docs](https://hawapi.theproject.id/docs/)
6+
- [SDK Docs](https://pkg.go.dev/github.com/HawAPI/go-sdk)
7+
8+
## Topics
9+
10+
- [Installation](#installation)
11+
- [Usage](#usage)
12+
- [Init client](#init-client)
13+
- [Fetch information](#fetch-information)
14+
- [Error handling](#error-handling)
15+
16+
## Installation
17+
18+
```
19+
go get github.com/HawAPI/go-sdk@latest
20+
```
21+
22+
## Usage
23+
24+
- [See examples](./examples)
25+
26+
### Init client
27+
28+
```go
29+
package main
30+
31+
import (
32+
"fmt"
33+
34+
"github.com/HawAPI/go-sdk"
35+
)
36+
37+
func main() {
38+
// Create a new client with default options
39+
client := hawapi.NewClient()
40+
41+
// Create client with custom options
42+
client = hawapi.NewClientWithOpts(hawapi.Options{
43+
Endpoint: "http://localhost:8080/api",
44+
// When using 'WithOpts' or 'NewClientWithOpts' the value of
45+
// 'UseInMemoryCache' will be set to false
46+
UseInMemoryCache: true,
47+
// Version
48+
// Language
49+
// Token
50+
// ...
51+
})
52+
53+
// You can also change the options later
54+
client.WithOpts(hawapi.Options{
55+
Language: "pt-BR",
56+
// When using 'WithOpts' or 'NewClientWithOpts' the value of
57+
// 'UseInMemoryCache' will be set to false
58+
UseInMemoryCache: true,
59+
})
60+
}
61+
```
62+
63+
### Fetch information
64+
65+
```go
66+
package main
67+
68+
import (
69+
"fmt"
70+
71+
"github.com/HawAPI/go-sdk"
72+
)
73+
74+
func main() {
75+
client := hawapi.NewClient()
76+
77+
res, err := client.ListActors()
78+
if err != nil {
79+
panic(err)
80+
}
81+
82+
fmt.Println(res)
83+
}
84+
```
85+
86+
### Error handling
87+
88+
- Check out the [hawapi.ErrorResponse](./pkg/hawapi/error.go)
89+
90+
```go
91+
package main
92+
93+
import (
94+
"fmt"
95+
96+
"github.com/HawAPI/go-sdk"
97+
"github.com/google/uuid"
98+
)
99+
100+
func main() {
101+
client := hawapi.NewClient()
102+
103+
id, _ := uuid.Parse("<unknown uuid>")
104+
res, err := client.FindActor(id)
105+
if err != nil {
106+
// If the error is coming from the API request,
107+
// it'll be of type hawapi.ErrorResponse.
108+
if resErr, ok := err.(hawapi.ErrorResponse); ok {
109+
fmt.Printf("API error %d Message: %s\n", resErr.Code, resErr.Message)
110+
} else {
111+
fmt.Println("SDK error:", err)
112+
}
113+
}
114+
115+
fmt.Println(res)
116+
}
117+
```

examples/create.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/HawAPI/go-sdk/pkg/hawapi"
7+
)
8+
9+
func main() {
10+
client := hawapi.NewClient()
11+
client.WithOpts(hawapi.Options{
12+
// JWT auth is required when performing any POST, PATCH and DELETE requests
13+
Token: "<JWT>",
14+
})
15+
16+
actor := hawapi.CreateActor{
17+
// ...
18+
}
19+
res, err := client.CreateActor(actor)
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
fmt.Println(res.FirstName)
25+
}

examples/list.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/HawAPI/go-sdk/pkg/hawapi"
7+
)
8+
9+
func main() {
10+
// Create a new client with default options
11+
client := hawapi.NewClient()
12+
13+
// Override options
14+
client.WithOpts(hawapi.Options{
15+
Endpoint: "http://localhost:8080/api",
16+
// When using 'WithOpts' or 'NewClientWithOpts' the value of
17+
// 'UseInMemoryCache' will be set to false
18+
UseInMemoryCache: true,
19+
})
20+
21+
res, err := client.ListActors()
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
fmt.Println(res)
27+
fmt.Println(len(res.Data))
28+
}

go.mod

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/HawAPI/go-sdk
2+
3+
go 1.22.0
4+
5+
require (
6+
github.com/fatih/color v1.17.0
7+
github.com/google/uuid v1.6.0
8+
)
9+
10+
require (
11+
github.com/mattn/go-colorable v0.1.13 // indirect
12+
github.com/mattn/go-isatty v0.0.20 // indirect
13+
golang.org/x/sys v0.18.0 // indirect
14+
)

go.sum

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
2+
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
3+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
6+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
7+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
9+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
10+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
13+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

pkg/cache/cache.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cache
2+
3+
// Cache is a simple key / value cache
4+
type Cache interface {
5+
Get(key string) (any, bool)
6+
Set(key string, value any)
7+
Del(key string)
8+
Size() int
9+
Clear() int
10+
}
11+
12+
type memoryCache struct {
13+
cache map[string]any
14+
}
15+
16+
// NewMemoryCache creates a new Cache
17+
func NewMemoryCache() Cache {
18+
return &memoryCache{
19+
cache: make(map[string]any),
20+
}
21+
}
22+
23+
// Get will try to get associated with a key from the cache, if present
24+
func (c *memoryCache) Get(key string) (any, bool) {
25+
v, ok := c.cache[key]
26+
return v, ok
27+
}
28+
29+
// Set will store a key-value pair in the cache
30+
func (c *memoryCache) Set(key string, value any) {
31+
c.cache[key] = value
32+
}
33+
34+
// Del will remove a key and its associated value from the cache.
35+
func (c *memoryCache) Del(key string) {
36+
delete(c.cache, key)
37+
}
38+
39+
// Size will return the current number of entries in the cache.
40+
func (c *memoryCache) Size() int {
41+
return len(c.cache)
42+
}
43+
44+
// Clear will empty the cache, removing all stored key-value pairs.
45+
func (c *memoryCache) Clear() int {
46+
count := len(c.cache)
47+
c.cache = make(map[string]any)
48+
return count
49+
}

0 commit comments

Comments
 (0)