-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
45 lines (37 loc) · 1.26 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package services
import (
"context"
"fmt"
"github.com/Khan/genqlient/graphql"
"github.com/raito-io/sdk/internal/schema"
"github.com/raito-io/sdk/types"
)
type UserClient struct {
client graphql.Client
}
func NewUserClient(client graphql.Client) UserClient {
return UserClient{
client: client,
}
}
func (c *UserClient) GetUserByEmail(ctx context.Context, email string) (*types.User, error) {
result, err := schema.GetUserByEmail(ctx, c.client, email)
if err != nil {
return nil, types.NewErrClient(err)
}
if result.UserByEmail == nil {
return nil, types.NewErrNotFound(email, "user", "No user found for the given email address.")
}
switch user := (*result.UserByEmail).(type) {
case *schema.GetUserByEmailUserByEmailUser:
return &user.User, nil
case *schema.GetUserByEmailUserByEmailInvalidEmailError:
return nil, types.NewErrClient(fmt.Errorf("invalid email address %q: %s", user.ErrEmail, user.Message))
case *schema.GetUserByEmailUserByEmailNotFoundError:
return nil, types.NewErrNotFound(email, "user", user.Message)
case *schema.GetUserByEmailUserByEmailPermissionDeniedError:
return nil, types.NewErrPermissionDenied("getUserByEmail", user.Message)
default:
return nil, types.NewErrClient(fmt.Errorf("unexpected result type: %T", user))
}
}