Skip to content

mogita/go-fanfou

Repository files navigation

GO FANFOU

A Fanfou API library for Go

Software License Travis Go Report Card Coverage Status GoDoc

Key Features

  • Simple and intuitive endponits and error wrapping
  • Struct and original JSON output
  • Supports OAuth (safer) and XAuth (simpler)
  • Covers all endpoints of Fanfou API v1 over HTTPS

Installation

$ go get -u github.com/mogita/go-fanfou

Usage

package main

import "github.com/mogita/go-fanfou/fanfou"

func main() {
  // ...
}

Basic

To call an endpoint e.g. /statuses/public_timeline, you can call it like this:

// Every API endpoint has the same return value structure
data, JSON, err := c.Statuses.PublicTimeline(&fanfou.StatusesOptParams{
    Count: 10,
})

All optional parameter types starts with the resource's name. E.g. Statuses -> StatusesOptParams.

See the examples directory to learn how to authenticate the client instance before calling the endpoints.

Error Handling

Errors default to the format as below:

POST http://api.fanfou.com/photos/upload.json: 400 上传照片失败

Meanwhile they can be asserted to extract the specific detail that you can use to handle the errors programmatically. Like this:

_, _, err := c.Statuses.PublicTimeline(nil)

if err != nil {
    if fanfouErr, ok := err.(*fanfou.ErrorResponse); ok {
    	// Will print only the error message text returned by Fanfou API
        fmt.Printf("%s\n", fanfouErr.GetFanfouError())
        return
    }

    // Will print the default error format
    fmt.Println(err)
    return
}

Running the Examples

Check out the examples folder for working code snippets. You can run the examples with these commands to see how this library works:

Standard OAuth

$ go run examples/oauth/oauth.go --consumerkey <your_consumer_key> --consumersecret <your_consumer_secret>

OOB OAuth

OOB stands for "out of band"

$ go run examples/oauth_oob/oauth_oob.go --consumerkey <your_consumer_key> --consumersecret <your_consumer_secret>

XAuth

$ go run examples/xauth/xauth.go --consumerkey <your_consumer_key> --consumersecret <your_consumer_secret> --username <your_username> --password <your_password>

Upload Photos

$ go run examples/upload_photo/upload_photo.go --consumerkey <your_consumer_key> --consumersecret <your_consumer_secret> --username <your_username> --password <your_password>

Credits

Contributing

Thank you very much for paying attention to this library. If you feel like helping improve it, please kindly make sure to follow the instructions:

Link the pre-commit hook which runs tests and go-fmt before committing

ln -s $PWD/pre-commit.sh .git/hooks/pre-commit

Always run tests before committing

go test ./...

You can also follow this Trello board if you're interested in the progress of this project and its sibling products.

License

MIT © mogita