Skip to content

Commit 9c8836c

Browse files
committed
API 요청중 토큰 요청 3회 실패 시 응답 개선 (#3)
현재 requestToken() 에러 발생시 사용자에게 에러 정보를 반환하고 있다. 서버에서 정제된 에러 응답 데이터(CF-00401)로 반환되어야 하기 때문에 토큰 요청 에러가 발생해도 API 요청까지 도달하도록 개선. 추가로 불필요한 sleep 로직 제거.
1 parent 16a77ce commit 9c8836c

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

connector.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io/ioutil"
1010
"net/http"
1111
"net/url"
12-
"time"
1312
)
1413

1514
// CODEF API 요청 실행
@@ -19,10 +18,7 @@ func execute(
1918
accessToken *string,
2019
reqInfo *requestInfo,
2120
) (*Response, error) {
22-
err := setToken(reqInfo.clientID, reqInfo.clientSecret, accessToken)
23-
if err != nil {
24-
return nil, err
25-
}
21+
setToken(reqInfo.clientID, reqInfo.clientSecret, accessToken)
2622

2723
b, err := json.Marshal(body)
2824
if err != nil {
@@ -39,14 +35,15 @@ func execute(
3935
}
4036

4137
// 액세스 토큰 셋팅
42-
func setToken(clientID, clientSecret string, accessToken *string) error {
38+
func setToken(clientID, clientSecret string, accessToken *string) {
4339
repeatCount := 3
4440
i := 0
4541
if *accessToken == "" {
4642
for i < repeatCount {
4743
tokenMap, err := requestToken(clientID, clientSecret)
4844
if err != nil {
49-
return err
45+
i++
46+
continue
5047
}
5148
if token, ok := tokenMap["access_token"]; ok {
5249
*accessToken = token.(string)
@@ -55,13 +52,9 @@ func setToken(clientID, clientSecret string, accessToken *string) error {
5552
if *accessToken != "" {
5653
break
5754
}
58-
59-
time.Sleep(time.Millisecond * 20)
6055
i++
6156
}
6257
}
63-
64-
return nil
6558
}
6659

6760
// 액세스 토큰 요청

connector_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package easycodefgo
22

33
import (
44
"encoding/json"
5+
"log"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -25,8 +26,7 @@ func TestSetToken(t *testing.T) {
2526
ast := assert.New(t)
2627
codef := &Codef{}
2728

28-
err := setToken(SandboxClientID, SandboxClientSecret, codef.getAccessToken(TypeSandbox))
29-
ast.NoError(err)
29+
setToken(SandboxClientID, SandboxClientSecret, codef.getAccessToken(TypeSandbox))
3030
ast.NotEmpty(codef.accessToken)
3131
}
3232

@@ -48,8 +48,7 @@ func TestRequestProduct(t *testing.T) {
4848
ast.Empty(res.GetData())
4949

5050
// Connected ID 정상 발급 테스트
51-
err = setToken(SandboxClientID, SandboxClientSecret, &accessToken)
52-
ast.NoError(err)
51+
setToken(SandboxClientID, SandboxClientSecret, &accessToken)
5352

5453
res, err = requestProduct(SandboxDomain+PathCreateAccount, accessToken, string(data))
5554
testExistConnectedID(ast, res)
@@ -80,6 +79,31 @@ func testExistConnectedID(ast *assert.Assertions, res *Response) {
8079
ast.True(ok)
8180
}
8281

82+
// SetToken에서 3회 요청 실패 시 빈 액세스 토큰으로 요청하게됨
83+
// 이때 응답결과 CF-00401이 발생해야함
84+
func TestFailSetTokenResult(t *testing.T) {
85+
ast := assert.New(t)
86+
codef := &Codef{PublicKey: "public_key"}
87+
88+
codef.SetClientInfo("id", "pri")
89+
90+
parameter := map[string]interface{}{
91+
"connectedId": "8PQI4dQ......hKLhTnZ",
92+
"organization": "0004",
93+
"identity": "1130000627",
94+
}
95+
productURL := "/v1/kr/card/b/account/card-list" // 법인 보유카드 조회 URL
96+
result, err := codef.RequestProduct(productURL, TypeProduct, parameter)
97+
if err != nil {
98+
log.Fatalln(err)
99+
}
100+
101+
jsonMap := make(map[string]interface{})
102+
json.Unmarshal([]byte(result), &jsonMap)
103+
resultData := jsonMap["result"].(map[string]interface{})
104+
ast.Equal(resultData["code"], "CF-00401")
105+
}
106+
83107
// ConnectedID 발급을 위한 Body 테스트 데이터 생성
84108
func createParamForCreateConnectedID() (map[string]interface{}, error) {
85109
publicKey := "MIIBIjANBgkqhkiG9w0BAQ" +

0 commit comments

Comments
 (0)