-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgalaxy.go
339 lines (306 loc) · 9.99 KB
/
galaxy.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
// blend4go Galaxy API client library for Golang
//
// For most use cases, NewGalaxyInstance and GetAPIKey are the only functions in the root package that should be used.
// Subpackages provide their own implementations of the remaining functions and those should be preferred.
// Subpackages are organised by subject. See workflows, users, tools, roles, repositories, libraries, jobs, histories, groups, or datatypes subpackages for more information.
//
// A number of subpackages are currently unimplemented, they will be implemented with interest in this project or as needed
package blend4go
import (
"context"
"errors"
"fmt"
"github.com/go-resty/resty/v2"
"io"
"log"
"path"
"reflect"
"runtime"
"runtime/debug"
"strings"
)
// https://blog.golang.org/publishing-go-modules
// https://github.com/go-resty/resty
// https://pkg.go.dev/github.com/go-resty/resty
type GalaxyID = string
type GalaxyRequest = *resty.Request
type StatusResponse struct {
Status string `json:"status"`
Message string `json:"message"`
}
type ErrorResponse struct {
URL string
Method string
Message1 string `json:"message"`
Code1 string `json:"code"`
Message string `json:"err_msg"`
Code int `json:"err_code"`
}
func (e *ErrorResponse) String() string {
return fmt.Sprintf("%v %v %v%v: %v%v", e.Method, e.URL, e.Code, e.Code1, e.Message, e.Message1)
}
func (e *ErrorResponse) Error() string {
return e.String()
}
type LogLevel int
const (
NONE LogLevel = iota
ERROR
WARN
INFO
DEBUG
)
type GalaxyInstance struct {
Client *resty.Client
logErr *log.Logger
logInfo *log.Logger
logDebug *log.Logger
logLevel LogLevel
}
type GalaxyModel interface {
GetBasePath() string
SetGalaxyInstance(*GalaxyInstance)
GetID() GalaxyID
SetID(GalaxyID)
}
// returns an API key for authenticated user based on BaseAuth headers
// Username is the users email address until https://github.com/galaxyproject/galaxy/pull/10521
func GetAPIKey(ctx context.Context, host, username, password string) (string, error) {
r := resty.New()
r.SetHostURL(host)
log.Printf("[DEBUG] trying to get api key %v", host)
r.SetHeader("Accept", "application/json")
r.SetBasicAuth(username, password)
if res, err := r.R().SetError(&ErrorResponse{}).SetContext(ctx).SetResult(map[string]interface{}{}).Get("/api/authenticate/baseauth"); err == nil {
if result, err := HandleResponse(res); err == nil {
return (*result.(*map[string]interface{}))["api_key"].(string), nil
} else {
return "", err
}
} else {
return "", err
}
}
// Handle responses from Galaxy API, checking for errors
func HandleResponse(response *resty.Response) (interface{}, error) {
if response.IsError() {
err := response.Error().(*ErrorResponse)
err.Method = response.Request.Method
err.URL = response.Request.URL
if err.Message == "" {
err.Message = err.Message1
}
if err.Message == "" {
return nil, fmt.Errorf("%v %v: %v", response.Request.Method, response.Request.URL, string(response.Body()))
}
return nil, err
}
if response.IsSuccess() {
return response.Result(), nil
}
return nil, fmt.Errorf("unhandled response: %v", response.Status())
}
// Create a new connection handle to an instance of Galaxy
func NewGalaxyInstanceLogger(host, apiKey string, logWriter io.Writer, logLevel LogLevel) (g *GalaxyInstance) {
// Automatically attach caller package name to agent
pc, _, _, _ := runtime.Caller(1)
components := strings.Split(runtime.FuncForPC(pc).Name(), ".")
if len(components) > 1 {
components = components[:len(components)-1]
}
agent := strings.Join(components, ".") + " - blend4go"
if info, ok := debug.ReadBuildInfo(); ok {
agent = agent + " " + info.Main.Version + " " + info.Main.Sum
}
r := resty.New()
r.SetHostURL(host)
r.SetHeader("X-API-KEY", apiKey)
r.SetHeader("Accept", "application/json")
r.SetHeader("Content-Type", "application/json")
r.SetHeader("User-Agent", agent)
return &GalaxyInstance{
Client: r,
logErr: log.New(logWriter, "[Error]", log.Ldate|log.Ltime|log.Lshortfile),
logInfo: log.New(logWriter, "[Info]", log.Ldate|log.Ltime|log.Lshortfile),
logDebug: log.New(logWriter, "[Debug]", log.Ldate|log.Ltime|log.Lshortfile),
logLevel: logLevel,
}
}
func NewGalaxyInstance(host, apiKey string) (g *GalaxyInstance) {
return NewGalaxyInstanceLogger(host, apiKey, log.Writer(), ERROR)
}
func (g *GalaxyInstance) Error(v ...interface{}) {
if g.logLevel >= ERROR {
g.logErr.Print(v...)
}
}
func (g *GalaxyInstance) Errorf(format string, v ...interface{}) {
if g.logLevel >= ERROR {
g.logErr.Printf(format, v...)
}
}
func (g *GalaxyInstance) Info(v ...interface{}) {
if g.logLevel >= INFO {
g.logErr.Print(v...)
}
}
func (g *GalaxyInstance) Infof(format string, v ...interface{}) {
if g.logLevel >= INFO {
g.logErr.Printf(format, v...)
}
}
func (g *GalaxyInstance) Debug(v ...interface{}) {
if g.logLevel >= DEBUG {
g.logErr.Print(v...)
}
}
func (g *GalaxyInstance) Debugf(format string, v ...interface{}) {
if g.logLevel >= DEBUG {
g.logErr.Printf(format, v...)
}
}
func (g *GalaxyInstance) HandleResponse(response *resty.Response) (interface{}, error) {
g.Infof("%v %v", response.Request.Method, response.Request.URL)
g.Debugf("Request: %+v\nResponse: %+v", response.Request.RawRequest, response.RawResponse)
g.Infof("%v %v", response.StatusCode(), response.Status())
res, err := HandleResponse(response)
if err != nil {
g.Debug(err)
}
return res, err
}
// Helper to make generic requests against Galaxy API that return lists of objects
// params is a map of query parameters to add to the request
func (g *GalaxyInstance) List(ctx context.Context, path string, models interface{}, params *map[string]string) (interface{}, error) {
r := g.R(ctx)
if params != nil {
r.SetQueryParams(*params)
}
if res, err := r.SetResult(models).Get(path); err == nil {
if results, err := HandleResponse(res); err == nil {
v := reflect.Indirect(reflect.ValueOf(results))
if v.Kind() == reflect.Slice {
for i := 0; i < v.Len(); i++ {
if m, ok := v.Index(i).Interface().(GalaxyModel); ok {
m.SetGalaxyInstance(g)
} else {
return nil, errors.New("models param element does not implement GalaxyModel")
}
}
return results, nil
} else {
return nil, errors.New("models param was not of type slice")
}
} else {
return nil, err
}
} else {
return nil, err
}
}
// Helper to make generic requests against Galaxy API that returns single object
// params is a map of query parameters to add to the request
func (g *GalaxyInstance) Get(ctx context.Context, id GalaxyID, model GalaxyModel, params *map[string]string) (GalaxyModel, error) {
r := g.R(ctx)
if params != nil {
r.SetQueryParams(*params)
}
if res, err := r.SetResult(model).Get(path.Join(model.GetBasePath(), id)); err == nil {
if result, err := HandleResponse(res); err == nil {
m := result.(GalaxyModel)
m.SetGalaxyInstance(g)
return m, nil
} else {
return nil, err
}
} else {
return nil, err
}
}
// Helper to make generic PUT requests to Galaxy API to update objects
// params is a map of query parameters to add to the request
func (g *GalaxyInstance) Put(ctx context.Context, model GalaxyModel, params *map[string]string) (GalaxyModel, error) {
r := g.R(ctx)
if params != nil {
r.SetQueryParams(*params)
}
if res, err := r.SetResult(model).SetBody(model).Put(path.Join(model.GetBasePath(), model.GetID())); err == nil {
if result, err := HandleResponse(res); err == nil {
m := result.(GalaxyModel)
m.SetGalaxyInstance(g)
return m, nil
} else {
return nil, err
}
} else {
return nil, err
}
}
// Helper to make generic PATCH requests to Galaxy API to update objects
// params is a map of query parameters to add to the request
func (g *GalaxyInstance) Patch(ctx context.Context, model GalaxyModel, params *map[string]string) (GalaxyModel, error) {
r := g.R(ctx)
if params != nil {
r.SetQueryParams(*params)
}
if res, err := r.SetResult(model).SetBody(model).Patch(path.Join(model.GetBasePath(), model.GetID())); err == nil {
if result, err := HandleResponse(res); err == nil {
m := result.(GalaxyModel)
m.SetGalaxyInstance(g)
return m, nil
} else {
return nil, err
}
} else {
return nil, err
}
}
// Helper to make generic DELETE requests to delete single objects
// params is a map of query parameters to add to the request
func (g *GalaxyInstance) Delete(ctx context.Context, model GalaxyModel, params *map[string]string) error {
r := g.R(ctx)
if params != nil {
r.SetQueryParams(*params)
}
if res, err := r.Delete(path.Join(model.GetBasePath(), model.GetID())); err == nil {
_, err := HandleResponse(res)
return err
} else {
return err
}
}
// Helper to create a new request to the Galaxy API
// Use this if one of the other functions in this package are not appropriate for the request
func (g *GalaxyInstance) R(ctx context.Context) GalaxyRequest {
return g.Client.R().SetContext(ctx).SetError(&ErrorResponse{})
}
type ToolShed struct {
Name string `json:"name"`
Url string `json:"url"`
}
// Get a list of toolsheds configured in the galaxy instance
func (g *GalaxyInstance) ToolSheds(ctx context.Context) ([]*ToolShed, error) {
//GET /api/tool_shed Interact with the Toolshed registry of this instance.
if res, err := g.R(ctx).SetResult([]*ToolShed{}).Get("/api/tool_shed"); err == nil {
return res.Result().([]*ToolShed), nil
} else {
return nil, err
}
}
//GET /api/tool_shed/request
//GET /api/whoami Return information about the current authenticated user.
//GET /api/configuration Return an object containing exposable configuration settings.
// Return a description of the major version of Galaxy (e.g. 15.03).
func (g *GalaxyInstance) Version(ctx context.Context) (string, error) {
//GET /api/version
if res, err := g.R(ctx).SetResult(map[string]interface{}{}).Get("/api/version"); err == nil {
if result, err := HandleResponse(res); err == nil {
return (*result.(*map[string]interface{}))["version_major"].(string), nil
} else {
return "", err
}
} else {
return "", err
}
}
//PUT /api/configuration/toolbox Reload the Galaxy toolbox (but not individual tools).