-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts_mortgage.go
173 lines (145 loc) · 6.33 KB
/
products_mortgage.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package capis
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"time"
querystring "github.com/google/go-querystring/query"
"go.opencensus.io/trace"
)
type (
NewMortgageRequest struct {
ID string `json:"id"`
Issuer string `json:"issuer"`
Name string `json:"name"`
Description string `json:"description"`
URLApply string `json:"url_apply"`
URLLogo string `json:"url_logo"`
HighlightedPoints []string `json:"highlighted_points"`
TechnicalPoints []string `json:"technical_points"`
Labels []string `json:"labels"`
Type string `json:"type"`
OfferInterestRate RatePeriod `json:"offer_interest_rate"`
OfferInterestRateType string `json:"offer_interest_rate_type"`
StandardInterestRate Rate `json:"standard_interest_rate"`
StandardInterestRateType string `json:"standard_interest_rate_type"`
LoanToValue Rate `json:"loan_to_value"`
Fee Fee `json:"fee"`
MinimumLoan Money `json:"minimum_loan"`
MaximumLoan Money `json:"maximum_loan"`
MinimumTerm Months `json:"minimum_term"`
MaximumTerm Months `json:"maximum_term"`
EarlyRedemptionCharge Fee `json:"early_redemption_charge"`
IsConsumer bool `json:"is_consumer"`
IsCommercial bool `json:"is_commercial"`
BrokerOnly bool `json:"broker_only"`
Active bool `json:"active"`
Meta map[string]interface{} `json:"metadata"`
}
Mortgage struct {
ID string `json:"id"`
Issuer string `json:"issuer"`
Name string `json:"name"`
Description string `json:"description"`
URLApply string `json:"url_apply"`
URLLogo string `json:"url_logo"`
HighlightedPoints []string `json:"highlighted_points"`
TechnicalPoints []string `json:"technical_points"`
Labels []string `json:"labels"`
Type string `json:"type"`
OfferInterestRate RatePeriod `json:"offer_interest_rate"`
OfferInterestRateType string `json:"offer_interest_rate_type"`
StandardInterestRate Rate `json:"standard_interest_rate"`
StandardInterestRateType string `json:"standard_interest_rate_type"`
LoanToValue Rate `json:"loan_to_value"`
Fee Fee `json:"fee"`
MinimumLoan Money `json:"minimum_loan"`
MaximumLoan Money `json:"maximum_loan"`
MinimumTerm Months `json:"minimum_term"`
MaximumTerm Months `json:"maximum_term"`
EarlyRedemptionCharge Fee `json:"early_redemption_charge"`
IsConsumer bool `json:"is_consumer"`
IsCommercial bool `json:"is_commercial"`
BrokerOnly bool `json:"broker_only"`
Active bool `json:"active"`
Meta map[string]interface{} `json:"metadata"`
Created time.Time `json:"created"`
}
ListMortgagesResponse struct {
Data []*Mortgage `json:"data"`
}
)
func (s *ProductsService) FindMortgage(ctx context.Context, id string) (*Mortgage, error) {
ctx, span := trace.StartSpan(ctx, "lwebco.de/go-capis/ProductsService.FindMortgage")
defer span.End()
req, err := s.c.newRequest("GET", fmt.Sprintf("/v2/mortgages/%s", id), nil)
if err != nil {
return nil, err
}
res, err := s.c.Do(req.WithContext(ctx))
if err != nil {
s.c.logError(err)
return nil, ErrUnreachable
}
defer res.Body.Close()
prd := &Mortgage{}
return prd, unmarshalResponse(res, prd)
}
func (s *ProductsService) UpdateMortgage(ctx context.Context, mortgage *Mortgage) error {
ctx, span := trace.StartSpan(ctx, "lwebco.de/go-capis/ProductsService.UpdateMortgage")
defer span.End()
if len(mortgage.ID) == 0 {
return errors.New("can only update an existing mortgage")
}
rb, _ := json.Marshal(mortgage)
req, err := s.c.newRequest("PUT", fmt.Sprintf("/v2/mortgages/%s", mortgage.ID), bytes.NewReader(rb))
if err != nil {
return err
}
res, err := s.c.Do(req.WithContext(ctx))
if err != nil {
return ErrUnreachable
}
defer res.Body.Close()
return statusCodeToError(res.StatusCode)
}
func (s *ProductsService) NewMortgage(ctx context.Context, opts *NewMortgageRequest) error {
ctx, span := trace.StartSpan(ctx, "lwebco.de/go-capis/ProductsService.NewMortgage")
defer span.End()
rb, _ := json.Marshal(opts)
req, err := s.c.newRequest("POST", "/v2/mortgages", bytes.NewReader(rb))
if err != nil {
return err
}
res, err := s.c.Do(req.WithContext(ctx))
if err != nil {
return ErrUnreachable
}
defer res.Body.Close()
return statusCodeToError(res.StatusCode)
}
type MortgageProductFilters struct {
ID []string `url:"id,comma"`
}
func (s *ProductsService) ListMortgages(ctx context.Context, filters *MortgageProductFilters) (*ListMortgagesResponse, error) {
ctx, span := trace.StartSpan(ctx, "lwebco.de/go-capis/ProductsService.ListMortgages")
defer span.End()
obj := &ListMortgagesResponse{}
qs, _ := querystring.Values(filters)
req, err := s.c.newRequest("GET", "/v2/mortgages?"+qs.Encode(), nil)
if err != nil {
return nil, err
}
res, err := s.c.Do(req.WithContext(ctx))
if err != nil {
s.c.logError(err)
return nil, ErrUnreachable
}
defer res.Body.Close()
if err = statusCodeToError(res.StatusCode); err != nil {
return nil, err
}
return obj, unmarshalResponse(res, obj)
}