Exemplo n.º 1
0
// DecodeSlice decodes a JSON document array into a slice of Go native objects
func DecodeSlice(doc []byte, sliceObj interface{}, obj interface{}) (bool, error) {
	tracelog.STARTED("utils", "DecodeSlice")

	if IsArrayResponse(doc) == false {
		//decode as struct
		if err := Decode(doc, obj); err != nil {
			tracelog.ERROR(err, "utils", "DecodeSlice, Item Not Array, Unable to decode as struct")
			return false, err
		}

		//return false since not an array
		return false, nil
	}

	sliceMap := []map[string]interface{}{}

	if err := json.Unmarshal(doc, &sliceMap); err != nil {
		tracelog.ERROR(err, "utils", "DecodeSlice")
		return false, err
	}

	if err := mapstructure.DecodeSlicePath(sliceMap, sliceObj); err != nil {
		tracelog.ERROR(err, "utils", "DecodeSlice, Decoding Slice Object")
		return false, err
	}

	tracelog.COMPLETED("utils", "DecodeSlice")
	return true, nil
}
Exemplo n.º 2
0
func (this *Service) Prepare() (err error) {
	this.MongoSession, err = mongo.CopyMonotonicSession(this.UserId)
	if err != nil {
		tracelog.ERROR(err, this.UserId, "Service.Prepare")
		return err
	}

	return err
}
Exemplo n.º 3
0
// Decode takes a JSON document and decodes that into a Go native object
func Decode(doc []byte, obj interface{}) error {
	tracelog.STARTED("utils", "Decode")
	docMap := map[string]interface{}{}

	if err := json.Unmarshal(doc, &docMap); err != nil {
		tracelog.ERROR(err, "utils", "Decode, Building Mapped Doc")
		return err
	}

	return DecodeMap(docMap, obj)
}
Exemplo n.º 4
0
// DecodeMap decodes a JSON document into a Go native object
func DecodeMap(docMap map[string]interface{}, obj interface{}) error {
	if err := mapstructure.DecodePath(docMap, obj); err != nil {
		tracelog.ERROR(err, "utils", "Decode, Decoding Mapped Doc")
		return err
	}

	if tracelog.LogLevel() == tracelog.LEVEL_TRACE {
		helper.LogModel(obj, true)
	}

	return nil
}
Exemplo n.º 5
0
// Post performs a post request with the specified headers.
func (t *Transport) PostWithHeaders(url string, postParams url.Values, headers map[string]string) ([]byte, error) {
	client := &http.Client{Transport: t}
	req, err := http.NewRequest("POST", url, strings.NewReader(postParams.Encode()))
	if err != nil {
		tracelog.ERROR(err, "http_client", "PostWithHeaders")
		return nil, err
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	for key, value := range headers {
		req.Header.Set(key, value)
	}

	resp, err := client.Do(req)
	if err != nil {
		tracelog.ERROR(err, "http_client", "PostWithHeaders")
		return nil, err
	}

	return loadResponse(resp)
}
Exemplo n.º 6
0
func Prepare() *services.Service {
	service := &services.Service{}

	// TODO: Add Test User To Environment
	service.UserId = "testing"

	err := service.Prepare()
	if err != nil {
		tracelog.ERROR(err, service.UserId, "Prepare")
		return nil
	}

	tracelog.TRACE(service.UserId, "Before", "UserId[%s]", service.UserId)
	return service
}
Exemplo n.º 7
0
// Prepare is called create a service object
func Prepare() *services.Service {
	service := &services.Service{}

	service.UserId = "testing" // TODO: Deal With This Later
	tracelog.TRACE(service.UserId, "Before", "UserId[%s]", service.UserId)

	var err error
	service.MongoSession, err = mongo.CopyMonotonicSession(service.UserId)
	if err != nil {
		tracelog.ERROR(err, service.UserId, "Before")
		return nil
	}

	return service
}
Exemplo n.º 8
0
// Get performs a Get request with the specified headers.
func (t *Transport) GetWithHeaders(url string, headers map[string]string) ([]byte, error) {
	client := &http.Client{Transport: t}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		tracelog.ERROR(err, "http_client", "GetWithHeaders")
		return nil, err
	}

	for key, value := range headers {
		req.Header.Set(key, value)
	}

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