-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_4_start.patch
268 lines (267 loc) · 6.26 KB
/
step_4_start.patch
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
diff --git a/config/config.yml b/config/config.yml
index d05ab2f..2e3186e 100644
--- a/config/config.yml
+++ b/config/config.yml
@@ -1,3 +1,6 @@
cmd.name: openweather
debug.mode: true
+openweather:
+ apiurl: http://api.openweathermap.org/data/2.5/
+ apikey: d6cca0cbd5a5f50515df2afba93e5fa6
diff --git a/go.mod b/go.mod
index 7106416..ee34a85 100644
--- a/go.mod
+++ b/go.mod
@@ -7,5 +7,7 @@ require (
flamingo.me/flamingo/v3 v3.0.0-beta.2.0.20190729150101-4d8dc5d8a2ba
flamingo.me/pugtemplate v1.0.0-alpha.1
github.com/go-test/deep v1.0.1
+ github.com/pact-foundation/pact-go v0.0.13
github.com/stretchr/objx v0.2.0 // indirect
+ github.com/stretchr/testify v1.3.0
)
diff --git a/src/openweather/infrastructure/openweather/adapter.go b/src/openweather/infrastructure/openweather/adapter.go
new file mode 100644
index 0000000..2804472
--- /dev/null
+++ b/src/openweather/infrastructure/openweather/adapter.go
@@ -0,0 +1,44 @@
+package openweather
+
+import (
+ "context"
+ "errors"
+
+ "flamingo.me/example-openweather/src/openweather/domain"
+)
+
+type (
+ // Adapter for openweather
+ Adapter struct {
+ apiClient *APIClient
+ }
+
+ weatherDto struct {
+ // use https://mholt.github.io/json-to-go/ to generate it easily
+ }
+)
+
+var (
+ // Check if we really implement the interface during compilation
+ _ domain.Service = (*Adapter)(nil)
+ // ErrNoWeather is returned if no weather data is available
+ ErrNoWeather = errors.New("no weather data")
+)
+
+// Inject dependencies
+func (a *Adapter) Inject(
+ client *APIClient,
+) *Adapter {
+ a.apiClient = client
+
+ return a
+}
+
+// GetByCity returns the weather for the given city
+func (a *Adapter) GetByCity(ctx context.Context, city string) (domain.Weather, error) {
+ return domain.Weather{}, ErrNoWeather
+}
+
+func mapDto(dto *weatherDto) (domain.Weather, error) {
+ return domain.Weather{}, nil
+}
diff --git a/src/openweather/infrastructure/openweather/adapter_test.go b/src/openweather/infrastructure/openweather/adapter_test.go
new file mode 100644
index 0000000..7c2252a
--- /dev/null
+++ b/src/openweather/infrastructure/openweather/adapter_test.go
@@ -0,0 +1,100 @@
+package openweather
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "testing"
+
+ "flamingo.me/flamingo/v3/framework/flamingo"
+ "flamingo.me/flamingo/v3/framework/testutil"
+ "github.com/pact-foundation/pact-go/dsl"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestAdapter(t *testing.T) {
+ testutil.WithPact(t, "flamingo-helloworld", "openweather", PactTestOpenWeatherAdapter)
+}
+
+func PactTestOpenWeatherAdapter(t *testing.T, pact *dsl.Pact) {
+ stringFixtureContent := string(`
+{
+ "coord": {
+ "lon": -0.13,
+ "lat": 51.51
+ },
+ "weather": [
+ {
+ "id": 300,
+ "main": "Drizzle",
+ "description": "light intensity drizzle",
+ "icon": "09d"
+ }
+ ],
+ "base": "stations",
+ "main": {
+ "temp": 280.32,
+ "pressure": 1012,
+ "humidity": 81,
+ "temp_min": 279.15,
+ "temp_max": 281.15
+ },
+ "visibility": 10000,
+ "wind": {
+ "speed": 4.1,
+ "deg": 80
+ },
+ "clouds": {
+ "all": 90
+ },
+ "dt": 1485789600,
+ "sys": {
+ "type": 1,
+ "id": 5091,
+ "message": 0.0103,
+ "country": "GB",
+ "sunrise": 1485762037,
+ "sunset": 1485794875
+ },
+ "id": 2643743,
+ "name": "London",
+ "cod": 200
+}
+`)
+
+ pact.AddInteraction().
+ UponReceiving("An request to openweather").
+ Given("Data for London exists").
+ WithRequest(dsl.Request{
+ Method: "GET",
+ Path: "/data/2.5/weather",
+ Query: "appid=APIKEY&q=London&units=metric", // Correct Order!
+ }).
+ WillRespondWith(dsl.Response{
+ Status: 200,
+ Body: stringFixtureContent,
+ })
+
+ if err := pact.Verify(func() error {
+ var adapter = new(Adapter).Inject(
+ &APIClient{
+ baseURL: fmt.Sprintf("http://%s:%d/data/2.5", pact.Host, pact.Server.Port),
+ apiKey: "APIKEY",
+ logger: flamingo.NullLogger{},
+ httpClient: http.DefaultClient,
+ },
+ )
+
+ weatherInfo, e := adapter.GetByCity(context.Background(), "London")
+
+ require.NoError(t, e)
+
+ assert.Equal(t, "Drizzle", weatherInfo.MainCharacter)
+ assert.Equal(t, int(280), weatherInfo.Temp)
+
+ return nil
+ }); err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/src/openweather/infrastructure/openweather/apiClient.go b/src/openweather/infrastructure/openweather/apiClient.go
new file mode 100644
index 0000000..7cc056e
--- /dev/null
+++ b/src/openweather/infrastructure/openweather/apiClient.go
@@ -0,0 +1,83 @@
+package openweather
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+ "strings"
+
+ "flamingo.me/flamingo/v3/framework/flamingo"
+)
+
+type (
+ // APIClient for openweather
+ APIClient struct {
+ baseURL string
+ apiKey string
+ httpClient *http.Client
+ logger flamingo.Logger
+ }
+)
+
+// Inject dependencies
+func (c *APIClient) Inject(
+ httpClient *http.Client,
+ logger flamingo.Logger,
+ cfg *struct {
+ BaseURL string `inject:"config:openweather.apiurl"`
+ APIKey string `inject:"config:openweather.apikey"`
+ },
+) *APIClient {
+ c.httpClient = httpClient
+ c.logger = logger
+ if cfg != nil {
+ c.baseURL = cfg.BaseURL
+ c.apiKey = cfg.APIKey
+ }
+
+ return c
+}
+
+func (c *APIClient) request(ctx context.Context, method, path string, body io.Reader) (*http.Response, error) {
+ path = strings.TrimRight(c.baseURL, "/") + "/" + strings.TrimLeft(path, "/")
+
+ c.logger.Debugf("openweather.apiClient.request: method: %v path: %v", method, path)
+
+ u, err := url.Parse(path)
+ if err != nil {
+ return nil, err
+ }
+
+ query := u.Query()
+ query.Add("appid", c.apiKey)
+ query.Add("units", "metric")
+
+ u.RawQuery = query.Encode()
+
+ c.logger.Info("Requesting", u.String())
+
+ request, err := http.NewRequest(method, u.String(), body)
+ if err != nil {
+ return nil, err
+ }
+ request.Header.Set("Content-Type", "application/json")
+
+ response, err := c.httpClient.Do(request.WithContext(ctx))
+
+ if err != nil {
+ return nil, err
+ }
+
+ return response, nil
+}
+
+// errorFromResponse helper
+func (c *APIClient) errorFromResponse(response *http.Response) error {
+ c.logger.Error(fmt.Sprintf("openweather.apiClient Unexpected Status. %v Body: %v", response.StatusCode, response.Body))
+ b, _ := ioutil.ReadAll(response.Body)
+ return errors.New(string(b))
+}