Skip to content

Commit

Permalink
Add: Moved to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
sofyan48 committed Aug 22, 2024
1 parent c35ae7d commit 266a748
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main.go
8 changes: 0 additions & 8 deletions event/contract.go

This file was deleted.

19 changes: 0 additions & 19 deletions event/event.go

This file was deleted.

3 changes: 3 additions & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/kiriminaja/kaj-log-activities-sdk

go 1.22.3
4 changes: 2 additions & 2 deletions client.go → sdk/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package kaj_log_activities_sdk

import (
"github.com/kiriminaja/kaj-log-activities-sdk/event"
"github.com/kiriminaja/kaj-log-activities-sdk/requester"
"github.com/kiriminaja/kaj-log-activities-sdk/sdk/event"
"github.com/kiriminaja/kaj-log-activities-sdk/sdk/requester"
)

type Client struct {
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions sdk/event/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package event

type EventContract interface {
Upsert(event string, data map[string]interface{}) (map[string]interface{}, error)
List() (map[string]interface{}, error)
}
17 changes: 17 additions & 0 deletions sdk/event/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package event

import "encoding/json"

type Config struct {
URL string
}

type Response struct {
Name string `json:"name"`
Message string `json:"message"`
Data map[string]interface{}
}

func (r *Response) Cast(response []byte, val interface{}) error {
return json.Unmarshal(response, &r)
}
50 changes: 50 additions & 0 deletions sdk/event/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package event

import (
"encoding/json"

"github.com/kiriminaja/kaj-log-activities-sdk/sdk/requester"
)

type eventCase struct {
request requester.Contract
endpoint string
}

func NewEvent(cfg Config, reqs requester.Contract) EventContract {
return &eventCase{
request: reqs,
endpoint: cfg.URL + "/ex/v1/event",
}
}

func (e *eventCase) Upsert(event string, data map[string]interface{}) (map[string]interface{}, error) {
payload := map[string]interface{}{
"event": event,
"data": data,
}
body, err := json.Marshal(payload)
if err != nil {
return nil, err
}
var result map[string]interface{}
response, err := e.request.POST(e.endpoint, map[string]string{}, body)
if err != nil {
return result, err
}
err = json.Unmarshal(response, &result)
return result, err
}

func (e *eventCase) List() (map[string]interface{}, error) {
var result Response
data, err := e.request.GET(e.endpoint, nil)
if err != nil {
return nil, err
}
err = result.Cast(data, &result)
if err != nil {
return result.Data, err
}
return result.Data, nil
}
4 changes: 4 additions & 0 deletions sdk/packages/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package packages

type PackageContract interface {
}
5 changes: 5 additions & 0 deletions sdk/packages/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package packages

type Config struct {
URL string
}
15 changes: 15 additions & 0 deletions sdk/packages/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package packages

import "github.com/kiriminaja/kaj-log-activities-sdk/sdk/requester"

type packageCase struct {
request requester.Contract
endpoint string
}

func NewPackage(cfg Config, request requester.Contract) PackageContract {
return &packageCase{
request: request,
endpoint: cfg.URL + "/ex/v1/package/",
}
}
File renamed without changes.
File renamed without changes.

0 comments on commit 266a748

Please sign in to comment.