forked from dnanexus/dxfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dx_find.go
59 lines (51 loc) · 1.18 KB
/
dx_find.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
package dxfuse
import (
"context"
"encoding/json"
"fmt"
"github.com/dnanexus/dxda"
)
type FindProjectRequest struct {
Name string `json:"name"`
Level string `json:"level"`
}
type FindResult struct {
Id string `json:"id"`
}
type FindProjectReply struct {
Results []FindResult `json:"results"`
}
// Find the project-id for a project name. Return nil if
// the project does not exist
func DxFindProject(
ctx context.Context,
dxEnv *dxda.DXEnvironment,
projName string) (string, error) {
request := FindProjectRequest{
Name : projName,
Level : "VIEW",
}
var payload []byte
payload, err := json.Marshal(request)
if err != nil {
return "", err
}
httpClient := dxda.NewHttpClient(false)
repJs, err := dxda.DxAPI(ctx, httpClient, NumRetriesDefault, dxEnv, "system/findProjects", string(payload))
if err != nil {
return "", err
}
var reply FindProjectReply
if err = json.Unmarshal(repJs, &reply); err != nil {
return "", err
}
if len(reply.Results) == 0 {
// project not found
return "", nil
} else if len(reply.Results) == 1 {
return reply.Results[0].Id, nil
} else {
err := fmt.Errorf("Found more than one project with the name %s", projName)
return "", err
}
}