-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_transact.go
114 lines (98 loc) · 3.05 KB
/
http_transact.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
/*
* Copyright © 2022 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package skill
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
"github.com/atomist-skills/go-skill/environment"
"github.com/atomist-skills/go-skill/internal"
"github.com/google/uuid"
"olympos.io/encoding/edn"
)
func createHttpMessageSender(workspace string, apikey string, correlationId string, logger Logger) messageSender {
return messageSender{
Transact: func(entities interface{}) error {
return httpTransact(entities, "", workspace, apikey, correlationId, logger)
},
TransactOrdered: func(entities interface{}, orderingKey string) error {
return httpTransact(entities, orderingKey, workspace, apikey, correlationId, logger)
},
}
}
func httpTransact(entities interface{}, orderingKey string, workspace string, apikey string, correlationId string, logger Logger) error {
var entityArray []interface{}
rt := reflect.TypeOf(entities)
switch rt.Kind() {
case reflect.Array:
case reflect.Slice:
entityArray = entities.([]interface{})
default:
entityArray = []any{entities}
}
transactions, err := makeTransaction(entityArray, orderingKey)
if err != nil {
return err
}
flattenedEntities := transactions.Data
bs, err := edn.MarshalPPrint(flattenedEntities, nil)
if err != nil {
return err
}
message := internal.ResponseMessage{
ApiVersion: "2",
CorrelationId: "",
Team: internal.Team{
Id: workspace,
},
Type: "facts_ingestion",
Entities: string(bs),
}
if correlationId != "" {
message.CorrelationId = correlationId
} else if orderingKey != "" {
message.CorrelationId = orderingKey
} else {
message.CorrelationId = uuid.NewString()
}
client := http.DefaultClient
logger.Debugf("Transacting entities with correlation id %s:\n%s", message.CorrelationId, string(bs))
j, _ := json.MarshalIndent(message, "", " ")
url := "https://api.scout.docker.com/v1/skills/remote/" + workspace
if environment.IsStaging() {
url = "https://api.scout-stage.docker.com/v1/skills/remote/" + workspace
}
httpReq, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(j))
if err != nil {
return err
}
httpReq.Header.Set("Authorization", "Bearer "+apikey)
httpReq.Header.Set("x-atomist-correlation-id", message.CorrelationId)
if orderingKey != "" {
httpReq.Header.Set("x-atomist-ordering-key", message.CorrelationId)
}
resp, err := client.Do(httpReq)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 202 {
return fmt.Errorf("error transacting entities: %s", resp.Status)
}
return nil
}