| 1 | package transport |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "io" |
| 7 | "io/ioutil" |
| 8 | |
| 9 | "github.com/mitch000001/go-hbci/message" |
| 10 | "github.com/mitch000001/go-hbci/segment" |
| 11 | ) |
| 12 | |
| 13 | // Transport defines an interface over the various ways data got exchanged with |
| 14 | // HBCI servers. It is used by higher level implementations to perform requests |
| 15 | // against the bank servers. |
| 16 | type Transport interface { |
| 17 | Do(*Request) (*Response, error) |
| 18 | } |
| 19 | |
| 20 | // The Func type is an adapter to allow the use of ordinary functions |
| 21 | // as Transport handlers. If f is a function with the appropriate signature, |
| 22 | // Func(f) is a Transport that calls f. |
| 23 | type Func func(*Request) (*Response, error) |
| 24 | |
| 25 | // Do calls fn(req). |
| 26 | func (fn Func) Do(req *Request) (*Response, error) { |
| 27 | return fn(req) |
| 28 | } |
| 29 | |
| 30 | // Middleware defines the interface for writing middleware for transports |
| 31 | type Middleware func(Transport) Transport |
| 32 | |
| 33 | // A Request represents a client request to a HBCI server |
| 34 | type Request struct { |
| 35 | // URL specifies the URI being requested |
| 36 | // |
| 37 | // For the sake of simplicity this is a string instead a *url.URL right now |
| 38 | URL string |
| 39 | // Body is the request's body. |
| 40 | // |
| 41 | // Body has always to be non-nil |
| 42 | Body io.ReadCloser |
| 43 | } |
| 44 | |
| 45 | // ReadResponse reads and returns a Response from r. It populates the embedded |
| 46 | // SegmentExtractor to have it ready to use. |
| 47 | func ReadResponse(r *bufio.Reader, req *Request) (*Response, error) { |
| 48 | var buf bytes.Buffer |
| 49 | marshaledMessage, err := ioutil.ReadAll(io.TeeReader(r, &buf)) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | extractor := message.NewSegmentExtractor(marshaledMessage) |
| 54 | _, err = extractor.Extract() |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | response := &Response{ |
| 59 | Request: req, |
| 60 | MarshaledResponse: marshaledMessage, |
| 61 | SegmentExtractor: extractor, |
| 62 | Body: ioutil.NopCloser(&buf), |
| 63 | } |
| 64 | return response, nil |
| 65 | } |
| 66 | |
| 67 | // A Response represents a server response from a HBCI server |
| 68 | type Response struct { |
| 69 | // The SegmentExtractor can be used to conveniently query segments from the |
| 70 | // Response. |
| 71 | // |
| 72 | // Right now the Response embeds the SegmentExtractor, which violates the SRP. |
| 73 | // Future Response types may get rid of it. |
| 74 | *message.SegmentExtractor |
| 75 | // Request is the request that was sent to obtain this Response. |
| 76 | // Request's Body is nil (having already been consumed). |
| 77 | Request *Request |
| 78 | // MarshaledResponse represents the body payload as byte slice. It's strongly |
| 79 | // discouraged to use that as it may or may not be populated by the Transport. |
| 80 | // |
| 81 | // It's only there for legacy reasons. |
| 82 | MarshaledResponse []byte |
| 83 | // Body represents the response body. |
| 84 | Body io.ReadCloser |
| 85 | } |
| 86 | |
| 87 | // IsEncrypted returns whether the response contains an encrypted message. |
| 88 | // This method will panic when the SegmentExtractor is not populated. |
| 89 | func (h *Response) IsEncrypted() bool { |
| 90 | return h.SegmentExtractor.FindSegment(segment.EncryptionHeaderSegmentID) != nil |
| 91 | } |