bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/annotate/client.go (about)

     1  package annotate
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"net/http"
     7  	"time"
     8  )
     9  
    10  type Client struct {
    11  	apiRoot string
    12  	client  *http.Client
    13  }
    14  
    15  func NewClient(apiRoot string) Client {
    16  	return Client{
    17  		apiRoot: apiRoot,
    18  		client:  &http.Client{},
    19  	}
    20  }
    21  
    22  // SendAnnotation sends a annotation to an annotations server
    23  // apiRoot should be "http://foo/api" where the annotation routes
    24  // would be available at http://foo/api/annotation...
    25  func (c *Client) SendAnnotation(a Annotation) (Annotation, error) {
    26  	b, err := json.Marshal(a)
    27  	if err != nil {
    28  		return a, err
    29  	}
    30  
    31  	res, err := c.client.Post(c.apiRoot+"/annotation", "application/json", bytes.NewReader(b))
    32  	if err != nil {
    33  		return a, err
    34  	}
    35  	defer res.Body.Close()
    36  	err = json.NewDecoder(res.Body).Decode(&a)
    37  	return a, err
    38  }
    39  
    40  // GetAnnotation gets an annotation by ID, and will return
    41  // nil without an error if the annotation does not exist
    42  func (c *Client) GetAnnotation(id string) (*Annotation, error) {
    43  	a := &Annotation{}
    44  	res, err := c.client.Get(c.apiRoot + "/annotation/" + id)
    45  	if res != nil && res.StatusCode == http.StatusNotFound {
    46  		return nil, nil
    47  	}
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	defer res.Body.Close()
    52  	err = json.NewDecoder(res.Body).Decode(&a)
    53  	return a, err
    54  }
    55  
    56  func (c *Client) GetAnnotations(start, end *time.Time, source, host, creationUser, owner, category, url, message string) (Annotations, error) {
    57  	a := Annotations{}
    58  	req, err := http.NewRequest("GET", c.apiRoot+"/annotation/query", nil)
    59  	if err != nil {
    60  		return a, nil
    61  	}
    62  	q := req.URL.Query()
    63  	if start != nil {
    64  		q.Add(StartDate, start.Format(time.RFC3339))
    65  	}
    66  	if end != nil {
    67  		q.Add(EndDate, end.Format(time.RFC3339))
    68  	}
    69  	if source != "" {
    70  		q.Add(Source, source)
    71  	}
    72  	if host != "" {
    73  		q.Add(Host, host)
    74  	}
    75  	if creationUser != "" {
    76  		q.Add(CreationUser, creationUser)
    77  	}
    78  	if owner != "" {
    79  		q.Add(Owner, owner)
    80  	}
    81  	if category != "" {
    82  		q.Add(Category, category)
    83  	}
    84  	if url != "" {
    85  		q.Add(Url, url)
    86  	}
    87  	if message != "" {
    88  		q.Add(Message, message)
    89  	}
    90  	req.URL.RawQuery = q.Encode()
    91  	req.Header.Add("Accept", "application/json")
    92  	res, err := c.client.Do(req)
    93  	if err != nil {
    94  		return a, err
    95  	}
    96  	defer res.Body.Close()
    97  	err = json.NewDecoder(res.Body).Decode(&a)
    98  	return a, err
    99  }