Passed
Push — main ( 0f0403...5f05f1 )
by Acho
02:42
created

heartbeat_service.go   A

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 25
dl 0
loc 43
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A httpsms.*HeartbeatService.Index 0 28 5
1
package httpsms
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"net/http"
7
	"strconv"
8
)
9
10
// HeartbeatService is the API client for the `/heartbeats` endpoint
11
type HeartbeatService service
12
13
// Index returns a list of heartbeats from an android phone. It will be sorted by timestamp in descending order.
14
//
15
// API Docs: https://api.httpsms.com/index.html#/Heartbeats/get_heartbeats
16
func (service *HeartbeatService) Index(ctx context.Context, params *HeartbeatIndexParams) (*HeartbeatsResponse, *Response, error) {
17
	request, err := service.client.newRequest(ctx, http.MethodGet, "/v1/heartbeats", nil)
18
	if err != nil {
19
		return nil, nil, err
20
	}
21
22
	q := request.URL.Query()
23
	q.Add("skip", strconv.Itoa(params.Skip))
24
	q.Add("owner", params.Owner)
25
	q.Add("limit", strconv.Itoa(params.Limit))
26
27
	if params.Query != nil {
28
		q.Add("query", *params.Query)
29
	}
30
31
	request.URL.RawQuery = q.Encode()
32
33
	response, err := service.client.do(request)
34
	if err != nil {
35
		return nil, response, err
36
	}
37
38
	heartbeats := new(HeartbeatsResponse)
39
	if err = json.Unmarshal(*response.Body, heartbeats); err != nil {
40
		return nil, response, err
41
	}
42
43
	return heartbeats, response, nil
44
}
45