-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement Following endpoints (#84)
Closes #10
- Loading branch information
Showing
22 changed files
with
756 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package following | ||
|
||
import ( | ||
"github.com/yitsushi/go-misskey/core" | ||
"github.com/yitsushi/go-misskey/models" | ||
) | ||
|
||
// CreateRequest is the request structure to create a following. | ||
type CreateRequest struct { | ||
UserID string `json:"userId"` | ||
} | ||
|
||
// Validate request. | ||
func (r CreateRequest) Validate() error { | ||
if r.UserID == "" { | ||
return core.RequestValidationError{ | ||
Request: r, | ||
Message: core.UndefinedRequiredField, | ||
Field: "UserID", | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Create following endpoint. | ||
func (s *Service) Create(userID string) (models.User, error) { | ||
var response models.User | ||
|
||
request := &CreateRequest{UserID: userID} | ||
|
||
err := s.Call( | ||
&core.JSONRequest{Request: request, Path: "/following/create"}, | ||
&response, | ||
) | ||
|
||
return response, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package following_test | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/yitsushi/go-misskey" | ||
"github.com/yitsushi/go-misskey/core" | ||
"github.com/yitsushi/go-misskey/services/following" | ||
"github.com/yitsushi/go-misskey/test" | ||
) | ||
|
||
func TestService_Create(t *testing.T) { | ||
client := test.MakeMockClient(test.SimpleMockOptions{ | ||
Endpoint: "/api/following/create", | ||
RequestData: &following.CreateRequest{}, | ||
ResponseFile: "user.json", | ||
StatusCode: http.StatusOK, | ||
}) | ||
|
||
user, err := client.Following().Create("88v9vu5nbu") | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, "88v9vu5nbu", user.ID) | ||
} | ||
|
||
func TestCreateRequest_Validate(t *testing.T) { | ||
test.ValidateRequests( | ||
t, | ||
[]core.BaseRequest{ | ||
following.CreateRequest{}, | ||
following.CreateRequest{UserID: ""}, | ||
}, | ||
[]core.BaseRequest{ | ||
following.CreateRequest{UserID: "88v9vu5nbu"}, | ||
}, | ||
) | ||
} | ||
|
||
func ExampleService_Create() { | ||
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN")) | ||
|
||
user, err := client.Following().Create("88v9vu5nbu") | ||
if err != nil { | ||
log.Printf("[Following/Create] %s", err) | ||
|
||
return | ||
} | ||
|
||
log.Printf("[Following/Create] %s", user.Username) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package following | ||
|
||
import ( | ||
"github.com/yitsushi/go-misskey/core" | ||
"github.com/yitsushi/go-misskey/models" | ||
) | ||
|
||
// DeleteRequest is the request structure to delete a following. | ||
type DeleteRequest struct { | ||
UserID string `json:"userId"` | ||
} | ||
|
||
// Validate request. | ||
func (r DeleteRequest) Validate() error { | ||
if r.UserID == "" { | ||
return core.RequestValidationError{ | ||
Request: r, | ||
Message: core.UndefinedRequiredField, | ||
Field: "UserID", | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Delete following endpoint. | ||
func (s *Service) Delete(userID string) (models.User, error) { | ||
var response models.User | ||
|
||
request := &DeleteRequest{UserID: userID} | ||
|
||
err := s.Call( | ||
&core.JSONRequest{Request: request, Path: "/following/delete"}, | ||
&response, | ||
) | ||
|
||
return response, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package following_test | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/yitsushi/go-misskey" | ||
"github.com/yitsushi/go-misskey/core" | ||
"github.com/yitsushi/go-misskey/services/following" | ||
"github.com/yitsushi/go-misskey/test" | ||
) | ||
|
||
func TestService_Delete(t *testing.T) { | ||
client := test.MakeMockClient(test.SimpleMockOptions{ | ||
Endpoint: "/api/following/delete", | ||
RequestData: &following.DeleteRequest{}, | ||
ResponseFile: "user.json", | ||
StatusCode: http.StatusOK, | ||
}) | ||
|
||
user, err := client.Following().Delete("88v9vu5nbu") | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, "88v9vu5nbu", user.ID) | ||
} | ||
|
||
func TestDeleteRequest_Validate(t *testing.T) { | ||
test.ValidateRequests( | ||
t, | ||
[]core.BaseRequest{ | ||
following.DeleteRequest{}, | ||
following.DeleteRequest{UserID: ""}, | ||
}, | ||
[]core.BaseRequest{ | ||
following.DeleteRequest{UserID: "88v9vu5nbu"}, | ||
}, | ||
) | ||
} | ||
|
||
func ExampleService_Delete() { | ||
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN")) | ||
|
||
user, err := client.Following().Delete("88v9vu5nbu") | ||
if err != nil { | ||
log.Printf("[Following/Delete] %s", err) | ||
|
||
return | ||
} | ||
|
||
log.Printf("[Following/Delete] %s", user.Username) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"id": "88v9vu5nbu", | ||
"name": null, | ||
"username": "kiki_test", | ||
"host": null, | ||
"avatarUrl": "https://slippy.xyz/identicon/88v9vu5nbu", | ||
"avatarBlurhash": null, | ||
"avatarColor": null, | ||
"isBot": true, | ||
"emojis": [], | ||
"onlineStatus": "unknown" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package following | ||
|
||
import ( | ||
"github.com/yitsushi/go-misskey/core" | ||
"github.com/yitsushi/go-misskey/models" | ||
) | ||
|
||
// InvalidateRequest is the request structure to invalidate a following. | ||
type InvalidateRequest struct { | ||
UserID string `json:"userId"` | ||
} | ||
|
||
// Validate request. | ||
func (r InvalidateRequest) Validate() error { | ||
if r.UserID == "" { | ||
return core.RequestValidationError{ | ||
Request: r, | ||
Message: core.UndefinedRequiredField, | ||
Field: "UserID", | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Invalidate following endpoint. | ||
func (s *Service) Invalidate(userID string) (models.User, error) { | ||
var response models.User | ||
|
||
request := &InvalidateRequest{UserID: userID} | ||
|
||
err := s.Call( | ||
&core.JSONRequest{Request: request, Path: "/following/invalidate"}, | ||
&response, | ||
) | ||
|
||
return response, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package following_test | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/yitsushi/go-misskey" | ||
"github.com/yitsushi/go-misskey/core" | ||
"github.com/yitsushi/go-misskey/services/following" | ||
"github.com/yitsushi/go-misskey/test" | ||
) | ||
|
||
func TestService_Invalidate(t *testing.T) { | ||
client := test.MakeMockClient(test.SimpleMockOptions{ | ||
Endpoint: "/api/following/invalidate", | ||
RequestData: &following.InvalidateRequest{}, | ||
ResponseFile: "user.json", | ||
StatusCode: http.StatusOK, | ||
}) | ||
|
||
user, err := client.Following().Invalidate("88v9vu5nbu") | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, "88v9vu5nbu", user.ID) | ||
} | ||
|
||
func TestInvalidateRequest_Validate(t *testing.T) { | ||
test.ValidateRequests( | ||
t, | ||
[]core.BaseRequest{ | ||
following.InvalidateRequest{}, | ||
following.InvalidateRequest{UserID: ""}, | ||
}, | ||
[]core.BaseRequest{ | ||
following.InvalidateRequest{UserID: "88v9vu5nbu"}, | ||
}, | ||
) | ||
} | ||
|
||
func ExampleService_Invalidate() { | ||
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN")) | ||
|
||
user, err := client.Following().Invalidate("88v9vu5nbu") | ||
if err != nil { | ||
log.Printf("[Following/Invalidate] %s", err) | ||
|
||
return | ||
} | ||
|
||
log.Printf("[Following/Invalidate] %s", user.Username) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package requests | ||
|
||
import ( | ||
"github.com/yitsushi/go-misskey/core" | ||
) | ||
|
||
// AcceptRequest is the request structure to accept a following request. | ||
type AcceptRequest struct { | ||
UserID string `json:"userId"` | ||
} | ||
|
||
// Validate request. | ||
func (r AcceptRequest) Validate() error { | ||
if r.UserID == "" { | ||
return core.RequestValidationError{ | ||
Request: r, | ||
Message: core.UndefinedRequiredField, | ||
Field: "UserID", | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Accept following endpoint. | ||
func (s *Service) Accept(userID string) error { | ||
request := &AcceptRequest{UserID: userID} | ||
|
||
err := s.Call( | ||
&core.JSONRequest{Request: request, Path: "/following/requests/accept"}, | ||
&core.EmptyResponse{}, | ||
) | ||
|
||
return err | ||
} |
Oops, something went wrong.