|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + oauthapi "github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/tools" |
| 6 | +) |
| 7 | + |
| 8 | +type OAuthApiClient interface { |
| 9 | + // DeleteOAuthApp Unauthorizes an app. |
| 10 | + DeleteOAuthApp(ctx context.Context, app string) error |
| 11 | + // DeleteOAuthAppTool Remove a tool from an app. |
| 12 | + DeleteOAuthAppTool(ctx context.Context, app, tool string) error |
| 13 | + // GetOAuthApp Returns an app object. |
| 14 | + GetOAuthApp(ctx context.Context, app string) (oauthapi.OAuthApp, error) |
| 15 | + // ListOAuthApps Lists all app objects. |
| 16 | + ListOAuthApps(ctx context.Context) ([]oauthapi.OAuthApp, error) |
| 17 | + // PostOAuthApp Authorize an app. |
| 18 | + PostOAuthApp(ctx context.Context, app, scopes string) (oauthapi.AuthResponse, error) |
| 19 | + // PostOAuthAppTool Add a tool to an app. |
| 20 | + PostOAuthAppTool(ctx context.Context, app, tool string) error |
| 21 | +} |
| 22 | + |
| 23 | +var _ OAuthApiClient = &oauthApiClientImpl{} |
| 24 | + |
| 25 | +type oauthApiClientImpl struct { |
| 26 | + *oauthapi.APIClient |
| 27 | +} |
| 28 | + |
| 29 | +func NewOAuthApiClient(socketPath string) OAuthApiClient { |
| 30 | + return &oauthApiClientImpl{ |
| 31 | + APIClient: oauthapi.NewAPIClient(GetOAuthConfiguration(socketPath)), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (o oauthApiClientImpl) DeleteOAuthApp(ctx context.Context, app string) error { |
| 36 | + apiReq := o.ToolsApi.DeleteOAuthApp(ctx, app) |
| 37 | + _, err := apiReq.Execute() |
| 38 | + return err |
| 39 | +} |
| 40 | + |
| 41 | +func (o oauthApiClientImpl) DeleteOAuthAppTool(ctx context.Context, app, tool string) error { |
| 42 | + apiReq := o.ToolsApi.DeleteOAuthAppTool(ctx, app, tool) |
| 43 | + _, err := apiReq.Execute() |
| 44 | + return err |
| 45 | +} |
| 46 | + |
| 47 | +func (o oauthApiClientImpl) GetOAuthApp(ctx context.Context, app string) (oauthapi.OAuthApp, error) { |
| 48 | + apiReq := o.ToolsApi.GetOAuthApp(ctx, app) |
| 49 | + res, _, err := apiReq.Execute() |
| 50 | + if err != nil { |
| 51 | + return oauthapi.OAuthApp{}, err |
| 52 | + } |
| 53 | + return *res, nil |
| 54 | +} |
| 55 | + |
| 56 | +func (o oauthApiClientImpl) ListOAuthApps(ctx context.Context) ([]oauthapi.OAuthApp, error) { |
| 57 | + apiReq := o.ToolsApi.ListOAuthApps(ctx) |
| 58 | + res, _, err := apiReq.Execute() |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + return res, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (o oauthApiClientImpl) PostOAuthApp(ctx context.Context, app, scopes string) (oauthapi.AuthResponse, error) { |
| 66 | + apiReq := o.ToolsApi.PostOAuthApp(ctx, app) |
| 67 | + res, _, err := apiReq.Scopes(scopes).Execute() |
| 68 | + if err != nil { |
| 69 | + return oauthapi.AuthResponse{}, err |
| 70 | + } |
| 71 | + return *res, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (o oauthApiClientImpl) PostOAuthAppTool(ctx context.Context, app, tool string) error { |
| 75 | + apiReq := o.ToolsApi.PostOAuthAppTool(ctx, app, tool) |
| 76 | + _, err := apiReq.Execute() |
| 77 | + return err |
| 78 | +} |
| 79 | + |
| 80 | +type OAuthApp struct { |
| 81 | + App string `json:"app"` |
| 82 | + Authorized bool `json:"authorized"` |
| 83 | + Provider string `json:"provider"` |
| 84 | + Scopes []OAuthScopes `json:"scopes,omitempty"` |
| 85 | + Tools []string `json:"tools"` |
| 86 | +} |
| 87 | + |
| 88 | +type OAuthScopes struct { |
| 89 | + Description string `json:"description,omitempty"` |
| 90 | + Metadata []string `json:"metadata,omitempty"` |
| 91 | + Name string `json:"name,omitempty"` |
| 92 | +} |
| 93 | + |
| 94 | +type AuthResponse struct { |
| 95 | + AuthType string `json:"authType,omitempty"` |
| 96 | + BrowserUrl string `json:"browserUrl,omitempty"` |
| 97 | +} |
0 commit comments