Beispiel #1
0
func (r APIResponder) waitAndSendResult(resp []objects.Storable, url string, key string, delay int) {
	log.Printf("waitAndSendResult called with url %s and key %s", url, key)

	// Let's go to sleep for a while
	time.Sleep(time.Duration(delay) * time.Second)
	var buffer []byte
	var err error

	if len(resp) == 1 {
		buffer, err = objects.Marshal(resp[0])
	} else {
		buffer, err = objects.Marshal(resp)
	}

	if err != nil {
		log.Printf("Could not marshal callback object %s", key)
		return
	}
	cb := NewCallbacker(url)
	err = cb.Callback(buffer)
	if err != nil {
		log.Printf("Could not send callback %s", key)
		return
	}
}
Beispiel #2
0
func (r APIResponder) Handle(req *http.Request) (int, []byte) {

	log.Println("APIResponder.Handle entered")
	method := req.Method
	u := req.URL
	pathTokens := util.Unmunge(u.Path)
	queryTokens := strings.Split(u.RawQuery, "&")

	// Read the request body
	buffer, err := r.ReadBody(req)
	if err != nil {
		return BAD, nil
	}
	log.Println("APIResponder.Handle: body read")

	pr := r.getProcessor(method)
	if &pr == nil {
		return BAD, nil
	}

	obj, err := pr.Unmarshal(buffer)
	if err != nil {
		return ERROR, nil
	}
	if obj != nil {
		log.Printf("Have object of type %T", obj)
		log.Printf("Received message with RequestId %s:\n%s\n", obj.GetKey(), buffer)
	} else {
		log.Println("Request did not include a body")
	}
	cerr := pr.ValidateRequest(pathTokens, queryTokens, obj)
	if cerr != nil {
		log.Printf("Invalid %s request: %s\n", method, cerr.Error())
		return cerr.Code(), nil
	}

	// Process it
	responses, cerr := pr.ProcessRequest(pathTokens, queryTokens, obj, &r)
	if cerr != nil {
		log.Printf("Error processing %s request: %s\n", method, cerr.Error())
		return cerr.Code(), nil
	}

	// Marshal the results into the response body
	if len(responses) > 0 {
		buffer, err = objects.Marshal(responses)
		log.Printf("Marshalling %d objects into response as \n%s", len(responses), buffer)
	}
	if err != nil {
		log.Printf("Error marshalling response: %s\n", err.Error())
		return ERROR, nil
	}
	log.Printf("%s Request handled", method)
	return OK, buffer

}
Beispiel #3
0
func (c Client) PostRequest(obj interface{}, path string) *objects.CodedError {
	buffer, err := objects.Marshal(obj)
	if err != nil {
		log.Printf("Error marshalling request body, error=%s", err.Error())
		return objects.FromError(err, 500)
	}
	req, err := http.NewRequest("POST", c.makeUrl(path), bytes.NewReader(buffer))
	if err != nil {
		return objects.FromError(err, 500)
	}
	req.Header.Set("Content-Type", CONTENT_TYPE)
	req.Header.Set(PTV_HDR, PTV_HDR_VAL)

	resp, err := c.Do(req)
	if err != nil {
		return objects.FromError(err, 500)
	}
	status := resp.StatusCode
	if status == 200 {
		return nil
	}
	return objects.NewErrorf("Unsuccessful request, server returned %d", status, status)

}