-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit.go
118 lines (103 loc) · 2.94 KB
/
unit.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
//********************************************************************************************************************//
//
// Copyright (C) 2018 - 2022 J&J Ideenschmiede GmbH <[email protected]>
//
// This file is part of gosw6.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor (aka gowizzard)
//
//********************************************************************************************************************//
package gosw6
import (
"encoding/json"
"fmt"
"net/url"
"time"
)
// UnitsReturn is to decode the json data
type UnitsReturn struct {
Total int `json:"total"`
Data []struct {
ShortCode string `json:"shortCode"`
Name string `json:"name"`
Translations interface{} `json:"translations"`
Products interface{} `json:"products"`
UniqueIdentifier string `json:"_uniqueIdentifier"`
VersionId interface{} `json:"versionId"`
Translated struct {
ShortCode string `json:"shortCode"`
Name string `json:"name"`
CustomFields struct {
} `json:"customFields"`
} `json:"translated"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt interface{} `json:"updatedAt"`
Extensions struct {
ForeignKeys struct {
ApiAlias interface{} `json:"apiAlias"`
Extensions []interface{} `json:"extensions"`
} `json:"foreignKeys"`
} `json:"extensions"`
Id string `json:"id"`
CustomFields interface{} `json:"customFields"`
ApiAlias string `json:"apiAlias"`
} `json:"data"`
Aggregations []interface{} `json:"aggregations"`
Errors []struct {
Code string `json:"code"`
Status string `json:"status"`
Title string `json:"title"`
Detail string `json:"detail"`
Meta struct {
Trace []struct {
File string `json:"file"`
Line int `json:"line"`
Function string `json:"function"`
Class string `json:"class"`
Type string `json:"type"`
} `json:"trace"`
File string `json:"file"`
Line int `json:"line"`
} `json:"meta"`
} `json:"errors"`
}
// Units are to get a list of all units
func Units(parameter map[string]string, r Request) (UnitsReturn, error) {
// Set config for request
c := Config{
Path: "/api/unit",
Method: "GET",
Body: nil,
}
// Parse url & add attributes
parse, err := url.Parse(c.Path)
if err != nil {
return UnitsReturn{}, err
}
newUrl, err := url.ParseQuery(parse.RawQuery)
if err != nil {
return UnitsReturn{}, err
}
for index, value := range parameter {
newUrl.Add(index, value)
}
// Set new url
parse.RawQuery = newUrl.Encode()
c.Path = fmt.Sprintf("%s", parse)
// Send request
response, err := c.Send(r)
if err != nil {
return UnitsReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode UnitsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return UnitsReturn{}, err
}
// Return data
return decode, err
}