github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/pulse.go (about)

     1  package rest
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  	"path"
     8  	"strconv"
     9  
    10  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
    11  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/common"
    12  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/pulse"
    13  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/pulseprofile"
    14  )
    15  
    16  type PulseService struct {
    17  	requestFactory
    18  	Synchronous
    19  }
    20  
    21  type Nickname string
    22  
    23  // PublicPulseProfile returns details for a specific Pulse profile
    24  // https://docs.bitfinex.com/reference#rest-public-pulse-profile
    25  func (ps *PulseService) PublicPulseProfile(nickname Nickname) (*pulseprofile.PulseProfile, error) {
    26  	if (len(nickname)) == 0 {
    27  		return nil, fmt.Errorf("nickname is required argument")
    28  	}
    29  
    30  	req := NewRequestWithMethod(path.Join("pulse", "profile", string(nickname)), "GET")
    31  	raw, err := ps.Request(req)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	pp, err := pulseprofile.NewFromRaw(raw)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	return pp, nil
    42  }
    43  
    44  // PublicPulseHistory returns latest pulse messages. You can specify
    45  // an end timestamp to view older messages.
    46  // see https://docs.bitfinex.com/reference#rest-public-pulse-hist
    47  func (ps *PulseService) PublicPulseHistory(limit int, end common.Mts) ([]*pulse.Pulse, error) {
    48  	req := NewRequestWithMethod(path.Join("pulse", "hist"), "GET")
    49  	req.Params = make(url.Values)
    50  	req.Params.Add("limit", strconv.Itoa(limit))
    51  	req.Params.Add("end", strconv.FormatInt(int64(end), 10))
    52  
    53  	raw, err := ps.Request(req)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	pph, err := pulse.SnapshotFromRaw(raw)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return pph, nil
    64  }
    65  
    66  // AddPulse submits pulse message
    67  // see https://docs.bitfinex.com/reference#rest-auth-pulse-add
    68  func (ps *PulseService) AddPulse(p *pulse.Pulse) (*pulse.Pulse, error) {
    69  	tl := len(p.Title)
    70  	if tl < 16 || tl > 120 {
    71  		return nil, fmt.Errorf("Title length min 16 and max 120 characters. Got:%d", tl)
    72  	}
    73  
    74  	payload, err := json.Marshal(p)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	req, err := ps.requestFactory.NewAuthenticatedRequestWithBytes(common.PermissionWrite, path.Join("pulse", "add"), payload)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	raw, err := ps.Request(req)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	pm, err := pulse.FromRaw(raw)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	return pm, nil
    95  }
    96  
    97  // AddComment submits pulse comment
    98  // see https://docs.bitfinex.com/reference#rest-auth-pulse-add
    99  func (ps *PulseService) AddComment(p *pulse.Pulse) (*pulse.Pulse, error) {
   100  	if len(p.Parent) == 0 {
   101  		return nil, fmt.Errorf("Pulse comment requires `Parent` parameter to be set")
   102  	}
   103  
   104  	return ps.AddPulse(p)
   105  }
   106  
   107  // PulseHistory allows you to retrieve your pulse history.
   108  // see https://docs.bitfinex.com/reference#rest-auth-pulse-hist
   109  func (ps *PulseService) PulseHistory() ([]*pulse.Pulse, error) {
   110  	req, err := ps.NewAuthenticatedRequest(common.PermissionRead, path.Join("pulse", "hist"))
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	raw, err := ps.Request(req)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	pph, err := pulse.SnapshotFromRaw(raw)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	return pph, nil
   126  }
   127  
   128  // DeletePulse removes your pulse message. Returns 0 if no pulse was deleted and 1 if it was
   129  // see https://docs.bitfinex.com/reference#rest-auth-pulse-del
   130  func (ps *PulseService) DeletePulse(pid string) (int, error) {
   131  	payload := map[string]interface{}{"pid": pid}
   132  
   133  	req, err := ps.NewAuthenticatedRequestWithData(common.PermissionWrite, path.Join("pulse", "del"), payload)
   134  	if err != nil {
   135  		return 0, err
   136  	}
   137  
   138  	raw, err := ps.Request(req)
   139  	if err != nil {
   140  		return 0, err
   141  	}
   142  
   143  	return convert.ToInt(raw[0]), nil
   144  }