|
| 1 | +package da |
| 2 | + |
| 3 | +import "github.com/apprentice3d/forge-api-go-client/oauth" |
| 4 | + |
| 5 | +// API struct holds all paths necessary to access Design Automation API |
| 6 | +type API struct { |
| 7 | + oauth.TwoLeggedAuth |
| 8 | + DesignAutomationPath string |
| 9 | +} |
| 10 | + |
| 11 | +// NewAPIWithCredentials returns a DesignAutomation API client with default configurations |
| 12 | +func NewAPIWithCredentials(ClientID string, ClientSecret string) API { |
| 13 | + return API{ |
| 14 | + oauth.NewTwoLeggedClient(ClientID, ClientSecret), |
| 15 | + "/da/us-east/v3", |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +// UserId gives you the id used to identify the user |
| 21 | +func (api API) UserId() (nickname string, err error) { |
| 22 | + bearer, err := api.Authenticate("code:all") |
| 23 | + if err != nil { |
| 24 | + return |
| 25 | + } |
| 26 | + path := api.Host + api.DesignAutomationPath |
| 27 | + nickname, err = getUserID(path, bearer.AccessToken) |
| 28 | + |
| 29 | + return |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +// EngineList lists all available Engines. |
| 35 | +func (api API) EngineList() (list EngineList, err error) { |
| 36 | + |
| 37 | + bearer, err := api.Authenticate("code:all") |
| 38 | + if err != nil { |
| 39 | + return |
| 40 | + } |
| 41 | + path := api.Host + api.DesignAutomationPath |
| 42 | + list, err = listEngines(path, bearer.AccessToken) |
| 43 | + |
| 44 | + return |
| 45 | +} |
| 46 | + |
| 47 | +// EngineDetails gives details on an engine providing it's id. |
| 48 | +func (api API) EngineDetails(id string) (list EngineDetails, err error) { |
| 49 | + |
| 50 | + bearer, err := api.Authenticate("code:all") |
| 51 | + if err != nil { |
| 52 | + return |
| 53 | + } |
| 54 | + path := api.Host + api.DesignAutomationPath |
| 55 | + list, err = getEngineDetails(path, id, bearer.AccessToken) |
| 56 | + |
| 57 | + return |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +// CreateApp creates an app with given name and using specified engine |
| 62 | +// name - should be unique and will be the appID |
| 63 | +// engine - engineId to be used by this app (check EngineList) |
| 64 | +func (api API) CreateApp(name, engine string) (app AppBundle, err error) { |
| 65 | + |
| 66 | + bearer, err := api.Authenticate("code:all") |
| 67 | + if err != nil { |
| 68 | + return |
| 69 | + } |
| 70 | + path := api.Host + api.DesignAutomationPath |
| 71 | + app, err = createApp(path, name, engine, bearer.AccessToken) |
| 72 | + |
| 73 | + app.authenticator = &api.TwoLeggedAuth |
| 74 | + app.path = path |
| 75 | + app.name = name |
| 76 | + |
| 77 | + //WARNING: when an AppBundle is created, it is assigned an '$LATEST' alias |
| 78 | + // but this alias is not usable and if no other alias is created for this |
| 79 | + // appBundle, then the alias listing will fail. |
| 80 | + // Thus I decided to autoasign a "default" alias upon app creation |
| 81 | + go app.CreateAlias("default", 1) |
| 82 | + |
| 83 | + return |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +// AppList lists all available appbundles. |
| 90 | +func (api API) AppList() (list AppList, err error) { |
| 91 | + |
| 92 | + bearer, err := api.Authenticate("code:all") |
| 93 | + if err != nil { |
| 94 | + return |
| 95 | + } |
| 96 | + path := api.Host + api.DesignAutomationPath |
| 97 | + list, err = listApps(path, bearer.AccessToken) |
| 98 | + |
| 99 | + return |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +// AppDelete will delete the app with specified id |
| 104 | +//func (api API) AppDelete(id string) (err error) { |
| 105 | +// |
| 106 | +// bearer, err := api.Authenticate("code:all") |
| 107 | +// if err != nil { |
| 108 | +// return |
| 109 | +// } |
| 110 | +// path := api.Host + api.DesignAutomationPath |
| 111 | +// err = deleteApp(path, id, bearer.AccessToken) |
| 112 | +// |
| 113 | +// return |
| 114 | +//} |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
0 commit comments