Skip to content

Commit e3b65a7

Browse files
authored
Merge pull request #26 from libsv/feature/godoc_improve
improve better documentation
2 parents ed7a480 + c2be0d0 commit e3b65a7

File tree

2 files changed

+76
-16
lines changed

2 files changed

+76
-16
lines changed

channel.go

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ func (c *Client) getTokenBaseEndpoint(accountid, channelid string) string {
1616
return fmt.Sprintf("%s/%s/channel/%s/api-token", c.getChanelBaseEndpoint(), accountid, channelid)
1717
}
1818

19-
// ChannelsRequest hold data for get channels request
19+
// ChannelsRequest hold data for get channels request for a particular account
2020
type ChannelsRequest struct {
2121
AccountID string `json:"accountid"`
2222
}
2323

24-
// ChannelsReply hold data for get channels reply
24+
// ChannelsReply hold data for get channels reply. It is a list of channel's detail
2525
type ChannelsReply struct {
2626
Channels []struct {
2727
ID string `json:"id"`
@@ -75,7 +75,10 @@ type ChannelReply struct {
7575
} `json:"access_tokens"`
7676
}
7777

78-
// ChannelUpdateRequest hold data for update channel request
78+
// ChannelUpdateRequest hold data for update channel request.
79+
// The request contains the account and channel identification,
80+
// And the properties values to be updated. These properties defines
81+
// common permission for the channel
7982
type ChannelUpdateRequest struct {
8083
AccountID string `json:"accountid"`
8184
ChannelID string `json:"channelid"`
@@ -85,19 +88,23 @@ type ChannelUpdateRequest struct {
8588
}
8689

8790
// ChannelUpdateReply hold data for update channel reply
91+
// If successful, the reply contains the confirmed updated properties
8892
type ChannelUpdateReply struct {
8993
PublicRead bool `json:"public_read"`
9094
PublicWrite bool `json:"public_write"`
9195
Locked bool `json:"locked"`
9296
}
9397

9498
// ChannelDeleteRequest hold data for delete channel request
99+
// The request contains the account and channel identification
95100
type ChannelDeleteRequest struct {
96101
AccountID string `json:"accountid"`
97102
ChannelID string `json:"channelid"`
98103
}
99104

100105
// ChannelCreateRequest hold data for create channel request
106+
// The request should contain the account id, and optionally some
107+
// channel properties to be initialised.
101108
type ChannelCreateRequest struct {
102109
AccountID string `json:"accountid"`
103110
PublicRead bool `json:"public_read"`
@@ -111,6 +118,9 @@ type ChannelCreateRequest struct {
111118
}
112119

113120
// ChannelCreateReply hold data for create channel reply
121+
// It contains the new channel id, it's properties and the first
122+
// default created token to allow authentification the communication
123+
// on this channel
114124
type ChannelCreateReply struct {
115125
ID string `json:"id"`
116126
Href string `json:"href"`
@@ -134,13 +144,20 @@ type ChannelCreateReply struct {
134144
}
135145

136146
// TokenRequest hold data for get token request
147+
// A token belong to a particular channel, which again belong to a particular account.
148+
// To identify a token, it needs to provide account id, channel id, and token id
137149
type TokenRequest struct {
138150
AccountID string `json:"accountid"`
139151
ChannelID string `json:"channelid"`
140152
TokenID string `json:"tokenid"`
141153
}
142154

143155
// TokenReply hold data for get token reply
156+
// The reply contains
157+
//
158+
// - token id (a number which is uniquely identified in the database)
159+
// - token value, which will be used for authentification to read/write messages on the channel
160+
// - some permission properties attached to the token
144161
type TokenReply struct {
145162
ID string `json:"id"`
146163
Token string `json:"token"`
@@ -150,22 +167,27 @@ type TokenReply struct {
150167
}
151168

152169
// TokenDeleteRequest hold data for delete token request
170+
// A token belong to a particular channel, which again belong to a particular account.
171+
// To identify a token, it needs to provide account id, channel id, and token id
153172
type TokenDeleteRequest struct {
154173
AccountID string `json:"accountid"`
155174
ChannelID string `json:"channelid"`
156175
TokenID string `json:"tokenid"`
157176
}
158177

159178
// TokensRequest hold data for get tokens request
179+
// The request contains the account id and channel id.
160180
type TokensRequest struct {
161181
AccountID string `json:"accountid"`
162182
ChannelID string `json:"channelid"`
163183
}
164184

165-
// TokensReply hold data for get tokens reply
185+
// TokensReply hold data for get tokens reply. It is a list of detail for tokens
166186
type TokensReply []TokenReply
167187

168188
// TokenCreateRequest hold data for create token request
189+
// The request should contains existing account and channel id,
190+
// with optionally some description and permission properties attached to the token
169191
type TokenCreateRequest struct {
170192
AccountID string `json:"accountid"`
171193
ChannelID string `json:"channelid"`
@@ -175,6 +197,7 @@ type TokenCreateRequest struct {
175197
}
176198

177199
// TokenCreateReply hold data for create token reply
200+
// It hold the id and value of the new token, and some of the token's properties
178201
type TokenCreateReply struct {
179202
ID string `json:"id"`
180203
Token string `json:"token"`
@@ -183,7 +206,7 @@ type TokenCreateReply struct {
183206
CanWrite bool `json:"can_write"`
184207
}
185208

186-
// Channels get the list of channels
209+
// Channels get the list of channels with detail for a particular account
187210
func (c *Client) Channels(ctx context.Context, r ChannelsRequest) (*ChannelsReply, error) {
188211
req, err := http.NewRequestWithContext(
189212
ctx,
@@ -203,7 +226,7 @@ func (c *Client) Channels(ctx context.Context, r ChannelsRequest) (*ChannelsRepl
203226
return &res, nil
204227
}
205228

206-
// Channel get the channel
229+
// Channel get single channel's detail for a particular account
207230
func (c *Client) Channel(ctx context.Context, r ChannelRequest) (*ChannelReply, error) {
208231
req, err := http.NewRequestWithContext(
209232
ctx,
@@ -224,7 +247,7 @@ func (c *Client) Channel(ctx context.Context, r ChannelRequest) (*ChannelReply,
224247
return &res, nil
225248
}
226249

227-
// ChannelUpdate update the channel
250+
// ChannelUpdate update the channel's properties.
228251
func (c *Client) ChannelUpdate(ctx context.Context, r ChannelUpdateRequest) (*ChannelUpdateReply, error) {
229252
payload, err := json.Marshal(r)
230253
if err != nil {
@@ -250,7 +273,7 @@ func (c *Client) ChannelUpdate(ctx context.Context, r ChannelUpdateRequest) (*Ch
250273
return &res, nil
251274
}
252275

253-
// ChannelDelete delete the channel
276+
// ChannelDelete delete the channel a particular channel. It return nothing, which is a 204 http code (No Content)
254277
func (c *Client) ChannelDelete(ctx context.Context, r ChannelDeleteRequest) error {
255278
req, err := http.NewRequestWithContext(
256279
ctx,
@@ -266,7 +289,7 @@ func (c *Client) ChannelDelete(ctx context.Context, r ChannelDeleteRequest) erro
266289
return c.sendRequest(req, nil)
267290
}
268291

269-
// ChannelCreate create a channel
292+
// ChannelCreate create a new channel for a particular account
270293
func (c *Client) ChannelCreate(ctx context.Context, r ChannelCreateRequest) (*ChannelCreateReply, error) {
271294
payload, err := json.Marshal(r)
272295
if err != nil {
@@ -292,7 +315,7 @@ func (c *Client) ChannelCreate(ctx context.Context, r ChannelCreateRequest) (*Ch
292315
return &res, nil
293316
}
294317

295-
// Token get the token
318+
// Token get the token's detail.
296319
func (c *Client) Token(ctx context.Context, r TokenRequest) (*TokenReply, error) {
297320
req, err := http.NewRequestWithContext(
298321
ctx,
@@ -313,7 +336,13 @@ func (c *Client) Token(ctx context.Context, r TokenRequest) (*TokenReply, error)
313336
return &res, nil
314337
}
315338

316-
// TokenDelete delate the token
339+
// TokenDelete delete a particular token
340+
//
341+
// It is typically used when an admin create a temporary
342+
// communication capability for a particular user on a channel,
343+
//
344+
// After the user has finished he usage of the channel,
345+
// the admin then delete the token
317346
func (c *Client) TokenDelete(ctx context.Context, r TokenDeleteRequest) error {
318347
req, err := http.NewRequestWithContext(ctx,
319348
http.MethodDelete,
@@ -328,7 +357,8 @@ func (c *Client) TokenDelete(ctx context.Context, r TokenDeleteRequest) error {
328357
return c.sendRequest(req, nil)
329358
}
330359

331-
// Tokens get the list of tokens
360+
// Tokens get the list of tokens. It return a full list of tokens
361+
// for a particular account id and channel id
332362
func (c *Client) Tokens(ctx context.Context, r TokensRequest) (*TokensReply, error) {
333363
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.getTokenBaseEndpoint(r.AccountID, r.ChannelID), nil)
334364
if err != nil {
@@ -343,7 +373,13 @@ func (c *Client) Tokens(ctx context.Context, r TokensRequest) (*TokensReply, err
343373
return &res, nil
344374
}
345375

346-
// TokenCreate create a token
376+
// TokenCreate create a new token for a particular account and channel
377+
//
378+
// It is typically used when and admin/orchestrator need to create
379+
// a communication channel for a group of 2 or more peers.
380+
//
381+
// He then create a channel and a list of token, then give the tokens
382+
// to each peers so they can communicate through the channel
347383
func (c *Client) TokenCreate(ctx context.Context, r TokenCreateRequest) (*TokenCreateReply, error) {
348384
payload, err := json.Marshal(r)
349385
if err != nil {

message.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func (c *Client) getMessageBaseEndpoint() string {
1212
}
1313

1414
// MessageHeadRequest hold data for HEAD message request
15+
// It request the max sequence for a particular channel
1516
type MessageHeadRequest struct {
1617
ChannelID string `json:"channelid"`
1718
}
@@ -23,6 +24,9 @@ type MessageWriteRequest struct {
2324
}
2425

2526
// MessageWriteReply hold data for write message reply
27+
// It contains the id of the message in the database,
28+
// the received timestamp, the content type, and the
29+
// base64 encoding of the message content
2630
type MessageWriteReply struct {
2731
Sequence int64 `json:"sequence"`
2832
Received string `json:"received"`
@@ -40,6 +44,9 @@ type MessagesRequest struct {
4044
type MessagesReply []MessageWriteReply
4145

4246
// MessageMarkRequest hold data for mark message request
47+
//
48+
// A particular message is identified by its sequence number
49+
// and the channel id in which it belong to
4350
type MessageMarkRequest struct {
4451
ChannelID string `json:"channelid"`
4552
Sequence int64 `json:"sequence"`
@@ -48,12 +55,17 @@ type MessageMarkRequest struct {
4855
}
4956

5057
// MessageDeleteRequest hold data for delete message request
58+
// A particular message is identified by its sequence number
59+
// and the channel id in which it belong to
5160
type MessageDeleteRequest struct {
5261
ChannelID string `json:"channelid"`
5362
Sequence int64 `json:"sequence"`
5463
}
5564

56-
// MessageHead send HEAD message request
65+
// MessageHead send HEAD message request. It request the max sequence for a particular channel
66+
//
67+
// The request should use bearer token authentification method.
68+
// The token is provided by the TokenCreate endpoint
5769
func (c *Client) MessageHead(ctx context.Context, r MessageHeadRequest) error {
5870
req, err := http.NewRequestWithContext(
5971
ctx,
@@ -68,7 +80,10 @@ func (c *Client) MessageHead(ctx context.Context, r MessageHeadRequest) error {
6880
return c.sendRequest(req, nil)
6981
}
7082

71-
// MessageWrite write a message
83+
// MessageWrite write a message to a particular channel
84+
//
85+
// The request should use bearer token authentification method.
86+
// The token is provided by the TokenCreate endpoint
7287
func (c *Client) MessageWrite(ctx context.Context, r MessageWriteRequest) (*MessageWriteReply, error) {
7388
req, err := http.NewRequestWithContext(
7489
ctx,
@@ -88,7 +103,10 @@ func (c *Client) MessageWrite(ctx context.Context, r MessageWriteRequest) (*Mess
88103
return &res, nil
89104
}
90105

91-
// Messages get messages list
106+
// Messages get messages list. It can query read/unread messages.
107+
//
108+
// The request should use bearer token authentification method.
109+
// The token is provided by the TokenCreate endpoint
92110
func (c *Client) Messages(ctx context.Context, r MessagesRequest) (MessagesReply, error) {
93111
req, err := http.NewRequestWithContext(
94112
ctx,
@@ -114,6 +132,9 @@ func (c *Client) Messages(ctx context.Context, r MessagesRequest) (MessagesReply
114132
}
115133

116134
// MessageMark mark a message
135+
//
136+
// The request should use bearer token authentification method.
137+
// The token is provided by the TokenCreate endpoint
117138
func (c *Client) MessageMark(ctx context.Context, r MessageMarkRequest) error {
118139
payloadStr := fmt.Sprintf("{\"read\":%t}", r.Read)
119140
channelURL := fmt.Sprintf("%s/channel/%s", c.getMessageBaseEndpoint(), r.ChannelID)
@@ -135,6 +156,9 @@ func (c *Client) MessageMark(ctx context.Context, r MessageMarkRequest) error {
135156
}
136157

137158
// MessageDelete delete a message
159+
//
160+
// The request should use bearer token authentification method.
161+
// The token is provided by the TokenCreate endpoint
138162
func (c *Client) MessageDelete(ctx context.Context, r MessageDeleteRequest) error {
139163
channelURL := fmt.Sprintf("%s/channel/%s", c.getMessageBaseEndpoint(), r.ChannelID)
140164
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("%s/%v", channelURL, r.Sequence), nil)

0 commit comments

Comments
 (0)