diff --git a/README.md b/README.md new file mode 100644 index 0000000..ba3370e --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +``` +# go-spiget + +go-spiget is a Go client library for accessing the [Spiget API v2](https://spiget.org/documentation). +## Installation + +go-spiget is compatible with modern Go releases in module mode, with Go installed: + +```bash +go get github.com/sunxyw/go-spiget +``` + +will resolve and add the package to the current development module, along with its dependencies. + +Alternatively the same can be achieved if you use import in a package: + +```go +import "github.com/sunxyw/go-spiget" +``` + +and run `go get` without parameters. + +## Usage/Examples + +Construct a new Spiget client, then use the various services on the client to access different parts of the Spiget API. +For example: + +```go +client := spiget.NewClient(nil) + +// Get resource with ID 6245, PlaceholderAPI +resource, _, err := client.Resources.Get(context.Background(), 6245) +``` + +Some API methods have optional parameters that can be passed. For example: + +```go +opt := &spiget.ResourceSearchOptions{ + Field: "name", +} +resources, _, err := client.Resources.Search(context.Background(), "PlaceholderAPI", opt) +``` + +The services of a client divide the API ito logical chunks and correspond to the structure of the Spiget API documentation at https://spiget.org/documentation . + +NOTE: Using the [context](https://godoc.org/context) package, one can easily pass cancelation signals and deadlines to various services of the client for handling a request. In case there is no context available, then context.Background() can be used as a starting point. + +For more sample code snippets, head over to the [example](https://github.com/sunxyw/go-spiget/tree/master/example) directory. + +## License + +[MIT](https://choosealicense.com/licenses/mit/) + +``` + +``` diff --git a/example/list_authors.go b/example/list_authors.go new file mode 100644 index 0000000..5143a05 --- /dev/null +++ b/example/list_authors.go @@ -0,0 +1,24 @@ +package example + +import ( + "context" + "fmt" + + "github.com/sunxyw/go-spiget/spiget" +) + +func ListAuthors() { + client := spiget.NewClient(nil) + + opt := &spiget.AuthorListOptions{ + ListOptions: spiget.ListOptions{ + Page: 3, + }, + } + authors, _, err := client.Authors.List(context.Background(), opt) + if err != nil { + panic(err) + } + + fmt.Println(authors) +} diff --git a/example/search_resources.go b/example/search_resources.go new file mode 100644 index 0000000..11888e5 --- /dev/null +++ b/example/search_resources.go @@ -0,0 +1,24 @@ +package example + +import ( + "context" + "fmt" + + "github.com/sunxyw/go-spiget/spiget" +) + +func SearchResources() { + client := spiget.NewClient(nil) + + opt := &spiget.ResourceSearchOptions{ + Field: "name", + } + resources, _, err := client.Resources.Search(context.Background(), "PlaceholderAPI", opt) + if err != nil { + panic(err) + } + + for _, resource := range resources { + fmt.Println(resource) + } +} diff --git a/spiget/authors.go b/spiget/authors.go index 2afdd99..3527046 100644 --- a/spiget/authors.go +++ b/spiget/authors.go @@ -86,7 +86,7 @@ type AuthorSearchOptions struct { // Search searches for authors by specified field. // // Spiget API docs: https://spiget.org/documentation/#!/authors/get_search_authors_query -func (a *AuthorsService) Search(ctx context.Context, query string, opts AuthorSearchOptions) ([]*Author, *Response, error) { +func (a *AuthorsService) Search(ctx context.Context, query string, opts *AuthorSearchOptions) ([]*Author, *Response, error) { u := "search/authors/" + query u, err := addOptions(u, opts) if err != nil { diff --git a/spiget/resources.go b/spiget/resources.go index 95ed809..0595b28 100644 --- a/spiget/resources.go +++ b/spiget/resources.go @@ -337,7 +337,7 @@ type ResourceSearchOptions struct { // Search resources. // // Spiget API docs: https://spiget.org/documentation/#!/resources/get_search_resources_query -func (r *ResourcesService) Search(ctx context.Context, query string, opts ResourceSearchOptions) ([]*Resource, *Response, error) { +func (r *ResourcesService) Search(ctx context.Context, query string, opts *ResourceSearchOptions) ([]*Resource, *Response, error) { u := "search/resources/" + query u, err := addOptions(u, opts) if err != nil { diff --git a/spiget/search.go b/spiget/search.go index 91add0c..ffa47bd 100644 --- a/spiget/search.go +++ b/spiget/search.go @@ -7,13 +7,13 @@ type SearchService service // Search authors. // // Note: This is actually an alias for the AuthorsService.Search method. -func (s *SearchService) SearchAuthor(ctx context.Context, query string, opts AuthorSearchOptions) ([]*Author, *Response, error) { +func (s *SearchService) SearchAuthor(ctx context.Context, query string, opts *AuthorSearchOptions) ([]*Author, *Response, error) { return s.client.Authors.Search(ctx, query, opts) } // Search resources. // // Note: This is actually an alias for the ResourcesService.Search method. -func (s *SearchService) SearchResource(ctx context.Context, query string, opts ResourceSearchOptions) ([]*Resource, *Response, error) { +func (s *SearchService) SearchResource(ctx context.Context, query string, opts *ResourceSearchOptions) ([]*Resource, *Response, error) { return s.client.Resources.Search(ctx, query, opts) }