Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More notes #54

Merged
merged 2 commits into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,17 @@ func (e RequestValidationError) Error() string {
e.Message,
)
}

// NotImplementedYet is an error for endpoint without implementation.
// The error will contain a reason for that, for example
// we don't know what is the response structure yet.
type NotImplementedYet struct {
Reason string
}

func (e NotImplementedYet) Error() string {
return fmt.Sprintf(
"Not implemented yet, reason: %s",
e.Reason,
)
}
1 change: 1 addition & 0 deletions models/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ type Note struct {
URI string `json:"uri"`
URL string `json:"url"`
Instance *Instance `json:"instance"`
Poll *Poll `json:"poll"`
}
46 changes: 46 additions & 0 deletions services/notes/conversation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package notes

import (
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/models"
)

// ConversationRequest represents an Conversation request.
type ConversationRequest struct {
NoteID string `json:"noteId"`
Limit uint `json:"limit"`
Offset uint `json:"offset,omitempty"`
}

// Validate the request.
func (r ConversationRequest) Validate() error {
if r.NoteID == "" {
return core.RequestValidationError{
Request: r,
Message: core.UndefinedRequiredField,
Field: "NoteID",
}
}

if r.Limit < 1 || r.Limit > 100 {
return core.RequestValidationError{
Request: r,
Message: core.NewRangeError(1, 100),
Field: "Limit",
}
}

return nil
}

// Conversation endpoint.
func (s *Service) Conversation(request ConversationRequest) ([]models.Note, error) {
var response []models.Note

err := s.Call(
&core.JSONRequest{Request: &request, Path: "/notes/conversation"},
&response,
)

return response, err
}
67 changes: 67 additions & 0 deletions services/notes/conversation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package notes_test

import (
"log"
"net/http"
"os"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/services/notes"
"github.com/yitsushi/go-misskey/test"
)

func TestService_Conversation(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/notes/conversation",
RequestData: &notes.ConversationRequest{},
ResponseFile: "conversation.json",
StatusCode: http.StatusOK,
})

noteList, err := client.Notes().Conversation(notes.ConversationRequest{
NoteID: "noteid",
Limit: 10,
})
if !assert.NoError(t, err) {
return
}

assert.Len(t, noteList, 4)
}

func TestConversationRequest_Validate(t *testing.T) {
test.ValidateRequests(
t,
[]core.BaseRequest{
notes.ConversationRequest{},
notes.ConversationRequest{NoteID: "asd"},
notes.ConversationRequest{Limit: 10},
},
[]core.BaseRequest{
notes.ConversationRequest{NoteID: "asd", Limit: 20},
},
)
}

func ExampleService_Conversation() {
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN"))
client.LogLevel(logrus.DebugLevel)

noteList, err := client.Notes().Conversation(notes.ConversationRequest{
NoteID: "noteid",
Limit: 10,
})
if err != nil {
log.Printf("[Notes] Error happened: %s", err)

return
}

for _, note := range noteList {
log.Printf(" - %s", note.Text)
}
}
28 changes: 28 additions & 0 deletions services/notes/favorites/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package favorites

import (
"github.com/yitsushi/go-misskey/core"
)

// CreateRequest represents an Create request.
type CreateRequest struct {
NoteID string `json:"noteId"`
}

// Validate the request.
func (r CreateRequest) Validate() error {
return nil
}

// Create endpoint.
func (s *Service) Create(noteID string) error {
return s.Call(
&core.JSONRequest{
Request: &CreateRequest{
NoteID: noteID,
},
Path: "/notes/favorites/create",
},
&core.EmptyResponse{},
)
}
38 changes: 38 additions & 0 deletions services/notes/favorites/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package favorites_test

import (
"log"
"net/http"
"os"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"github.com/yitsushi/go-misskey/services/notes/favorites"
"github.com/yitsushi/go-misskey/test"
)

func TestService_Create(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/notes/favorites/create",
RequestData: &favorites.CreateRequest{},
ResponseFile: "empty",
StatusCode: http.StatusNoContent,
})

err := client.Notes().Favorites().Create("noteid")
assert.NoError(t, err)
}

func ExampleService_Create() {
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN"))
client.LogLevel(logrus.DebugLevel)

err := client.Notes().Favorites().Create("noteid")
if err != nil {
log.Printf("[Notes] Error happened: %s", err)

return
}
}
28 changes: 28 additions & 0 deletions services/notes/favorites/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package favorites

import (
"github.com/yitsushi/go-misskey/core"
)

// DeleteRequest represents an Delete request.
type DeleteRequest struct {
NoteID string `json:"noteId"`
}

// Validate the request.
func (r DeleteRequest) Validate() error {
return nil
}

// Delete endpoint.
func (s *Service) Delete(noteID string) error {
return s.Call(
&core.JSONRequest{
Request: &DeleteRequest{
NoteID: noteID,
},
Path: "/notes/favorites/delete",
},
&core.EmptyResponse{},
)
}
38 changes: 38 additions & 0 deletions services/notes/favorites/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package favorites_test

import (
"log"
"net/http"
"os"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"github.com/yitsushi/go-misskey/services/notes/favorites"
"github.com/yitsushi/go-misskey/test"
)

func TestService_Delete(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/notes/favorites/delete",
RequestData: &favorites.DeleteRequest{},
ResponseFile: "empty",
StatusCode: http.StatusNoContent,
})

err := client.Notes().Favorites().Delete("noteid")
assert.NoError(t, err)
}

func ExampleService_Delete() {
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN"))
client.LogLevel(logrus.DebugLevel)

err := client.Notes().Favorites().Delete("noteid")
if err != nil {
log.Printf("[Notes] Error happened: %s", err)

return
}
}
Empty file.
13 changes: 13 additions & 0 deletions services/notes/favorites/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package favorites

import "github.com/yitsushi/go-misskey/core"

// Service is the base for all the endpoints on this service.
type Service struct {
Call core.RequestHandlerFunc
}

// NewService creates a new Service instance.
func NewService(requestHandler core.RequestHandlerFunc) *Service {
return &Service{Call: requestHandler}
}
Loading