Compare commits
2 Commits
master
...
improve-redis
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6dfc474924 | ||
| 1215790919 |
@@ -10,6 +10,7 @@ import (
|
|||||||
"satusehat-rssa/internal/integration"
|
"satusehat-rssa/internal/integration"
|
||||||
"satusehat-rssa/internal/midleware"
|
"satusehat-rssa/internal/midleware"
|
||||||
"satusehat-rssa/internal/routes"
|
"satusehat-rssa/internal/routes"
|
||||||
|
"satusehat-rssa/pkg/redis"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
@@ -23,6 +24,12 @@ func main() {
|
|||||||
log.Println("No .env file found or error loading it")
|
log.Println("No .env file found or error loading it")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//redis
|
||||||
|
rdb, err := redis.NewRedis()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("❌ Redis connection failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// init gin engine
|
// init gin engine
|
||||||
e := gin.Default()
|
e := gin.Default()
|
||||||
|
|
||||||
@@ -63,6 +70,8 @@ func main() {
|
|||||||
//Load Akses
|
//Load Akses
|
||||||
akses := config.LoadConfig()
|
akses := config.LoadConfig()
|
||||||
authMiddleware := midleware.AuthMiddleware{}
|
authMiddleware := midleware.AuthMiddleware{}
|
||||||
|
akses.Redis = rdb
|
||||||
|
redis.NewRedisAuth(akses)
|
||||||
|
|
||||||
oa := integration.NewOauthRequestRepo(akses)
|
oa := integration.NewOauthRequestRepo(akses)
|
||||||
at := integration.NewAllergancyToleranRepo(akses)
|
at := integration.NewAllergancyToleranRepo(akses)
|
||||||
|
|||||||
@@ -4,19 +4,22 @@ go 1.21.12
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.10.1
|
github.com/gin-gonic/gin v1.10.1
|
||||||
github.com/go-playground/validator/v10 v10.26.0
|
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
|
github.com/redis/go-redis/v9 v9.17.2
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bytedance/sonic v1.11.6 // indirect
|
github.com/bytedance/sonic v1.11.6 // indirect
|
||||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
|
github.com/go-playground/validator/v10 v10.26.0 // indirect
|
||||||
github.com/goccy/go-json v0.10.2 // indirect
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
|
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||||
@@ -9,6 +15,8 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
||||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||||
@@ -23,8 +31,6 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
|
|||||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
github.com/go-playground/validator/v10 v10.26.0 h1:SP05Nqhjcvz81uJaRfEV0YBSSSGMc/iMaVtFbr3Sw2k=
|
github.com/go-playground/validator/v10 v10.26.0 h1:SP05Nqhjcvz81uJaRfEV0YBSSSGMc/iMaVtFbr3Sw2k=
|
||||||
github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
||||||
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
|
|
||||||
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
|
||||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
@@ -51,6 +57,8 @@ github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6
|
|||||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI=
|
||||||
|
github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
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/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package integration
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -9,6 +10,7 @@ import (
|
|||||||
"satusehat-rssa/internal/constant"
|
"satusehat-rssa/internal/constant"
|
||||||
"satusehat-rssa/internal/model"
|
"satusehat-rssa/internal/model"
|
||||||
"satusehat-rssa/pkg/httputil"
|
"satusehat-rssa/pkg/httputil"
|
||||||
|
"satusehat-rssa/pkg/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServiceRequestInterface interface {
|
type ServiceRequestInterface interface {
|
||||||
@@ -133,17 +135,15 @@ func (s *ServiceRequestRepository) CreateServiceRequest(req model.ServiceRequest
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
oauth := model.OauthRequest{
|
// use redis auth helper to get or create token
|
||||||
ClientId: s.akses.ClientId,
|
redisRepo := redis.NewRedisAuth(s.akses)
|
||||||
ClientSecret: s.akses.ClientSecret,
|
token, err := redisRepo.GetOrCreateTokenSS(context.Background(), req.Subject.Reference)
|
||||||
}
|
|
||||||
OauthInterface := NewOauthRequestRepo(s.akses)
|
|
||||||
token, err := OauthInterface.GenerateToken(oauth)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if token != nil {
|
|
||||||
request.Header.Set("Authorization", "Bearer "+token.AccessToken)
|
if token != "" {
|
||||||
|
request.Header.Set("Authorization", "Bearer "+token)
|
||||||
}
|
}
|
||||||
request.Header.Set("Content-Type", constant.ContentTypeFHIRJSON)
|
request.Header.Set("Content-Type", constant.ContentTypeFHIRJSON)
|
||||||
res, err := client.Do(request)
|
res, err := client.Do(request)
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
import "github.com/redis/go-redis/v9"
|
||||||
|
|
||||||
type Akses struct {
|
type Akses struct {
|
||||||
AuthUrl string
|
AuthUrl string
|
||||||
BaseUrl string
|
BaseUrl string
|
||||||
@@ -7,4 +9,5 @@ type Akses struct {
|
|||||||
KfaUrl string
|
KfaUrl string
|
||||||
ClientId string
|
ClientId string
|
||||||
ClientSecret string
|
ClientSecret string
|
||||||
|
Redis *redis.Client
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewRedis() (*redis.Client, error) {
|
||||||
|
addr := os.Getenv("REDIS_ADDR")
|
||||||
|
if addr == "" {
|
||||||
|
addr = "localhost:6379"
|
||||||
|
}
|
||||||
|
|
||||||
|
rdb := redis.NewClient(&redis.Options{
|
||||||
|
Addr: addr,
|
||||||
|
})
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// cek koneksi saat startup
|
||||||
|
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return rdb, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"satusehat-rssa/internal/constant"
|
||||||
|
"satusehat-rssa/internal/model"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RedisAkses struct {
|
||||||
|
akses *model.Akses
|
||||||
|
}
|
||||||
|
|
||||||
|
type RedisInterface interface {
|
||||||
|
GetOrCreateTokenSS(ctx context.Context, nik string) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRedisAuth(akses *model.Akses) RedisInterface {
|
||||||
|
return &RedisAkses{
|
||||||
|
akses: akses,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RedisAkses) GetOrCreateTokenSS(ctx context.Context, nik string) (string, error) {
|
||||||
|
const redisKey = "token-ss"
|
||||||
|
var code string
|
||||||
|
|
||||||
|
// 1️⃣ ambil dari redis
|
||||||
|
token, err := s.akses.Redis.Get(ctx, redisKey).Result()
|
||||||
|
if err == nil && token != "" {
|
||||||
|
code, err = s.GetPatientByNIK(nik, token)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// redis error selain key not found
|
||||||
|
if err != nil && err != redis.Nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if code == "invalid-access-token" || token == "" {
|
||||||
|
// hapus token dari redis
|
||||||
|
if err := s.akses.Redis.Del(ctx, redisKey).Err(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
// 2️⃣ generate token baru
|
||||||
|
oauth := model.OauthRequest{
|
||||||
|
ClientId: s.akses.ClientId,
|
||||||
|
ClientSecret: s.akses.ClientSecret,
|
||||||
|
}
|
||||||
|
|
||||||
|
obj_token, err := s.GenerateToken(oauth)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3️⃣ simpan ke redis (opsional TTL)
|
||||||
|
if err := s.akses.Redis.Set(ctx, redisKey, obj_token.AccessToken, 0).Err(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj_token.AccessToken, nil
|
||||||
|
}else{
|
||||||
|
return token, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *RedisAkses) GenerateToken(req model.OauthRequest) (*model.OauthResponse, error) {
|
||||||
|
var (
|
||||||
|
data *model.OauthResponse
|
||||||
|
)
|
||||||
|
url := o.akses.AuthUrl + "/accesstoken?grant_type=client_credentials"
|
||||||
|
method := "POST"
|
||||||
|
|
||||||
|
req_data := "client_id=" + req.ClientId + "&client_secret=" + req.ClientSecret
|
||||||
|
payload := strings.NewReader(req_data)
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
request, err := http.NewRequest(method, url, payload)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
res, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
err = json.NewDecoder(res.Body).Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *RedisAkses) GetPatientByNIK(nik string, token string) (string, error) {
|
||||||
|
var (
|
||||||
|
data *map[string]interface{}
|
||||||
|
)
|
||||||
|
url := p.akses.BaseUrl + "/Patient?identifier=https://fhir.kemkes.go.id/id/nik|" + nik
|
||||||
|
method := "GET"
|
||||||
|
client := &http.Client{}
|
||||||
|
request, err := http.NewRequest(method, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if token != "" {
|
||||||
|
request.Header.Add("Authorization", "Bearer "+token)
|
||||||
|
} else {
|
||||||
|
return "", errors.New(constant.ErrGenerateToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
request.Header.Set("Content-Type", constant.ContentTypeFHIRJSON)
|
||||||
|
request.Header.Set("Accept", constant.ContentTypeFHIRJSON)
|
||||||
|
|
||||||
|
res, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
err = json.NewDecoder(res.Body).Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cek jika response adalah OperationOutcome dan ambil code
|
||||||
|
if data != nil {
|
||||||
|
if rt, ok := (*data)["resourceType"].(string); ok && rt == "OperationOutcome" {
|
||||||
|
if issues, ok := (*data)["issue"].([]interface{}); ok && len(issues) > 0 {
|
||||||
|
if issueMap, ok := issues[0].(map[string]interface{}); ok {
|
||||||
|
if code, ok := issueMap["code"].(string); ok {
|
||||||
|
return code, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dataJSON, _ := json.Marshal(data)
|
||||||
|
return string(dataJSON), nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user