|
| 1 | +package knock |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/pkg/errors" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +// ProvidersService is an interface for communicating with the Knock |
| 11 | +// Providers API endpoints. |
| 12 | +type ProvidersService interface { |
| 13 | + AuthCheck(context.Context, *ProviderAuthCheckRequest) (*ProviderAuthCheckResponse, error) |
| 14 | + ListChannels(context.Context, *ProviderListChannelsRequest) (*ProviderListChannelsResponse, error) |
| 15 | + RevokeAccess(context.Context, *ProviderRevokeAccessRequest) (bool, error) |
| 16 | +} |
| 17 | + |
| 18 | +type providersService struct { |
| 19 | + client *Client |
| 20 | +} |
| 21 | + |
| 22 | +var _ ProvidersService = &providersService{} |
| 23 | + |
| 24 | +func NewProvidersService(client *Client) *providersService { |
| 25 | + return &providersService{ |
| 26 | + client: client, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Client structs |
| 31 | +type ProviderContext struct { |
| 32 | + // ProviderName is included as a path parameter |
| 33 | + ProviderName string `json:"-"` |
| 34 | + // ChannelId is included as a path parameter |
| 35 | + ChannelId string `json:"-"` |
| 36 | +} |
| 37 | + |
| 38 | +type ProviderAccessTokenObject struct { |
| 39 | + ObjectId string `json:"object_id"` |
| 40 | + Collection string `json:"collection"` |
| 41 | +} |
| 42 | + |
| 43 | +type ProviderAuthCheckRequest struct { |
| 44 | + ProviderContext `json:"-"` |
| 45 | + AccessTokenObject ProviderAccessTokenObject `json:"access_token_object"` |
| 46 | +} |
| 47 | + |
| 48 | +type ProviderAuthCheckResponse struct { |
| 49 | + Ok bool `json:"ok"` |
| 50 | + Url string `json:"url"` |
| 51 | + Team string `json:"team"` |
| 52 | + User string `json:"user"` |
| 53 | + TeamId string `json:"team_id"` |
| 54 | + UserId string `json:"user_id"` |
| 55 | + Error string `json:"error,omitempty"` |
| 56 | +} |
| 57 | + |
| 58 | +type ProviderListChannelsRequest struct { |
| 59 | + ProviderContext `json:"-"` |
| 60 | + AccessTokenObject ProviderAccessTokenObject `json:"access_token_object"` |
| 61 | + // TODO: query_options |
| 62 | +} |
| 63 | + |
| 64 | +type ProviderListChannelsResponse struct { |
| 65 | + // TODO: |
| 66 | +} |
| 67 | + |
| 68 | +type ProviderRevokeAccessRequest struct { |
| 69 | + ProviderContext `json:"-"` |
| 70 | + AccessTokenObject ProviderAccessTokenObject `json:"access_token_object"` |
| 71 | +} |
| 72 | + |
| 73 | +func providersAPIPath(providerName, channelId string) string { |
| 74 | + return fmt.Sprintf("v1/providers/%s/%s", providerName, channelId) |
| 75 | +} |
| 76 | + |
| 77 | +func (ps providersService) AuthCheck(ctx context.Context, request *ProviderAuthCheckRequest) (*ProviderAuthCheckResponse, error) { |
| 78 | + path := providersAPIPath(request.ProviderName, request.ChannelId) |
| 79 | + path = fmt.Sprintf("%s/auth_check", path) |
| 80 | + |
| 81 | + req, err := ps.client.newRequest(http.MethodGet, path, nil, nil) |
| 82 | + if err != nil { |
| 83 | + return nil, errors.Wrap(err, "error creating request for provider auth check") |
| 84 | + } |
| 85 | + |
| 86 | + authCheckResponse := &ProviderAuthCheckResponse{} |
| 87 | + _, err = ps.client.do(ctx, req, authCheckResponse) |
| 88 | + if err != nil { |
| 89 | + return nil, errors.Wrap(err, "error making request for provider auth check") |
| 90 | + } |
| 91 | + |
| 92 | + return authCheckResponse, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (ps providersService) ListChannels(ctx context.Context, request *ProviderListChannelsRequest) (*ProviderListChannelsResponse, error) { |
| 96 | + path := providersAPIPath(request.ProviderName, request.ChannelId) |
| 97 | + path = fmt.Sprintf("%s/channels", path) |
| 98 | + |
| 99 | + //TODO implement me |
| 100 | + panic("implement me") |
| 101 | +} |
| 102 | + |
| 103 | +func (ps providersService) RevokeAccess(ctx context.Context, request *ProviderRevokeAccessRequest) (bool, error) { |
| 104 | + //TODO implement me |
| 105 | + panic("implement me") |
| 106 | +} |
0 commit comments