Skip to content

Commit

Permalink
add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxyw authored Jul 17, 2022
1 parent 4ff506a commit cf7d41d
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/)

```
```
24 changes: 24 additions & 0 deletions example/list_authors.go
Original file line number Diff line number Diff line change
@@ -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)
}
24 changes: 24 additions & 0 deletions example/search_resources.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion spiget/authors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion spiget/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions spiget/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit cf7d41d

Please sign in to comment.