-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauth.go
More file actions
144 lines (127 loc) · 4.51 KB
/
auth.go
File metadata and controls
144 lines (127 loc) · 4.51 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package logchimp
import (
"context"
"net/http"
"slices"
"github.com/logchimp/logchimp-go/internal/apijson"
"github.com/logchimp/logchimp-go/internal/requestconfig"
"github.com/logchimp/logchimp-go/option"
"github.com/logchimp/logchimp-go/packages/param"
"github.com/logchimp/logchimp-go/packages/respjson"
)
// AuthService contains methods and other services that help with interacting with
// the logchimp API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewAuthService] method instead.
type AuthService struct {
Options []option.RequestOption
Setup AuthSetupService
Email AuthEmailService
}
// NewAuthService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewAuthService(opts ...option.RequestOption) (r AuthService) {
r = AuthService{}
r.Options = opts
r.Setup = NewAuthSetupService(opts...)
r.Email = NewAuthEmailService(opts...)
return
}
// Login to account using email and password.
func (r *AuthService) Login(ctx context.Context, body AuthLoginParams, opts ...option.RequestOption) (res *AuthLoginResponse, err error) {
opts = slices.Concat(r.Options, opts)
path := "auth/login"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Create a new user account.
func (r *AuthService) Signup(ctx context.Context, body AuthSignupParams, opts ...option.RequestOption) (res *AuthSignupResponse, err error) {
opts = slices.Concat(r.Options, opts)
path := "auth/signup"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
type AuthUser struct {
AuthToken string `json:"authToken"`
// URL of the user's avatar
Avatar string `json:"avatar" format:"url"`
// Email address of the user
Email string `json:"email" format:"email"`
// User's full name
Name string `json:"name"`
UserID string `json:"userId"`
// User's username
Username string `json:"username"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
AuthToken respjson.Field
Avatar respjson.Field
Email respjson.Field
Name respjson.Field
UserID respjson.Field
Username respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r AuthUser) RawJSON() string { return r.JSON.raw }
func (r *AuthUser) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type AuthLoginResponse struct {
User AuthUser `json:"user"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
User respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r AuthLoginResponse) RawJSON() string { return r.JSON.raw }
func (r *AuthLoginResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type AuthSignupResponse struct {
User AuthUser `json:"user"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
User respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r AuthSignupResponse) RawJSON() string { return r.JSON.raw }
func (r *AuthSignupResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type AuthLoginParams struct {
Email string `json:"email,required" format:"email"`
Password string `json:"password,required" format:"password"`
paramObj
}
func (r AuthLoginParams) MarshalJSON() (data []byte, err error) {
type shadow AuthLoginParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *AuthLoginParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type AuthSignupParams struct {
Email string `json:"email,required" format:"email"`
Password string `json:"password,required" format:"password"`
paramObj
}
func (r AuthSignupParams) MarshalJSON() (data []byte, err error) {
type shadow AuthSignupParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *AuthSignupParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}