Skip to content

Commit

Permalink
Add partial support for follow_api
Browse files Browse the repository at this point in the history
  • Loading branch information
tchap committed Aug 15, 2016
1 parent 10872bf commit 4b0a4ca
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
13 changes: 13 additions & 0 deletions apis/follow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Follow API

This package adds support for `follow_api`.

## State

| Method Name | Raw Version | Full Version |
| ------------------------- |:-----------:|:------------:|
| `get_followers` | DONE | DONE |
| `get_following` | DONE | DONE |
| `get_feed_entries` | DONE | DONE |
| `get_feed` | DONE | |
| `get_account_reputations` | DONE | |
168 changes: 168 additions & 0 deletions apis/follow/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package follow

import (
// Stdlib
"encoding/json"

// RPC
"github.com/go-steem/rpc/interfaces"

// Vendor
"github.com/pkg/errors"
)

type API struct {
caller interfaces.Caller
}

func NewAPI(caller interfaces.Caller) *API {
return &API{caller}
}

func (api *API) call(method string, params, resp interface{}) error {
return api.caller.Call("call", []interface{}{"follow_api", method, params}, resp)
}

func (api *API) GetFollowersRaw(
accountName string,
start string,
kind string,
limit uint16,
) (*json.RawMessage, error) {

var resp json.RawMessage
params := []interface{}{accountName, start, kind, limit}
if err := api.call("get_followers", params, &resp); err != nil {
return nil, errors.Wrap(err, "go-steem/rpc: follow_api: failed to call get_followers")
}
return &resp, nil
}

func (api *API) GetFollowers(
accountName string,
start string,
kind string,
limit uint16,
) ([]*FollowObject, error) {

raw, err := api.GetFollowersRaw(accountName, start, kind, limit)
if err != nil {
return nil, err
}

var resp []*FollowObject
if err := json.Unmarshal([]byte(*raw), &resp); err != nil {
return nil, errors.Wrap(
err, "go-steem/rpc: follow_api: failed to unmarshal get_followers response")
}
return resp, nil

}

func (api *API) GetFollowingRaw(
accountName string,
start string,
kind string,
limit uint16,
) (*json.RawMessage, error) {

var resp json.RawMessage
params := []interface{}{accountName, start, kind, limit}
if err := api.call("get_following", params, &resp); err != nil {
return nil, errors.Wrap(err, "go-steem/rpc: follow_api: failed to call get_following")
}
return &resp, nil
}

func (api *API) GetFollowing(
accountName string,
start string,
kind string,
limit uint16,
) ([]*FollowObject, error) {

raw, err := api.GetFollowingRaw(accountName, start, kind, limit)
if err != nil {
return nil, err
}

var resp []*FollowObject
if err := json.Unmarshal([]byte(*raw), &resp); err != nil {
return nil, errors.Wrap(
err, "go-steem/rpc: follow_api: failed to unmarshal get_following response")
}
return resp, nil
}

func (api *API) GetFeedEntriesRaw(
accountName string,
entryID uint32,
limit uint16,
) (*json.RawMessage, error) {

if limit == 0 {
limit = 500
}

var resp json.RawMessage
params := []interface{}{accountName, entryID, limit}
if err := api.call("get_feed_entries", params, &resp); err != nil {
return nil, errors.Wrap(err, "go-steem/rpc: follow_api: failed to call get_feed_entries")
}
return &resp, nil
}

func (api *API) GetFeedEntries(
accountName string,
entryID uint32,
limit uint16,
) ([]*FeedEntry, error) {

raw, err := api.GetFeedEntriesRaw(accountName, entryID, limit)
if err != nil {
return nil, err
}

var resp []*FeedEntry
if err := json.Unmarshal([]byte(*raw), &resp); err != nil {
return nil, errors.Wrap(
err, "go-steem/rpc: follow_api: failed to unmarshal get_feed_entries response")
}
return resp, nil
}

func (api *API) GetFeedRaw(
accountName string,
entryID uint32,
limit uint16,
) (*json.RawMessage, error) {

if limit == 0 {
limit = 500
}

var resp json.RawMessage
params := []interface{}{accountName, entryID, limit}
if err := api.call("get_feed", params, &resp); err != nil {
return nil, errors.Wrap(err, "go-steem/rpc: follow_api: failed to call get_feed")
}
return &resp, nil
}

func (api *API) GetAccountReputationsRaw(
lowerBoundName string,
limit uint32,
) (*json.RawMessage, error) {

if limit == 0 {
limit = 1000
}

var resp json.RawMessage
params := []interface{}{lowerBoundName, limit}
if err := api.call("get_account_reputations", params, &resp); err != nil {
return nil, errors.Wrap(
err, "go-steem/rpc: follow_api: failed to call get_account_reputations")
}
return &resp, nil
}
32 changes: 32 additions & 0 deletions apis/follow/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package follow

const (
FollowKindFollow = "blog"
FollowKindIgnore = "ignore"
)

type FollowObject struct {
Follower string `json:"follower"`
Following string `json:"following"`
What []string `json:"what"`
}

type FeedEntry struct {
Author string `json:"string"`
Permlink string `json:"permlink"`
EntryID uint32 `json:"entry_id"`
}

/*
type CommentFeedEntry struct {
Comment *CommentObject `json:"comment"`
EntryID uint32 `json:"entry_id"`
}
*/

/*
type AccountReputation struct {
Account string `json:"account"`
Reputation ??? `json:"reputation"`
}
*/
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpc
import (
// RPC
"github.com/go-steem/rpc/apis/database"
"github.com/go-steem/rpc/apis/follow"
"github.com/go-steem/rpc/apis/login"
"github.com/go-steem/rpc/interfaces"
)
Expand All @@ -19,13 +20,17 @@ type Client struct {

// Database represents database_api.
Database *database.API

// Follow represents follow_api.
Follow *follow.API
}

// NewClient creates a new RPC client that use the given CallCloser internally.
func NewClient(cc interfaces.CallCloser) *Client {
client := &Client{cc: cc}
client.Login = login.NewAPI(client.cc)
client.Database = database.NewAPI(client.cc)
client.Follow = follow.NewAPI(client.cc)
return client
}

Expand Down

0 comments on commit 4b0a4ca

Please sign in to comment.