Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get hubs, projects, directories and fileinfoes #13

Open
wants to merge 8 commits into
base: feature/data_management
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea
.idea/
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries
Expand Down Expand Up @@ -65,3 +67,5 @@ crashlytics.properties
crashlytics-build.properties
fabric.properties
.idea/

*.cmd
2 changes: 1 addition & 1 deletion dm/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"strconv"

"github.com/apprentice3d/forge-api-go-client/oauth"
"forge-api-go-client/oauth"
)

// BucketAPI holds the necessary data for making Bucket related calls to Forge Data Management service
Expand Down
2 changes: 1 addition & 1 deletion dm/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dm_test

import (
"fmt"
"github.com/apprentice3d/forge-api-go-client/dm"
"forge-api-go-client/dm"
"log"
"os"
"testing"
Expand Down
249 changes: 203 additions & 46 deletions dm/hubs.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,204 @@
package dm
//
//type Hubs struct {
// Data []Content `json:"data,omitempty"`
// JsonApi JsonAPI `json:"jsonapi,omitempty"`
// Links Link `json:"links,omitempty"`
//}
//
//type JsonAPI struct {
// Version string `json:"version,omitempty"`
//}
//
//type Link struct {
// Self struct {
// Href string `json:"href,omitempty"`
// } `json:"self,omitempty"`
//}
//
//type Content struct {
// Relationships struct {
// Projects Project `json:"projects,omitempty"`
// } `json:"relationships,omitempty"`
// Attributes Attribute `json:"attributes,omitempty"`
// Type string `json:"type,omitempty"`
// Id string `json:"id,omitempty"`
// Links Link `json:"links,omitempty"`
//}
//
//type Project struct {
// Links struct {
// Related struct {
// Href string `json:"href,omitempty"`
// } `json:"related,omitempty"`
// } `json:"links,omitempty"`
//}
//
//type Attribute struct {
// Name string `json:"name,omitempty"`
// Extension struct {
// Data map[string]interface{} `json:"data,omitempty"`
// Version string `json:"version,omitempty"`
// Type string `json:"type,omitempty"`
// Schema struct {
// Href string `json:"href,omitempty"`
// } `json:"schema,omitempty"`
// } `json:"extension,omitempty"`
//}

import (
"encoding/json"
"errors"
"fmt"
"forge-api-go-client/oauth"
"io/ioutil"
"net/http"
"strconv"
)

type HubsAPI struct {
oauth.TwoLeggedAuth
HubsAPIPath string
}

func NewHubsAPIWithCredentials(ClientID, ClientSecret string) HubsAPI {
return HubsAPI{
TwoLeggedAuth: oauth.NewTwoLeggedClient(ClientID, ClientSecret),
HubsAPIPath: "/project/v1/hubs",
}
}

type Hubs struct {
Data []Content `json:"data,omitempty"`
JsonApi JsonAPI `json:"jsonapi,omitempty"`
Links Link `json:"links,omitempty"`
}

type JsonAPI struct {
Version string `json:"version,omitempty"`
}

type Link struct {
Self struct {
Href string `json:"href,omitempty"`
} `json:"self,omitempty"`
}

type Content struct {
Relationships struct {
Projects Project `json:"projects,omitempty"`
} `json:"relationships,omitempty"`
Attributes Attribute `json:"attributes,omitempty"`
Type string `json:"type,omitempty"`
Id string `json:"id,omitempty"`
Links Link `json:"links,omitempty"`
}

type Project struct {
Links struct {
Related struct {
Href string `json:"href,omitempty"`
} `json:"related,omitempty"`
} `json:"links,omitempty"`
}

type Attribute struct {
Name string `json:"name,omitempty"`
Extension struct {
Data map[string]interface{} `json:"data,omitempty"`
Version string `json:"version,omitempty"`
Type string `json:"type,omitempty"`
Schema struct {
Href string `json:"href,omitempty"`
} `json:"schema,omitempty"`
} `json:"extension,omitempty"`
}

type Projects struct {
Jsonapi struct {
Version string `json:"version,omitempty"`
} `json:"jsonapi,omitempty"`
Links struct {
Self struct {
Href string `json:"href,omitempty"`
} `json:"self,omitempty"`
} `json:"links,omitempty"`
Data []struct {
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
Attributes struct {
Name string `json:"name,omitempty"`
Extension struct {
Type string `json:"type,omitempty"`
Version string `json:"version,omitempty"`
Schema struct {
Href string `json:"href,omitempty"`
} `json:"schema,omitempty"`
Data struct {
} `json:"data,omitempty"`
} `json:"extension,omitempty"`
} `json:"attributes,omitempty"`
Links struct {
Self struct {
Href string `json:"href,omitempty"`
} `json:"self,omitempty"`
} `json:"links,omitempty"`
Relationships struct {
Hub struct {
Data struct {
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
} `json:"data,omitempty"`
Links struct {
Related struct {
Href string `json:"href,omitempty"`
} `json:"related,omitempty"`
} `json:"links,omitempty"`
} `json:"hub,omitempty"`
RootFolder struct {
Data struct {
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
} `json:"data,omitempty"`
Meta struct {
Link struct {
Href string `json:"href,omitempty"`
} `json:"link,omitempty"`
} `json:"meta,omitempty"`
} `json:"rootFolder,omitempty"`
} `json:"relationships,omitempty"`
} `json:"data,omitempty"`
}


func (api *HubsAPI) GetHubs() (result Hubs, err error){
bearer, err := api.Authenticate("data:read")
if err != nil {
return Hubs{}, err
}

path := api.Host + api.HubsAPIPath
result, err = listHubs(path, bearer.AccessToken)
return result, err
}

func listHubs(path string, token string) (result Hubs, err error) {
task := http.Client{}

req, err := http.NewRequest("GET", path, nil)
if err != nil {
return Hubs{}, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)

response, err := task.Do(req)
if err != nil {
return Hubs{}, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
content, _ := ioutil.ReadAll(response.Body)
err = errors.New("[" + strconv.Itoa(response.StatusCode) + "] " + string(content))
return Hubs{}, err
}

decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&result)
return result, nil
}

func (api *HubsAPI) GetHubProjects(hubId string) (result Projects, err error){
bearer, err := api.Authenticate("data:read")
if err != nil {
return Projects{}, err
}

path := fmt.Sprintf("%s/%s/%s/projects", api.Host, api.HubsAPIPath, hubId)
result, err = getHubProjects(path, bearer.AccessToken)
return result, err
}

func getHubProjects(path string, token string) (result Projects, err error) {
task := http.Client{}

req, err := http.NewRequest("GET", path, nil)
if err != nil {
return Projects{}, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)

response, err := task.Do(req)
if err != nil {
return Projects{}, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
content, _ := ioutil.ReadAll(response.Body)
err = errors.New("[" + strconv.Itoa(response.StatusCode) + "] " + string(content))
return Projects{}, err
}

decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&result)
return result, nil
}
37 changes: 37 additions & 0 deletions dm/hubs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dm_test

import (
"fmt"
"forge-api-go-client/dm"
"os"
"testing"
)

func TestHubsAPI_ListHubs(t *testing.T) {
clientID := os.Getenv("FORGE_CLIENT_ID")
clientSecret := os.Getenv("FORGE_CLIENT_SECRET")

fmt.Printf("Using envs: %s\n%s\n", clientID, clientSecret)

hubsAPI := dm.NewHubsAPIWithCredentials(clientID, clientSecret)

t.Run("List Hubs", func(t *testing.T) {
hubs, err := hubsAPI.GetHubs()
if err!=nil{
t.Fatalf("Failed to list hubs: %s\n", err.Error())
}

if len(hubs.Data) == 0 {
t.Fatalf("Failed to list hubs. No hubs retreived.")
}

projects, err := hubsAPI.GetHubProjects(hubs.Data[0].Id)
if err!=nil{
t.Fatalf("Failed to list hub '%s' projects: %s\n", hubs.Data[0].Id, err.Error())
}

if len(projects.Data) == 0 {
t.Fatalf("Failed to list hub projects. No projects retreived or failed to unmarshal.")
}
})
}
3 changes: 1 addition & 2 deletions dm/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"io/ioutil"
"os"
"testing"

"github.com/apprentice3d/forge-api-go-client/dm"
"forge-api-go-client/dm"
)

func TestBucketAPI_ListObjects(t *testing.T) {
Expand Down
Loading