Debian Codegraph @

transport/https/https.go

raw
1 package transport
2
3 import (
4 "bytes"
5 "encoding/base64"
6 "io"
7 "io/ioutil"
8 "net/http"
9
10 "github.com/mitch000001/go-hbci/transport"
11 )
12
13 // NewBase64 returns a HTTPSBase64Transport. It sets http.DefaultClient as
14 // http.Client to perform requests to the HBCI server.
15 //
16 // Each request will be encoded and each response will be decoded with Base64
17 // encoding.
18 func NewBase64() transport.Transport {
19 return &HTTPSBase64Transport{
20 httpClient: http.DefaultClient,
21 }
22 }
23
24 // A HTTPSBase64Transport implements transport.Transport and performs request over
25 // HTTPS with also encoding requests and responses with Base64 encoding.
26 type HTTPSBase64Transport struct {
27 httpClient *http.Client
28 }
29
30 // Do performs the request to the HBCI server. If successful, it returns a
31 // populated transport.Response with the HTTP Response Body as Body and the
32 // request as Request.
33 //
34 // Before sending the request it will be encoded with Base64 encoding.
35 // When receiving the response with a status code 200 it will decode the response
36 // with Base64 encoding. A non 200 status code will be returned as is, without
37 // decoding it from Base64.
38 func (h *HTTPSBase64Transport) Do(request *transport.Request) (*transport.Response, error) {
39 var buf bytes.Buffer
40 encodingWriter := base64.NewEncoder(base64.StdEncoding, &buf)
41 _, err := io.Copy(encodingWriter, request.Body)
42 if err != nil {
43 return nil, err
44 }
45 err = encodingWriter.Close()
46 if err != nil {
47 return nil, err
48 }
49 httpResponse, err := h.httpClient.Post(request.URL, "application/vnd.hbci", &buf)
50 if err != nil {
51 return nil, err
52 }
53 var reader io.Reader
54 if httpResponse.StatusCode == http.StatusOK {
55 reader = base64.NewDecoder(base64.StdEncoding, httpResponse.Body)
56 } else {
57 reader = httpResponse.Body
58 }
59 return &transport.Response{Body: ioutil.NopCloser(reader), Request: request}, nil
60 }
61
62 // New returns a HTTPSTransport. It sets http.DefaultClient as http.Client to
63 // perform requests to the HBCI server
64 func New() *HTTPSTransport {
65 return &HTTPSTransport{
66 HTTPClient: http.DefaultClient,
67 }
68 }
69
70 // NewNonDefault returns a HTTPSTransport which uses the given http.Client to
71 // perform requests to the HBCO server
72 func NewNonDefault(h *http.Client) transport.Transport {
73 return &HTTPSTransport{
74 HTTPClient: h,
75 }
76 }
77
78 // A HTTPSTransport implements transport.Transport and performs request over HTTPS
79 type HTTPSTransport struct {
80 HTTPClient *http.Client
81 }
82
83 // Do performs the request to the HBCI server. If successful, it returns a
84 // populated transport.Response with the HTTP Response Body as Body and the
85 // request as Request
86 func (h *HTTPSTransport) Do(request *transport.Request) (*transport.Response, error) {
87 httpResponse, err := h.HTTPClient.Post(request.URL, "application/vnd.hbci", request.Body)
88 if err != nil {
89 return nil, err
90 }
91 return &transport.Response{Body: httpResponse.Body, Request: request}, nil
92 }