-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
578 lines (489 loc) · 16.7 KB
/
client.go
File metadata and controls
578 lines (489 loc) · 16.7 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package mcapi
import (
"crypto/tls"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-resty/resty/v2"
"github.com/materials-commons/hydra/pkg/mcdb/mcmodel"
)
// DataWrapper wraps json responses that have a data key before getting to the data, eg
// {"data": {"id": 1,... }}. It is used to get at the underlying data object.
type DataWrapper struct {
Data any `json:"data"`
}
type ErrorResponse struct {
Error string `json:"error"`
}
// APIError is an error that stores the StatusCode and Status from the response.
type APIError struct {
StatusCode int
Status string
}
// Error implements the Error interface.
func (e *APIError) Error() string {
return fmt.Sprintf("api error: %d %s", e.StatusCode, e.Status)
}
// NewAPIError creates an instance of APIError from a resty.Response. It extracts the
// StatusCode and Status from the response.
func NewAPIError(resp *resty.Response) *APIError {
respErr := ""
if resp.Error() != nil {
errResponse := resp.Error().(*ErrorResponse)
respErr = errResponse.Error
if respErr == "" {
respErr = string(resp.Body())
}
}
return &APIError{
StatusCode: resp.StatusCode(),
Status: fmt.Sprintf("%s: %s", resp.Status(), respErr),
}
}
// Used for now to resolve the checking
var tlsConfig = tls.Config{InsecureSkipVerify: true}
// Client is REST client for the Materials Commons API.
type Client struct {
APIKey string
BaseURL string
rClient *resty.Client
}
// ClientArgs are the arguments when creating the client. You specify the URL to the server and the
// API Key for the user. If BaseURL is blank then it defaults to https://materialscommons.org/api.
type ClientArgs struct {
APIKey string
BaseURL string
}
// NewClient creates a new client, sets the Accept and Content-Type headers to
// "application/json", and sets the Authorization header to the token. It
// does a small amount of cleaning on the BaseURL by removing the trailing
// slashes in the baseURL so the API can construct paths easier.
func NewClient(args *ClientArgs) *Client {
c := resty.New().
SetTLSClientConfig(&tlsConfig).
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetAuthToken(args.APIKey)
baseURL := "https://materialscommons.org/api"
if args.BaseURL != "" {
baseURL = strings.TrimSuffix(args.BaseURL, "/")
}
return &Client{
BaseURL: baseURL,
APIKey: args.APIKey,
rClient: c,
}
}
func (c *Client) SetDebug(on bool) {
c.rClient.SetDebug(on)
}
func checkError(resp *resty.Response, err error) error {
if err != nil {
return err
}
if resp.IsError() {
return NewAPIError(resp)
}
return nil
}
func (c *Client) r() *resty.Request {
return c.rClient.R()
}
// CreateProject creates a new project with the specified parameters in CreateProjectRequest and returns the created project.
func (c *Client) CreateProject(req CreateProjectRequest) (*mcmodel.Project, error) {
proj := &mcmodel.Project{}
url := c.BaseURL + "/projects"
resp, err := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{proj}).
Post(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return proj, nil
}
// GetProject retrieves the project details for the given project ID.
// It returns a pointer to the Project object and an error, if any.
func (c *Client) GetProject(id int) (*mcmodel.Project, error) {
proj := &mcmodel.Project{}
url := c.BaseURL + fmt.Sprintf("/projects/%d", id)
resp, err := c.r().
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{proj}).
Get(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return proj, nil
}
// DeleteProject deletes a project identified by the provided ID.
// It makes a DELETE request to the API endpoint corresponding to the given project ID.
// Returns an error if the project could not be deleted or if there is any issue with the request.
func (c *Client) DeleteProject(id int) error {
url := c.BaseURL + fmt.Sprintf("/projects/%d", id)
resp, err := c.r().Delete(url)
return checkError(resp, err)
}
// CreateExperiment creates a new experiment based on the given CreateExperimentRequest.
func (c *Client) CreateExperiment(request CreateExperimentRequest) (*mcmodel.Experiment, error) {
experiment := &mcmodel.Experiment{}
url := c.BaseURL + "/experiments"
resp, err := c.r().
SetBody(request).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{experiment}).
Post(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return experiment, nil
}
// CreateDataset creates a new dataset within the specified project.
// It takes a projectID and a CreateOrUpdateDatasetRequest as parameters.
// It returns a pointer to the created Dataset object or an error, if any occurs.
func (c *Client) CreateDataset(projectID int, req CreateOrUpdateDatasetRequest) (*mcmodel.Dataset, error) {
dataset := &mcmodel.Dataset{}
url := c.BaseURL + fmt.Sprintf("/projects/%d/datasets", projectID)
resp, err := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{dataset}).
Post(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return dataset, nil
}
func (c *Client) GetDataset(projectID int, datasetID int) (*mcmodel.Dataset, error) {
dataset := &mcmodel.Dataset{}
url := c.BaseURL + fmt.Sprintf("/projects/%d/datasets/%d", projectID, datasetID)
resp, err := c.r().
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{dataset}).
Get(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return dataset, nil
}
// UpdateDataset updates an existing dataset for the given project.
// Takes in a projectID, datasetID, and a CreateOrUpdateDatasetRequest object.
// Returns the updated Dataset object or an error if the update fails.
func (c *Client) UpdateDataset(projectID int, datasetID int, req CreateOrUpdateDatasetRequest) (*mcmodel.Dataset, error) {
dataset := &mcmodel.Dataset{}
url := c.BaseURL + fmt.Sprintf("/projects/%d/datasets/%d", projectID, datasetID)
resp, err := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{dataset}).
Put(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return dataset, nil
}
// UpdateDatasetFileSelection updates the file selection criteria for a specified dataset within a project.
// It includes and excludes specified files and directories based on the DatasetFileSelection object.
func (c *Client) UpdateDatasetFileSelection(projectID, datasetID int, fileSelection DatasetFileSelection) (*mcmodel.Dataset, error) {
dataset := &mcmodel.Dataset{}
if fileSelection.ExcludeFiles == nil {
fileSelection.ExcludeFiles = []string{}
}
if fileSelection.IncludeFiles == nil {
fileSelection.IncludeFiles = []string{}
}
if fileSelection.ExcludeDirs == nil {
fileSelection.ExcludeDirs = []string{}
}
if fileSelection.IncludeDirs == nil {
fileSelection.IncludeDirs = []string{}
}
url := c.BaseURL + fmt.Sprintf("/projects/%d/datasets/%d/change_file_selection", projectID, datasetID)
resp, err := c.r().
SetBody(fileSelection).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{dataset}).
Put(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return dataset, nil
}
// PublishDataset publishes a specified dataset by its datasetID in a particular project identified by projectID.
// Returns the published Dataset object or an error if the operation fails.
func (c *Client) PublishDataset(projectID int, datasetID int, publishAsTestDataset bool) (*mcmodel.Dataset, error) {
dataset := &mcmodel.Dataset{}
var req struct {
ProjectID int `json:"project_id"`
}
req.ProjectID = projectID
url := c.BaseURL + fmt.Sprintf("/datasets/%d/publish", datasetID)
request := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{dataset})
if publishAsTestDataset {
request = request.SetQueryParam("test", "true")
}
resp, err := request.Put(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return dataset, nil
}
// UnpublishDataset unpublishes a dataset associated with a specified project and dataset ID,
// returning the updated dataset or an error if the operation fails.
func (c *Client) UnpublishDataset(projectID int, datasetID int) (*mcmodel.Dataset, error) {
dataset := &mcmodel.Dataset{}
var req struct {
ProjectID int `json:"project_id"`
}
req.ProjectID = projectID
url := c.BaseURL + fmt.Sprintf("/datasets/%d/unpublish", datasetID)
resp, err := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{dataset}).
Put(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return dataset, nil
}
// CreateActivity creates a new activity based on the provided CreateActivityRequest struct.
func (c *Client) CreateActivity(req CreateActivityRequest) (*mcmodel.Activity, error) {
activity := &mcmodel.Activity{}
url := c.BaseURL + "/activities"
resp, err := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{activity}).
Post(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return activity, nil
}
// CreateEntity creates a new entity based on the provided request and returns the created entity or an error.
// The category in the request must be either 'experimental' or 'computational'. Defaults to 'experimental'.
func (c *Client) CreateEntity(req CreateEntityRequest) (*mcmodel.Entity, error) {
entity := &mcmodel.Entity{}
if req.Category == "" {
req.Category = "experimental"
}
if req.Category != "experimental" && req.Category != "computational" {
return nil, fmt.Errorf("category must be either 'experimental' or 'computational'")
}
url := c.BaseURL + "/entities"
resp, err := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{entity}).
Post(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return entity, nil
}
// CreateEntityState creates a new entity state associated with the provided project, entity, and activity IDs.
func (c *Client) CreateEntityState(projectID, entityID, activityID int, req CreateEntityStateRequest) (*mcmodel.Entity, error) {
entity := &mcmodel.Entity{}
url := c.BaseURL + fmt.Sprintf("/projects/%d/entities/%d/activities/%d/create-entity-state", projectID, entityID, activityID)
resp, err := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{entity}).
Post(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return entity, nil
}
// GetFileByPath fetches a file from a project given the specified project ID and file path.
// It returns a pointer to the file and an error if the fetch operation fails.
func (c *Client) GetFileByPath(projectID int, path string) (*mcmodel.File, error) {
file := &mcmodel.File{}
req := struct {
ProjectID int `json:"project_id"`
Path string `json:"path"`
}{
ProjectID: projectID,
Path: path,
}
url := c.BaseURL + "/files/by_path"
resp, err := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{file}).
Post(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return file, nil
}
// CreateDirectoryByPath creates a directory at the specified path within the given project. If the
// directory already exists, it returns the existing directory. It takes a project ID and a path as
// parameters and returns the created directory or an error.
func (c *Client) CreateDirectoryByPath(projectID int, path string) (*mcmodel.File, error) {
file := &mcmodel.File{}
req := struct {
ProjectID int `json:"project_id"`
Path string `json:"path"`
}{
ProjectID: projectID,
Path: path,
}
resp, err := c.r().
SetBody(req).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{file}).
Post(c.BaseURL + "/directories/by-path")
if err := checkError(resp, err); err != nil {
return nil, err
}
return file, nil
}
// UploadFileTo uploads a file to a specified project directory path. If the project directory path is
// blank then it uploads the file to the project root. If the directory does not exist, it creates it.
func (c *Client) UploadFileTo(projectID int, filePath string, projectPath string) (*mcmodel.File, error) {
if projectPath == "" {
projectPath = "/"
}
dir, err := c.CreateDirectoryByPath(projectID, projectPath)
if err != nil {
return nil, err
}
return c.UploadFile(projectID, dir.ID, filePath)
}
// UploadFile uploads a file to the specified project and directory.
// Parameters:
// - projectID: ID of the project to which the file will be uploaded.
// - directoryID: ID of the directory within the project where the file will be stored.
// - filePath: Local path of the file to be uploaded.
// Returns:
// - *mcmodel.File: Uploaded file metadata if the operation is successful.
// - error: Describes the error encountered during file upload.
func (c *Client) UploadFile(projectID, directoryID int, filePath string) (*mcmodel.File, error) {
var files [1]mcmodel.File
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer func(f *os.File) {
_ = f.Close()
}(f)
fileName := filepath.Base(filePath)
url := c.BaseURL + fmt.Sprintf("/projects/%d/files/%d/upload", projectID, directoryID)
resp, err := c.r().
SetFileReader("files[]", fileName, f).
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{&files}).
Post(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return &files[0], nil
}
// DepositDataset deposits a dataset in a specified project given the project ID and request details.
// It creates the dataset, uploads files into a unique directory, and sets file selection for the dataset.
func (c *Client) DepositDataset(projectID int, req DepositDatasetRequest) (*mcmodel.Dataset, error) {
// 1. Create the dataset.
createDatasetReq := CreateOrUpdateDatasetRequest{
Name: req.Metadata.Name,
Description: req.Metadata.Description,
Summary: req.Metadata.Summary,
License: req.Metadata.License,
Funding: req.Metadata.Funding,
//Experiments: nil,
//Communities: nil,
Tags: req.Metadata.Tags,
Authors: req.Metadata.Authors,
}
dataset, err := c.CreateDataset(projectID, createDatasetReq)
if err != nil {
return nil, err
}
// 2. Add the additional metadata to the dataset
// 3. Upload the files
// Create a unique directory for the dataset in the project?
// Or should we instead create a project per dataset?
// For now lets create a unique directory. This makes dataset file selection easy.
// The directory will have the dataset UUID as its name. This is kind of a crappy
// solution, but for now let's start with that. We can revise this decision
// later on after discussing with Valentin.
dir, err := c.CreateDirectoryByPath(projectID, "/"+dataset.UUID)
if err != nil {
return nil, err
}
// Now for each of the files we are uploading, we need to append
// the created directory to its path.
for _, file := range req.Files {
fileDir := file.Directory
if fileDir == "" {
fileDir = "/"
} else {
fileDir = fileDir + "/"
}
_, err := c.UploadFileTo(projectID, file.File, dir.Path+file.Directory)
if err != nil {
// For now lets stop all uploads and return an error
return nil, err
}
}
// 4. Set the dataset file selection
fileSelection := DatasetFileSelection{
IncludeFiles: nil,
ExcludeFiles: nil,
IncludeDirs: []string{"/" + dataset.UUID},
ExcludeDirs: nil,
}
dataset, err = c.UpdateDatasetFileSelection(projectID, dataset.ID, fileSelection)
if err != nil {
return nil, err
}
return dataset, nil
}
// ListProjects lists all the projects a user is a member of
func (c *Client) ListProjects() ([]mcmodel.Project, error) {
var projects []mcmodel.Project
url := c.BaseURL + "/projects"
resp, err := c.r().
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{&projects}).
Get(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return projects, nil
}
// ListDatasets lists all the datasets in a project
func (c *Client) ListDatasets(projectID int) ([]mcmodel.Dataset, error) {
var datasets []mcmodel.Dataset
url := c.BaseURL + fmt.Sprintf("/projects/%d/datasets", projectID)
resp, err := c.r().
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{&datasets}).
Get(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return datasets, nil
}
// MintDOIForDataset mints a new (findable) DOI for the dataset and assigns the DOI to it.
func (c *Client) MintDOIForDataset(projectID, datasetID int, publishAsTestDataset bool) (*mcmodel.Dataset, error) {
var dataset mcmodel.Dataset
url := c.BaseURL + fmt.Sprintf("/projects/%d/datasets/%d/assign_doi", projectID, datasetID)
request := c.r().
SetError(&ErrorResponse{}).
SetResult(&DataWrapper{&dataset})
if publishAsTestDataset {
request = request.SetQueryParam("test", "true")
}
resp, err := request.Put(url)
if err := checkError(resp, err); err != nil {
return nil, err
}
return &dataset, nil
}