/
clients.go
539 lines (470 loc) · 22.4 KB
/
clients.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package swf
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/http/httputil"
"os"
"strings"
"time"
"unicode"
"github.com/bmizerany/aws4"
)
// WorkflowClient is the combined client of the ActivityWorkerClient,
// DecisionWorkerClient, KinesisClient, WorkflowAdminClient,
// WorkflowInfoClient, and WorkflowWorkerClient interfaces.
type WorkflowClient interface {
ActivityWorkerClient
DecisionWorkerClient
KinesisClient
WorkflowAdminClient
WorkflowInfoClient
WorkflowWorkerClient
}
// WorkflowWorkerClient specifies ActivityWorkerClient operations related to starting and stopping workflows.
type WorkflowWorkerClient interface {
StartWorkflow(request StartWorkflowRequest) (*StartWorkflowResponse, error)
SignalWorkflow(request SignalWorkflowRequest) error
RequestCancelWorkflowExecution(request RequestCancelWorkflowExecution) error
TerminateWorkflowExecution(request TerminateWorkflowExecution) error
}
// DecisionWorkerClient specifies swf client operations related to requesting and responding to decision tasks.
type DecisionWorkerClient interface {
PollForDecisionTask(request PollForDecisionTaskRequest) (*PollForDecisionTaskResponse, error)
RespondDecisionTaskCompleted(request RespondDecisionTaskCompletedRequest) error
}
// ActivityWorkerClient specifies swf client operations related to requesting and responding to activity tasks.
type ActivityWorkerClient interface {
PollForActivityTask(request PollForActivityTaskRequest) (*PollForActivityTaskResponse, error)
RecordActivityTaskHeartbeat(request RecordActivityTaskHeartbeatRequest) (*RecordActivityTaskHeartbeatResponse, error)
RespondActivityTaskCompleted(request RespondActivityTaskCompletedRequest) error
RespondActivityTaskFailed(request RespondActivityTaskFailedRequest) error
RespondActivityTaskCanceled(request RespondActivityTaskCanceledRequest) error
}
// WorkflowAdminClient specifies swf client operations related to registering and deprecating domains, workflows and activities.
type WorkflowAdminClient interface {
RegisterActivityType(request RegisterActivityType) error
DeprecateActivityType(request DeprecateActivityType) error
RegisterWorkflowType(request RegisterWorkflowType) error
DeprecateWorkflowType(request DeprecateWorkflowType) error
RegisterDomain(request RegisterDomain) error
DeprecateDomain(request DeprecateDomain) error
}
// WorkflowInfoClient specifies swf client operations related to querying swf types and executions.
type WorkflowInfoClient interface {
CountClosedWorkflowExecutions(request CountClosedWorkflowExecutionsRequest) (*CountResponse, error)
CountOpenWorkflowExecutions(request CountOpenWorkflowExecutionsRequest) (*CountResponse, error)
CountPendingActivityTasks(request CountPendingActivityTasksRequest) (*CountResponse, error)
CountPendingDecisionTasks(request CountPendingDecisionTasksRequest) (*CountResponse, error)
ListWorkflowTypes(request ListWorkflowTypesRequest) (*ListWorkflowTypesResponse, error)
ListActivityTypes(request ListActivityTypesRequest) (*ListActivityTypesResponse, error)
DescribeActivityType(request DescribeActivityTypeRequest) (*DescribeActivityTypeResponse, error)
DescribeDomain(request DescribeDomainRequest) (*DescribeDomainResponse, error)
DescribeWorkflowType(request DescribeWorkflowTypeRequest) (*DescribeWorkflowTypeResponse, error)
DescribeWorkflowExecution(request DescribeWorkflowExecutionRequest) (*DescribeWorkflowExecutionResponse, error)
ListOpenWorkflowExecutions(request ListOpenWorkflowExecutionsRequest) (*ListOpenWorkflowExecutionsResponse, error)
ListClosedWorkflowExecutions(request ListClosedWorkflowExecutionsRequest) (*ListClosedWorkflowExecutionsResponse, error)
GetWorkflowExecutionHistory(request GetWorkflowExecutionHistoryRequest) (*GetWorkflowExecutionHistoryResponse, error)
ListDomains(request ListDomainsRequest) (*ListDomainsResponse, error)
}
// KinesisClient specifies operations used by this library on the aws kinesis api.
type KinesisClient interface {
PutRecord(request PutRecordRequest) (*PutRecordResponse, error)
GetRecords(request GetRecordsRequest) (*GetRecordsResponse, error)
CreateStream(request CreateStream) error
DescribeStream(request DescribeStreamRequest) (*DescribeStreamResponse, error)
GetShardIterator(request GetShardIteratorRequest) (*GetShardIteratorResponse, error)
}
// Region specifies the AWS region that a client should connect to.
type Region struct {
Name string
SWFEndpoint string
KinesisEndpoint string
}
var (
// USEast1 is the AWS us-east-1 Region
USEast1 = &Region{"us-east-1", "https://swf.us-east-1.amazonaws.com", "https://kinesis.us-east-1.amazonaws.com"}
// USWest1 is the AWS us-west-1 Region
USWest1 = &Region{"us-west-1", "https://swf.us-west-1.amazonaws.com", "https://kinesis.us-west-1.amazonaws.com"}
// USWest2 is the AWS us-west-2 Region
USWest2 = &Region{"us-west-2", "https://swf.us-west-2.amazonaws.com", "https://kinesis.us-west-2.amazonaws.com"}
// EUWest1 is the AWS eu-west-1 Region
EUWest1 = &Region{"eu-west-1", "https://swf.eu-west-1.amazonaws.com", "https://kinesis.eu-west-1.amazonaws.com"}
// APNorthEast1 is the AWS ap-northeast-1 Region
APNorthEast1 = &Region{"ap-northeast-1", "https://swf.ap-northeast-1.amazonaws.com", "https://kinesis.ap-northeast-1.amazonaws.com"}
// APSouthEast1 is the AWS ap-southeast-1 Region
APSouthEast1 = &Region{"ap-southeast-1", "https://swf.ap-southeast-1.amazonaws.com", "https://kinesis.ap-southeast-1.amazonaws.com"}
)
// Client is the implementation of the WorkflowClient, DecisionWorkerClient, ActivityWorkerClient, WorkflowAdminClient, and WorkflowInfoClient interfaces.
type Client struct {
keys *aws4.Keys
httpClient *http.Client
pollingClient *http.Client
Region *Region
Debug bool
}
// NewClient creates a new Client which uses the given credentials to talk to the given region with http.DefaultClient.
func NewClient(key string, secret string, region *Region) *Client {
return &Client{
keys: &aws4.Keys{AccessKey: key, SecretKey: secret},
httpClient: http.DefaultClient,
pollingClient: http.DefaultClient,
Region: region,
}
}
// NewClientWithHTTPClient creates a new Client which uses the given credentials to talk to the given region with the specified http.Client.
func NewClientWithHTTPClient(key string, secret string, region *Region, client *http.Client) *Client {
return &Client{
keys: &aws4.Keys{AccessKey: key, SecretKey: secret},
httpClient: client,
pollingClient: client,
Region: region,
}
}
// NewClientWithHTTPAndPollingClient creates a new Client which uses the given credentials to talk to the given region with the specified http.Client,
// and does polling calls with the configured polling client
func NewClientWithHTTPAndPollingClient(key string, secret string, region *Region, client *http.Client, pollingClient *http.Client) *Client {
return &Client{
keys: &aws4.Keys{AccessKey: key, SecretKey: secret},
httpClient: client,
pollingClient: pollingClient,
Region: region,
}
}
func (c *Client) service(svc string) *aws4.Service {
return &aws4.Service{Name: svc, Region: c.Region.Name}
}
// StartWorkflow executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_StartWorkflowExecution.html
func (c *Client) StartWorkflow(request StartWorkflowRequest) (*StartWorkflowResponse, error) {
resp := &StartWorkflowResponse{}
err := c.swfReqWithResponse("StartWorkflowExecution", request, resp)
return resp, err
}
// SignalWorkflow executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_SignalWorkflowExecution.html
func (c *Client) SignalWorkflow(request SignalWorkflowRequest) error {
err := c.swfReqNoResponse("SignalWorkflowExecution", request)
return err
}
// RequestCancelWorkflowExecution executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_RequestCancelWorkflowExecution.html
func (c *Client) RequestCancelWorkflowExecution(request RequestCancelWorkflowExecution) error {
err := c.swfReqNoResponse("RequestCancelWorkflowExecution", request)
return err
}
// TerminateWorkflowExecution executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_TerminateWorkflowExecution.html
func (c *Client) TerminateWorkflowExecution(request TerminateWorkflowExecution) error {
err := c.swfReqNoResponse("TerminateWorkflowExecution", request)
return err
}
// PollForDecisionTask executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_PollForDecisionTask.html
func (c *Client) PollForDecisionTask(request PollForDecisionTaskRequest) (*PollForDecisionTaskResponse, error) {
resp := &PollForDecisionTaskResponse{}
err := c.swfReqWithResponse("PollForDecisionTask", request, resp)
return resp, err
}
// RespondDecisionTaskCompleted executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_RespondDecisionTaskCompleted.html
func (c *Client) RespondDecisionTaskCompleted(request RespondDecisionTaskCompletedRequest) error {
err := c.swfReqNoResponse("RespondDecisionTaskCompleted", request)
return err
}
// PollForActivityTask executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_PollForActivityTask.html
func (c *Client) PollForActivityTask(request PollForActivityTaskRequest) (*PollForActivityTaskResponse, error) {
resp := &PollForActivityTaskResponse{}
err := c.swfReqWithResponse("PollForActivityTask", request, resp)
return resp, err
}
// RespondActivityTaskCompleted executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_RespondActivityTaskCompleted.html
func (c *Client) RespondActivityTaskCompleted(request RespondActivityTaskCompletedRequest) error {
err := c.swfReqNoResponse("RespondActivityTaskCompleted", request)
return err
}
// RespondActivityTaskFailed executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_RespondActivityTaskFailed.html
func (c *Client) RespondActivityTaskFailed(request RespondActivityTaskFailedRequest) error {
err := c.swfReqNoResponse("RespondActivityTaskFailed", request)
return err
}
// RespondActivityTaskCanceled executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_RespondActivityTaskCanceled.html
func (c *Client) RespondActivityTaskCanceled(request RespondActivityTaskCanceledRequest) error {
err := c.swfReqNoResponse("RespondActivityTaskCanceled", request)
return err
}
// RecordActivityTaskHeartbeat executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_RecordActivityTaskHeartbeat.html
func (c *Client) RecordActivityTaskHeartbeat(request RecordActivityTaskHeartbeatRequest) (*RecordActivityTaskHeartbeatResponse, error) {
resp := &RecordActivityTaskHeartbeatResponse{}
err := c.swfReqWithResponse("RecordActivityTaskHeartbeat", request, resp)
return resp, err
}
// RegisterActivityType executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_RegisterActivityType.html
func (c *Client) RegisterActivityType(request RegisterActivityType) error {
err := c.swfReqNoResponse("RegisterActivityType", request)
return err
}
// DeprecateActivityType executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_DeprecateActivityType.html
func (c *Client) DeprecateActivityType(request DeprecateActivityType) error {
err := c.swfReqNoResponse("DeprecateActivityType", request)
return err
}
// RegisterWorkflowType executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_RegisterWorkflowType.html
func (c *Client) RegisterWorkflowType(request RegisterWorkflowType) error {
err := c.swfReqNoResponse("RegisterWorkflowType", request)
return err
}
// DeprecateWorkflowType executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_DeprecateWorkflowType.html
func (c *Client) DeprecateWorkflowType(request DeprecateWorkflowType) error {
err := c.swfReqNoResponse("DeprecateWorkflowType", request)
return err
}
// RegisterDomain executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_RegisterDomain.html
func (c *Client) RegisterDomain(request RegisterDomain) error {
err := c.swfReqNoResponse("RegisterDomain", request)
return err
}
// DeprecateDomain executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_DeprecateDomain.html
func (c *Client) DeprecateDomain(request DeprecateDomain) error {
err := c.swfReqNoResponse("DeprecateDomain", request)
return err
}
// CountClosedWorkflowExecutions executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_CountClosedWorkflowExecutions.html
func (c *Client) CountClosedWorkflowExecutions(request CountClosedWorkflowExecutionsRequest) (*CountResponse, error) {
resp := &CountResponse{}
err := c.swfReqWithResponse("CountClosedWorkflowExecutions", request, resp)
return resp, err
}
// CountOpenWorkflowExecutions executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_CountOpenWorkflowExecutions.html
func (c *Client) CountOpenWorkflowExecutions(request CountOpenWorkflowExecutionsRequest) (*CountResponse, error) {
resp := &CountResponse{}
err := c.swfReqWithResponse("CountOpenWorkflowExecutions", request, resp)
return resp, err
}
// CountPendingActivityTasks executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_CountPendingActivityTasks.html
func (c *Client) CountPendingActivityTasks(request CountPendingActivityTasksRequest) (*CountResponse, error) {
resp := &CountResponse{}
err := c.swfReqWithResponse("CountPendingActivityTasks", request, resp)
return resp, err
}
// CountPendingDecisionTasks executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_CountPendingDecisionTasks.html
func (c *Client) CountPendingDecisionTasks(request CountPendingDecisionTasksRequest) (*CountResponse, error) {
resp := &CountResponse{}
err := c.swfReqWithResponse("CountPendingDecisionTasks", request, resp)
return resp, err
}
// DescribeActivityType executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_DescribeActivityType.html
func (c *Client) DescribeActivityType(request DescribeActivityTypeRequest) (*DescribeActivityTypeResponse, error) {
resp := &DescribeActivityTypeResponse{}
err := c.swfReqWithResponse("DescribeActivityType", request, resp)
return resp, err
}
// DescribeDomain executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_DescribeDomain.html
func (c *Client) DescribeDomain(request DescribeDomainRequest) (*DescribeDomainResponse, error) {
resp := &DescribeDomainResponse{}
err := c.swfReqWithResponse("DescribeDomain", request, resp)
return resp, err
}
// DescribeWorkflowType executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_DescribeWorkflowType.html
func (c *Client) DescribeWorkflowType(request DescribeWorkflowTypeRequest) (*DescribeWorkflowTypeResponse, error) {
resp := &DescribeWorkflowTypeResponse{}
err := c.swfReqWithResponse("DescribeWorkflowType", request, resp)
return resp, err
}
// DescribeWorkflowExecution executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_DescribeWorkflowExecution.html
func (c *Client) DescribeWorkflowExecution(request DescribeWorkflowExecutionRequest) (*DescribeWorkflowExecutionResponse, error) {
resp := &DescribeWorkflowExecutionResponse{}
err := c.swfReqWithResponse("DescribeWorkflowExecution", request, resp)
return resp, err
}
// ListActivityTypes executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_ListActivityTypes.html
func (c *Client) ListActivityTypes(request ListActivityTypesRequest) (*ListActivityTypesResponse, error) {
resp := &ListActivityTypesResponse{}
err := c.swfReqWithResponse("ListActivityTypes", request, resp)
return resp, err
}
// ListWorkflowTypes executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_ListWorkflowTypes.html
func (c *Client) ListWorkflowTypes(request ListWorkflowTypesRequest) (*ListWorkflowTypesResponse, error) {
resp := &ListWorkflowTypesResponse{}
err := c.swfReqWithResponse("ListWorkflowTypes", request, resp)
return resp, err
}
// ListOpenWorkflowExecutions executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_ListOpenWorkflowExecutions.html
func (c *Client) ListOpenWorkflowExecutions(request ListOpenWorkflowExecutionsRequest) (*ListOpenWorkflowExecutionsResponse, error) {
resp := &ListOpenWorkflowExecutionsResponse{}
err := c.swfReqWithResponse("ListOpenWorkflowExecutions", request, resp)
return resp, err
}
// ListClosedWorkflowExecutions executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_ListClosedWorkflowExecutions.html
func (c *Client) ListClosedWorkflowExecutions(request ListClosedWorkflowExecutionsRequest) (*ListClosedWorkflowExecutionsResponse, error) {
resp := &ListClosedWorkflowExecutionsResponse{}
err := c.swfReqWithResponse("ListClosedWorkflowExecutions", request, resp)
return resp, err
}
// GetWorkflowExecutionHistory executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_GetWorkflowExecutionHistory.html
func (c *Client) GetWorkflowExecutionHistory(request GetWorkflowExecutionHistoryRequest) (*GetWorkflowExecutionHistoryResponse, error) {
resp := &GetWorkflowExecutionHistoryResponse{}
err := c.swfReqWithResponse("GetWorkflowExecutionHistory", request, resp)
return resp, err
}
// ListDomains executes http://docs.aws.amazon.com/amazonswf/latest/apireference/API_ListDomains.html
func (c *Client) ListDomains(request ListDomainsRequest) (*ListDomainsResponse, error) {
resp := &ListDomainsResponse{}
err := c.swfReqWithResponse("ListDomains", request, resp)
return resp, err
}
// PutRecord executes http://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html
func (c *Client) PutRecord(request PutRecordRequest) (*PutRecordResponse, error) {
resp := &PutRecordResponse{}
err := c.kinesisReqWithResponse("PutRecord", request, resp)
return resp, err
}
// GetRecords executes http://docs.aws.amazon.com/kinesis/latest/APIReference/API_GetRecords.html
func (c *Client) GetRecords(request GetRecordsRequest) (*GetRecordsResponse, error) {
resp := &GetRecordsResponse{}
err := c.kinesisReqWithResponse("GetRecords", request, resp)
return resp, err
}
// GetShardIterator executes http://docs.aws.amazon.com/kinesis/latest/APIReference/API_GetShardIterator.html
func (c *Client) GetShardIterator(request GetShardIteratorRequest) (*GetShardIteratorResponse, error) {
resp := &GetShardIteratorResponse{}
err := c.kinesisReqWithResponse("GetShardIterator", request, resp)
return resp, err
}
//CreateStream executes http://docs.aws.amazon.com/kinesis/latest/APIReference/API_CreateStream.html
func (c *Client) CreateStream(request CreateStream) error {
err := c.kinesisReqNoResponse("CreateStream", request)
return err
}
// DescribeStream executes http://docs.aws.amazon.com/kinesis/latest/APIReference/API_DescribeStream.html
func (c *Client) DescribeStream(request DescribeStreamRequest) (*DescribeStreamResponse, error) {
resp := &DescribeStreamResponse{}
err := c.kinesisReqWithResponse("DescribeStream", request, resp)
return resp, err
}
func (c *Client) swfReqNoResponse(operation string, request interface{}) error {
resp, err := c.prepareAndExecuteRequest(c.httpClient, "swf", c.Region.SWFEndpoint, "SimpleWorkflowService."+operation, "application/x-amz-json-1.0", request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
response := new(ErrorResponse)
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
return err
}
return response
}
return err
}
func (c *Client) swfReqWithResponse(operation string, request interface{}, response interface{}) error {
client := c.httpClient
switch operation {
case "PollForDecisionTask", "PollForActivityTask":
client = c.pollingClient
}
resp, err := c.prepareAndExecuteRequest(client, "swf", c.Region.SWFEndpoint, "SimpleWorkflowService."+operation, "application/x-amz-json-1.0", request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
errResp := new(ErrorResponse)
if err := json.NewDecoder(resp.Body).Decode(errResp); err != nil {
return err
}
return errResp
}
err = json.NewDecoder(resp.Body).Decode(response)
if c.Debug {
pretty, _ := json.MarshalIndent(response, "", " ")
log.Println(string(pretty))
}
return err
}
func (c *Client) kinesisReqNoResponse(operation string, request interface{}) error {
resp, err := c.prepareAndExecuteRequest(c.httpClient, "kinesis", c.Region.KinesisEndpoint, "Kinesis_20131202."+operation, "application/x-amz-json-1.1", request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
errResp := new(ErrorResponse)
if err := json.NewDecoder(resp.Body).Decode(errResp); err != nil {
return err
}
return errResp
}
return err
}
func (c *Client) kinesisReqWithResponse(operation string, request interface{}, response interface{}) error {
resp, err := c.prepareAndExecuteRequest(c.httpClient, "kinesis", c.Region.KinesisEndpoint, "Kinesis_20131202."+operation, "application/x-amz-json-1.1", request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
errResp := new(ErrorResponse)
if err := json.NewDecoder(resp.Body).Decode(errResp); err != nil {
return err
}
return errResp
}
err = json.NewDecoder(resp.Body).Decode(response)
if c.Debug {
pretty, _ := json.MarshalIndent(response, "", " ")
log.Println(string(pretty))
}
return err
}
func (c *Client) prepareAndExecuteRequest(client *http.Client, service string, url string, target string, contentType string, request interface{}) (*http.Response, error) {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(request); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, &b)
if err != nil {
return nil, err
}
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
req.Header.Set("X-Amz-Target", target)
req.Header.Set("Content-Type", contentType)
req.Header.Set("Connection", "Keep-Alive")
err = c.sign(service, req)
if err != nil {
return nil, err
}
if c.Debug {
out, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}
multiln(string(out))
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if c.Debug {
defer resp.Body.Close()
out, err := httputil.DumpResponse(resp, true)
if err != nil {
return nil, err
}
multiln(string(out))
}
return resp, nil
}
func (c *Client) sign(svc string, request *http.Request) error {
return c.service(svc).Sign(c.keys, request)
}
func multiln(s string) {
lines := strings.Split(s, "\n")
for _, line := range lines {
ln := strings.TrimRightFunc(line, unicode.IsSpace)
log.Printf("line=%q\n", ln)
}
}
// MustGetenv reads an environment variable, and panics if its not defined.
func MustGetenv(key string) string {
value := os.Getenv(key)
if value == "" {
panic("Missing key " + key)
}
return value
}