forked from eclipse-iofog/iofog-go-sdk
/
util.go
79 lines (69 loc) · 1.85 KB
/
util.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
/*
* *******************************************************************************
* * Copyright (c) 2019 Edgeworx, Inc.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package client
import (
"bytes"
"fmt"
"io"
"strings"
)
// AgentTypeAgentTypeIDDict Map from string agent type to numeric id
var AgentTypeAgentTypeIDDict = map[string]int{
"x86": 1,
"arm": 2,
}
// AgentTypeIDAgentTypeDict Map from numeric id agent type to string agent type
var AgentTypeIDAgentTypeDict = map[int]string{
1: "x86",
2: "arm",
}
// RegistryTypeRegistryTypeIDDict Map from string registry type to numeric id
var RegistryTypeRegistryTypeIDDict = map[string]int{
"remote": 1,
"local": 2,
}
// RegistryTypeIDRegistryTypeDict Map from numeric id registry type to string
var RegistryTypeIDRegistryTypeDict = map[int]string{
1: "remote",
2: "local",
}
func getString(in io.Reader) (out string, err error) {
buf := new(bytes.Buffer)
if _, err = buf.ReadFrom(in); err != nil {
return
}
out = buf.String()
return
}
func checkStatusCode(code int, method, url string, body io.Reader) error {
if code < 200 || code >= 300 {
bodyString, err := getString(body)
if err != nil {
return err
}
switch code {
case 404:
return NewNotFoundError(fmt.Sprintf("Received Not found from %s %s\n: %s\n", method, url, bodyString))
default:
return NewHTTPError(fmt.Sprintf("Received %d from %s %s\n%s", code, method, url, bodyString), code)
}
}
return nil
}
func before(input, substr string) string {
pos := strings.Index(input, substr)
if pos == -1 {
return input
}
return input[0:pos]
}