instance.go
changeset 221 f9228d1f4267
parent 211 12f0761e6442
child 238 1c0042e76902
equal deleted inserted replaced
220:8794323cabbe 221:f9228d1f4267
     5 */
     5 */
     6 
     6 
     7 package madon
     7 package madon
     8 
     8 
     9 import (
     9 import (
       
    10 	"fmt"
       
    11 	"strconv"
       
    12 	"strings"
       
    13 	"time"
       
    14 
    10 	"github.com/sendgrid/rest"
    15 	"github.com/sendgrid/rest"
    11 )
    16 )
    12 
    17 
    13 // GetCurrentInstance returns current instance information
    18 // GetCurrentInstance returns current instance information
    14 func (mc *Client) GetCurrentInstance() (*Instance, error) {
    19 func (mc *Client) GetCurrentInstance() (*Instance, error) {
    18 	}
    23 	}
    19 	return &i, nil
    24 	return &i, nil
    20 }
    25 }
    21 
    26 
    22 // GetInstancePeers returns current instance peers
    27 // GetInstancePeers returns current instance peers
       
    28 // The peers are defined as the domains of users the instance has previously
       
    29 // resolved.
    23 func (mc *Client) GetInstancePeers() ([]InstancePeer, error) {
    30 func (mc *Client) GetInstancePeers() ([]InstancePeer, error) {
    24 	var peers []InstancePeer
    31 	var peers []InstancePeer
    25 	if err := mc.apiCall("instance/peers", rest.Get, nil, nil, nil, &peers); err != nil {
    32 	if err := mc.apiCall("instance/peers", rest.Get, nil, nil, nil, &peers); err != nil {
    26 		return nil, err
    33 		return nil, err
    27 	}
    34 	}
    28 	return peers, nil
    35 	return peers, nil
    29 }
    36 }
    30 
    37 
    31 // GetInstanceActivity returns current instance activity
    38 // GetInstanceActivity returns current instance activity
    32 func (mc *Client) GetInstanceActivity() (interface{}, error) {
    39 func (mc *Client) GetInstanceActivity() ([]WeekActivity, error) {
    33 	var activity interface{}
    40 	var activity []WeekActivity
    34 	if err := mc.apiCall("instance/activity", rest.Get, nil, nil, nil, &activity); err != nil {
    41 	if err := mc.apiCall("instance/activity", rest.Get, nil, nil, nil, &activity); err != nil {
    35 		return nil, err
    42 		return nil, err
    36 	}
    43 	}
    37 	return activity, nil
    44 	return activity, nil
    38 }
    45 }
       
    46 
       
    47 /* Activity time handling */
       
    48 
       
    49 // UnmarshalJSON handles deserialization for custom ActivityTime type
       
    50 func (act *ActivityTime) UnmarshalJSON(b []byte) error {
       
    51 	s, err := strconv.ParseInt(strings.Trim(string(b), "\""), 10, 64)
       
    52 	if err != nil {
       
    53 		return err
       
    54 	}
       
    55 	if s == 0 {
       
    56 		act.Time = time.Time{}
       
    57 		return nil
       
    58 	}
       
    59 	act.Time = time.Unix(s, 0)
       
    60 	return nil
       
    61 }
       
    62 
       
    63 // MarshalJSON handles serialization for custom ActivityTime type
       
    64 func (act *ActivityTime) MarshalJSON() ([]byte, error) {
       
    65 	return []byte(fmt.Sprintf("\"%d\"", act.Unix())), nil
       
    66 }