Пример #1
0
// Completes everything needed to make a request a non-blocking request
func completeNonBlockingRequest(data RequestData, w http.ResponseWriter, store storage.Storage) {
	storedRequest, err := store.StoreRequest(data.Blocking, data.Method, data.URL)
	if err != nil {
		gowebutils.SendError(w, err)
		return
	}
	if storedRequest == nil {
		gowebutils.SendError(w, fmt.Errorf("Unable to store non-blocking request"))
		return
	}
	err = respondToPost(w, storedRequest)
	if err != nil {
		gowebutils.SendError(w, err)
		return
	}
	go makeAndStoreRequest(storedRequest.RequestId, data)
}
Пример #2
0
func PostRequest(w http.ResponseWriter, r *http.Request) {
	store, body, err := prepareRequest(r)
	defer store.Close()
	if err != nil {
		gowebutils.SendError(w, err)
		return
	}

	var data RequestData
	err = json.Unmarshal(body, &data)
	if err != nil {
		gowebutils.SendError(w, err)
		return
	}

	if data.Blocking {
		completeBlockingRequest(data, w)
	} else {
		completeNonBlockingRequest(data, w, store)
	}
}
Пример #3
0
func GetRequest(c web.C, w http.ResponseWriter, r *http.Request) {
	requestId := c.URLParams["request_id"]
	if requestId == "" {
		err := errors.New("You must specify a request_id when retrieving a request")
		gowebutils.SendError(w, err)
		return
	}
	store, _, err := prepareRequest(r)
	defer store.Close()
	if err != nil {
		gowebutils.SendError(w, err)
		return
	}
	storedReq, err := store.GetRequest(requestId)
	if err != nil {
		gowebutils.SendError(w, err)
		return
	}
	if storedReq == nil {
		gowebutils.SendError(w, fmt.Errorf("Unable to find request with id '%v'", requestId))
		return
	}
	if storedReq.Finished {
		respBytes, err := ioutil.ReadAll(storedReq.Response.Body)
		if err != nil {
			gowebutils.SendError(w, err)
			return
		}
		w.Write(respBytes)
	} else {
		resp := RequestProcessingResp{
			Type:    "error",
			Code:    "request_processing",
			Message: "Request is currently processing and will complete soon",
		}
		json.NewEncoder(w).Encode(resp)
	}
}
Пример #4
0
// Completes everything needed to make a request a blocking request
func completeBlockingRequest(data RequestData, w http.ResponseWriter) {
	gowebutils.SendError(w, fmt.Errorf("Blocking requests are currently unsupported"))
}