/
billingclient.go
188 lines (177 loc) · 6.93 KB
/
billingclient.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
/*
* Copyright © 2021 peizhaoyou <peizhaoyou@4paradigm.com>
*
* 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 billingclient provides billingclient.
package billingclient
import (
"context"
"encoding/json"
"github.com/4paradigm/openaios-platform/src/internal/billingclient/apigen"
"github.com/4paradigm/openaios-platform/src/internal/response"
"github.com/labstack/gommon/log"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"strconv"
"time"
)
func GetBillingClient(billingServerURL string) (*apigen.Client, error) {
return apigen.NewClient(billingServerURL)
}
func InitUserBillingAccount(client *apigen.Client, userID string, internalURL string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
callbackURL := internalURL + "/internal-api/releases?user=" + userID
// TODO REMOVE
initBalance := 1000.0
params := apigen.PostAccountUseridParams{CallbackUrl: callbackURL, Balance: &initBalance}
resp, err := client.PostAccountUserid(ctx, userID, ¶ms)
if err != nil {
log.Error(err.Error())
return errors.New("cannot create user billing account")
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusConflict {
log.Error(resp.Body)
return errors.New("cannot create user billing account")
}
return nil
}
func GetUserBalance(client *apigen.Client, userID string) (*float64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetAccountUseridBalance(ctx, userID)
if err != nil {
log.Error(err.Error())
return nil, errors.New("cannot get user account balance")
}
if resp.StatusCode != http.StatusOK {
log.Error(resp.StatusCode)
return nil, errors.New("cannot get user account balance")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err.Error())
return nil, errors.New("cannot get user account balance")
}
var balance float64
err = json.Unmarshal(body, &balance)
if err != nil {
log.Error(err.Error())
return nil, errors.New("cannot get user account balance")
}
return &balance, nil
}
func GetOneComputeUnit(client *apigen.Client, userID string, computeunitID string) (*apigen.ComputeunitInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetComputeunitUseridComputeunitIdComputeunitId(ctx, userID, computeunitID)
if err != nil {
return nil, errors.Wrap(err, "cannot get single computeunit "+response.GetRuntimeLocation())
}
if resp.StatusCode == http.StatusBadRequest {
return nil, nil
} else if resp.StatusCode != http.StatusOK {
return nil, errors.New(strconv.FormatInt(int64(resp.StatusCode), 10) + " computeunit not exists. " + response.GetRuntimeLocation())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "cannot get single computeunit "+response.GetRuntimeLocation())
}
computeunitInfo := apigen.ComputeunitInfo{}
err = json.Unmarshal(body, &computeunitInfo)
if err != nil {
return nil, errors.Wrap(err, "computeunit not exists. "+response.GetRuntimeLocation())
}
return &computeunitInfo, nil
}
func GetComputeUnitListByUserID(client *apigen.Client, userID string) ([]apigen.ComputeunitInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetComputeunitUserid(ctx, userID)
if err != nil {
return nil, errors.Wrap(err, "cannot get computeunit list "+response.GetRuntimeLocation())
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(strconv.FormatInt(int64(resp.StatusCode), 10) + " cannot get computeunit list " + response.GetRuntimeLocation())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "cannot get computeunit list "+response.GetRuntimeLocation())
}
computeunitList := []apigen.ComputeunitInfo{}
err = json.Unmarshal(body, &computeunitList)
if err != nil {
return nil, errors.Wrap(err, "cannot get computeunit list "+response.GetRuntimeLocation())
}
return computeunitList, nil
}
func GetComputeUnitListByGroupName(client *apigen.Client, groupName string) ([]apigen.ComputeunitInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.GetComputeunitGroupGroupName(ctx, groupName)
if err != nil {
return nil, errors.Wrap(err, "cannot get computeunit list "+response.GetRuntimeLocation())
}
if resp.StatusCode == http.StatusBadRequest {
return nil, nil
} else if resp.StatusCode != http.StatusOK {
return nil, errors.New(strconv.FormatInt(int64(resp.StatusCode), 10) + " cannot get computeunit list " + response.GetRuntimeLocation())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "cannot get computeunit list "+response.GetRuntimeLocation())
}
computeunitList := []apigen.ComputeunitInfo{}
err = json.Unmarshal(body, &computeunitList)
if err != nil {
return nil, errors.Wrap(err, "cannot get computeunit list "+response.GetRuntimeLocation())
}
return computeunitList, nil
}
func GetComputeUnitPrice(client *apigen.Client, ComputeUnitID string) (float64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
params := apigen.GetComputeunitPriceParams{ComputeunitId: ComputeUnitID}
resp, err := client.GetComputeunitPrice(ctx, ¶ms)
if err != nil {
return 0, errors.Wrap(err, "Cannot get computeunit price. "+response.GetRuntimeLocation())
}
if resp.StatusCode != http.StatusOK {
return 0, errors.New("Cannot get computeunit price. " + response.GetRuntimeLocation())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, errors.Wrap(err, "Cannot get computeunit price. "+response.GetRuntimeLocation())
}
var price float64
err = json.Unmarshal(body, &price)
if err != nil {
return 0, errors.Wrap(err, "Cannot get computeunit price. "+response.GetRuntimeLocation())
}
return price, nil
}
func AddComputeunitGroupToUser(client *apigen.Client, userID string, groupName string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
params := apigen.PostComputeunitUseridParams{GroupName: groupName}
resp, err := client.PostComputeunitUserid(ctx, userID, ¶ms)
if err != nil {
return errors.Wrap(err, "cannot add group to user "+response.GetRuntimeLocation())
}
if resp.StatusCode != http.StatusOK {
return errors.New("cannot add group to user " + response.GetRuntimeLocation())
}
return nil
}