Skip to content

Commit f26e846

Browse files
committed
initial setup
1 parent 99989d6 commit f26e846

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

knock/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Client struct {
4444
Tenants TenantsService
4545
Users UsersService
4646
Workflows WorkflowsService
47+
Providers ProvidersService
4748
}
4849

4950
// ClientOption provides a variadic option for configuring the client
@@ -117,6 +118,7 @@ func NewClient(opts ...ClientOption) (*Client, error) {
117118
c.Tenants = &tenantsService{client: c}
118119
c.Users = &usersService{client: c}
119120
c.Workflows = &workflowsService{client: c}
121+
c.Providers = &providersService{client: c}
120122

121123
return c, nil
122124
}

knock/providers.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)