Ejemplo n.º 1
0
func (s roshiStreamService) Add(items []model.StreamItem) error {
	rItems, err := model.ToRoshiStreamItem(items)
	if err != nil {
		log.Error(err)
		return err
	}

	requestBody, err := json.Marshal(rItems)
	if err != nil {
		log.Error(err)
		return err
	}

	uri := s.url.String()

	log.WithFields(log.Fields{
		"Body": string(requestBody),
		"URL":  uri,
	}).Debug("Preparing to make request")

	req, err := http.NewRequest("POST", uri, bytes.NewBuffer(requestBody))
	if log.GetLevel() >= log.DebugLevel {
		debug(httputil.DumpRequestOut(req, true))
	}

	if err != nil {
		log.Error(err)
		return err
	}
	client := &http.Client{
		Timeout: s.timeout,
	}
	log.WithFields(log.Fields{
		"client": client,
		"req":    req,
	}).Debug("About to execute")

	resp, err := client.Do(req)
	if err != nil {
		log.Error(err)
		return err
	}

	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		debug(httputil.DumpResponse(resp, true))
		return errors.New("Request Failed with status: " + string(resp.StatusCode))
	}

	return nil
}
Ejemplo n.º 2
0
func TestJsonMarshal(t *testing.T) {
	id, _ := uuid.V4()
	item := model.StreamItem{
		StreamID: id.String(),
		// NOTE:  Converting between int64/float64 at the nanosecond level can
		//				create some tiny drift. In practice, this is fine.  Rounding to
		//				the second level avoids issues with testing.
		Timestamp: time.Now().Round(time.Second),
		Type:      model.TypePost,
		ID:        id.String(),
	}

	output, _ := json.Marshal(item)
	var fromJSON model.StreamItem
	err := json.Unmarshal(output, &fromJSON)

	log.WithFields(log.Fields{
		"Source":   item,
		"JSON":     string(output),
		"fromJSON": fromJSON,
		"ERROR":    err,
	}).Debug("StreamItem Example")

	if err != nil {
		t.Errorf("Error converting to/from json")
	}

	CheckStreamItems(item, fromJSON, t)

	output2, _ := json.Marshal(model.RoshiStreamItem(item))
	var fromJSON2 model.RoshiStreamItem
	err = json.Unmarshal(output2, &fromJSON2)

	log.WithFields(log.Fields{
		"Source":   item,
		"JSON":     string(output2),
		"fromJSON": fromJSON2,
		"ERROR":    err,
	}).Debug("RoshiStreamItem Example")

	if err != nil {
		t.Errorf("Error converting to/from json")
	}

	checkRoshiItems(model.RoshiStreamItem(item), fromJSON2, t)

	items := []model.StreamItem{
		item,
		item,
	}
	rItems, _ := model.ToRoshiStreamItem(items)
	output3, _ := json.Marshal(rItems)
	var fromJSON3 []model.RoshiStreamItem
	err = json.Unmarshal(output3, &fromJSON3)

	log.WithFields(log.Fields{
		"Source":   rItems,
		"JSON":     string(output3),
		"fromJSON": fromJSON3,
		"ERROR":    err,
	}).Debug("RoshiStreamItem Example")

	checkAllRoshi(rItems, fromJSON3, t)
}