This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
123 lines (104 loc) · 3.42 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Nuts consent bridge api
* Copyright (C) 2019. Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package api
import (
"context"
"encoding/json"
"fmt"
"github.com/nuts-foundation/consent-bridge-go-client/pkg"
core "github.com/nuts-foundation/nuts-go-core"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"time"
)
// HttpClient holds the server address and other basic settings for the http client
type HttpClient struct {
ServerAddress string
Timeout time.Duration
Logger *logrus.Entry
customClient *http.Client
}
func (hb HttpClient) GetConsentRequestStateById(ctx context.Context, uuid string) (*FullConsentRequestState, error) {
resp, err := hb.handleError("GetConsentRequestStateById", func() (*http.Response, error) {
return hb.client().GetConsentRequestStateById(ctx, uuid)
})
if err == nil {
defer resp.Body.Close()
}
// response is of type ConsentRequestState
respObj := FullConsentRequestState{}
err = json.NewDecoder(resp.Body).Decode(&respObj)
if err != nil {
err := fmt.Errorf("error in decoding ConsentRequestState: %w", err)
hb.Logger.Error(err)
return nil, err
}
return &respObj, nil
}
func (hb HttpClient) GetAttachmentBySecureHash(ctx context.Context, hash string) ([]byte, error) {
resp, err := hb.handleError("GetAttachmentBySecureHash", func() (*http.Response, error) {
return hb.client().GetAttachmentBySecureHash(ctx, hash)
})
if err == nil {
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
return nil, err
}
func (hb HttpClient) handleError(name string, f func() (*http.Response, error) ) (*http.Response, error) {
resp, err := f()
if err != nil {
err := fmt.Errorf("error in %s: %w", name, core.Wrap(err))
hb.Logger.Error(err)
return nil, err
}
if resp.StatusCode != 200 {
defer resp.Body.Close()
errorBody, _ := ioutil.ReadAll(resp.Body)
err := fmt.Errorf("error in %s, status: %d, reason: %v", name, resp.StatusCode, string(errorBody))
hb.Logger.Error(err)
return resp, err
}
return resp, nil
}
type BridgeClient interface {
// GetConsentRequestStateById returns the consent request state metadata based on the uuid
GetConsentRequestStateById(context.Context, string) (*FullConsentRequestState, error)
// GetAttachmentBySecureHash retrieves an attachment by its hash
GetAttachmentBySecureHash(context.Context, string) ([]byte, error)
}
// NewConsentBridgeClient returns a BridgeClient configured according to the current config
func NewConsentBridgeClient() BridgeClient {
return HttpClient{
ServerAddress: pkg.ConfigInstance().Address,
Logger: logrus.WithField("component", "ConsentBridgeClient"),
Timeout: time.Second * 60,
}
}
func (hb HttpClient) client() *Client {
if hb.customClient != nil {
return &Client{
Server: hb.ServerAddress,
Client: hb.customClient,
}
}
return &Client{
Server: hb.ServerAddress,
}
}