@@ -2,6 +2,7 @@ package nordigen
22
33import (
44 "bytes"
5+ "context"
56 "encoding/json"
67 "io"
78 "net/http"
@@ -16,87 +17,90 @@ type Token struct {
1617 RefreshExpires int `json:"refresh_expires"`
1718}
1819
20+ type TokenRefresh struct {
21+ Refresh string `json:"refresh"`
22+ }
23+
1924type Secret struct {
2025 SecretId string `json:"secret_id"`
2126 AccessId string `json:"secret_key"`
2227}
2328
2429const tokenPath = "token"
2530const tokenNewPath = "new/"
26- const tokenRefreshPath = "refresh"
27-
28- func (c Client ) newToken () (* Token , error ) {
29- req := http.Request {
30- Method : http .MethodPost ,
31- URL : & url.URL {
32- Path : strings .Join ([]string {tokenPath , tokenNewPath }, "/" ),
33- },
34- }
31+ const tokenRefreshPath = "refresh/"
3532
33+ func (c Client ) newToken (ctx context.Context ) (* Token , error ) {
3634 data , err := json .Marshal (Secret {
3735 SecretId : c .secretId ,
3836 AccessId : c .secretKey ,
3937 })
4038 if err != nil {
4139 return nil , err
4240 }
43- req .Body = io .NopCloser (bytes .NewBuffer (data ))
44- resp , err := c .c .Do (& req )
4541
46- if err != nil {
47- return nil , err
42+ req := & http.Request {
43+ Method : http .MethodPost ,
44+ Body : io .NopCloser (bytes .NewBuffer (data )),
45+ URL : & url.URL {
46+ Path : strings .Join ([]string {tokenPath , tokenNewPath }, "/" ),
47+ },
4848 }
49- body , err := io . ReadAll ( resp . Body )
49+ req = req . WithContext ( ctx )
5050
51+ resp , err := c .c .Do (req )
5152 if err != nil {
5253 return nil , err
5354 }
55+ defer resp .Body .Close ()
56+
57+ body , readErr := io .ReadAll (resp .Body )
58+ if readErr != nil {
59+ return nil , readErr
60+ }
5461 if resp .StatusCode != http .StatusOK {
55- return nil , & APIError {resp .StatusCode , string (body ), err }
62+ return nil , & APIError {StatusCode : resp .StatusCode , Body : string (body )}
5663 }
57- t := & Token {}
58- err = json .Unmarshal (body , & t )
5964
60- if err != nil {
65+ t := & Token {}
66+ if err := json .Unmarshal (body , t ); err != nil {
6167 return nil , err
6268 }
63-
6469 return t , nil
6570}
6671
67- func (c Client ) refreshToken (refresh string ) (* Token , error ) {
68- req := http.Request {
72+ func (c Client ) refreshToken (ctx context.Context , refresh string ) (* Token , error ) {
73+ data , err := json .Marshal (TokenRefresh {Refresh : refresh })
74+ if err != nil {
75+ return nil , err
76+ }
77+
78+ req := & http.Request {
6979 Method : http .MethodPost ,
80+ Body : io .NopCloser (bytes .NewBuffer (data )),
7081 URL : & url.URL {
7182 Path : strings .Join ([]string {tokenPath , tokenRefreshPath }, "/" ),
7283 },
7384 }
74- data , err := json .Marshal (refresh )
75-
76- if err != nil {
77- return & Token {}, err
78- }
79- req .Body = io .NopCloser (bytes .NewBuffer (data ))
80-
81- resp , err := c .c .Do (& req )
85+ req = req .WithContext (ctx )
8286
87+ resp , err := c .c .Do (req )
8388 if err != nil {
8489 return nil , err
8590 }
86- body , err := io . ReadAll ( resp .Body )
91+ defer resp .Body . Close ( )
8792
88- if err != nil {
89- return nil , err
93+ body , readErr := io .ReadAll (resp .Body )
94+ if readErr != nil {
95+ return nil , readErr
9096 }
9197 if resp .StatusCode != http .StatusOK {
92- return nil , & APIError {resp .StatusCode , string (body ), err }
98+ return nil , & APIError {StatusCode : resp .StatusCode , Body : string (body )}
9399 }
94- t := & Token {}
95- err = json .Unmarshal (body , & t )
96100
97- if err != nil {
101+ t := & Token {}
102+ if err := json .Unmarshal (body , t ); err != nil {
98103 return nil , err
99104 }
100-
101105 return t , nil
102106}
0 commit comments