Esempio n. 1
0
// Samples requests a list of example event data.
func (c *Client) EventSamples(types *[]string) (*events.Events, error) {
	url, err := url.Parse(fmt.Sprintf(messageEventsSamplesPathFormat, c.Config.BaseUrl, c.Config.ApiVersion))
	if err != nil {
		return nil, err
	}

	// Filter out types.
	if types != nil {
		// validate types
		for _, etype := range *types {
			if !events.ValidEventType(etype) {
				return nil, fmt.Errorf("Invalid event type [%s]", etype)
			}
		}

		// get the query string object so we can modify it
		q := url.Query()
		// add the requested events and re-encode
		q.Set("events", strings.Join(*types, ","))
		url.RawQuery = q.Encode()
	}

	// Send off our request
	res, err := c.HttpGet(url.String())
	if err != nil {
		return nil, err
	}

	// Assert that we got a JSON Content-Type back
	if err = res.AssertJson(); err != nil {
		return nil, err
	}

	// Get the Content
	bodyBytes, err := res.ReadBody()
	if err != nil {
		return nil, err
	}

	var events events.Events
	err = json.Unmarshal(bodyBytes, &events)
	if err != nil {
		return nil, err
	}

	return &events, nil
}
Esempio n. 2
0
// Samples requests a list of example event data.
func (c *Client) EventSamples(types *[]string) (*[]events.Event, error) {
	// append any requested event types to path
	var url string
	path := fmt.Sprintf(messageEventsPathFormat, c.Config.ApiVersion)
	if types == nil {
		url = fmt.Sprintf("%s%s/events/samples", c.Config.BaseUrl, path)
	} else {
		// validate types
		for _, etype := range *types {
			if !events.ValidEventType(etype) {
				return nil, fmt.Errorf("Invalid event type [%s]", etype)
			}
		}
		// break up the url into a net.URL object
		u, err := URL.Parse(fmt.Sprintf("%s%s/events/samples", c.Config.BaseUrl, path))
		if err != nil {
			return nil, err
		}

		// get the query string object so we can modify it
		q := u.Query()
		// add the requested events and re-encode
		q.Set("events", strings.Join(*types, ","))
		u.RawQuery = q.Encode()
		url = u.String()
	}

	// Send off our request
	res, err := c.HttpGet(url)
	if err != nil {
		return nil, err
	}

	// Assert that we got a JSON Content-Type back
	if err = res.AssertJson(); err != nil {
		return nil, err
	}

	// Get the Content
	bodyBytes, err := res.ReadBody()
	if err != nil {
		return nil, err
	}

	/*// DEBUG
	err = iou.WriteFile("./events.json", bodyBytes, 0644)
	if err != nil {
		return nil, err
	}
	*/

	// Parse expected response structure
	resMap := map[string][]*json.RawMessage{}
	err = json.Unmarshal(bodyBytes, &resMap)
	if err != nil {
		// FIXME: better error message
		return nil, err
	}

	// If the key "results" isn't present, something bad happened.
	results, ok := resMap["results"]
	if !ok {
		// FIXME: better error message
		return nil, fmt.Errorf("no results!")
	}

	return ParseEvents(results)
}