Example #1
0
func (gdp GenericDeleteProcessor) ProcessRequest(pathTokens []string, queryTokens []string, req objects.Storable,
	responder *APIResponder) ([]objects.Storable, *objects.CodedError) {

	lenp := len(pathTokens)
	log.Printf("Path parsed to %d tokens, ggp path has %d tokens", lenp, len(gdp.pathKeys))
	switch lenp - len(gdp.pathKeys) {
	case 0:
		{ // Addressed to general resource
			log.Println("GDP DELETE: general resource")
			return nil, objects.NewError("Not Found", 404)
		}
	case 1:
		{ // Addressed to buyerId
			log.Printf("GDP Delete: all buyer %s orders", pathTokens[lenp-1])
			err := gdp.DeleteAllByBuyer(pathTokens[lenp-1])
			if err != nil {
				return nil, objects.NewError(err.Error(), 500)
			}
			return nil, nil
		}
	case 2:
		{ // Addressed to specific item
			log.Printf("GDP Delete: specific order %s from buyer %s", pathTokens[lenp-1], pathTokens[lenp-2])
			err := gdp.DeleteObject(pathTokens[lenp-2], pathTokens[lenp-1])
			if err != nil {
				return nil, objects.NewError(err.Error(), 500)
			}

			retArr := make([]objects.Storable, 0)
			return retArr, nil
		}
	}

	return nil, objects.NewError("Problem processing DELETE request - too many path tokens", 400)
}
Example #2
0
func (c Client) GetRequest(url string, ref interface{}) *objects.CodedError {
	if len(url) == 0 {
		return objects.NewError("Could not GET: no URL specified", 500)
	}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return objects.FromError(err, 500)
	}
	req.Header.Set(PTV_HDR, PTV_HDR_VAL)

	resp, err := c.Do(req)
	if err != nil {
		return objects.FromError(err, 500)
	}
	status := resp.StatusCode
	log.Printf("GET received status code of %d", status)
	if status == 200 {
		buffer, err2 := c.ReadBody(resp)
		log.Printf("GET received body %s", buffer)
		defer resp.Body.Close()
		if err2 != nil {
			return objects.FromError(err2, 500)
		}
		log.Printf("GET response body read, len=%d", len(buffer))
		err2 = objects.Unmarshal(ref, buffer)
		if err2 != nil {
			return objects.FromError(err2, 500)
		}
		return nil
	}
	return objects.NewErrorf("Unsuccessful request, server returned %d", status, status)
}
Example #3
0
func (r InventoryRequestProcessor) ValidateRequest(pathTokens []string, queryTokens []string, rfp objects.Storable) *objects.CodedError {
	robj := rfp.(objects.AvailabilityRequestObject)
	if len(robj.RequestId) == 0 {
		return objects.NewError("No requestId", http.StatusBadRequest)
	}
	if len(robj.BuyerId) == 0 {
		return objects.NewError("No buyerId", http.StatusBadRequest)
	}
	if len(robj.AdvertiserId) == 0 {
		return objects.NewError("No advertiserId", http.StatusBadRequest)
	}
	if len(robj.ResponseUrl) == 0 {
		return objects.NewError("No responseUrl", http.StatusBadRequest)
	}
	log.Println("Request validated")
	return nil
}
Example #4
0
func (r OrderAPIProcessor) ValidateRequest(pathTokens []string, queryTokens []string, order objects.Storable) *objects.CodedError {
	robj := order.(objects.OrderObject)
	if len(robj.RequestId) == 0 {
		return objects.NewError("No requestId", http.StatusBadRequest)
	}
	if len(robj.BuyerId) == 0 {
		return objects.NewError("No buyerId", http.StatusBadRequest)
	}
	if len(robj.AdvertiserId) == 0 {
		return objects.NewError("No advertiserId", http.StatusBadRequest)
	}
	if len(robj.ResponseUrl) == 0 {
		return objects.NewError("No responseUrl", http.StatusBadRequest)
	}
	log.Println("Request validated")
	return nil
}
Example #5
0
func (r CallbackProcessor) ValidateRequest(pathTokens []string, queryTokens []string, rfp objects.Storable) *objects.CodedError {
	if &rfp == nil {
		log.Println("CallbackProcessor.received = nil")
		r.t.Fail()
		return objects.NewError("No response received", 400)
	}
	return nil
}
Example #6
0
func (r OrderAPIProcessor) ProcessRequest(pathTokens []string, queryTokens []string, order objects.Storable, responder *APIResponder) ([]objects.Storable, *objects.CodedError) {
	log.Println("processRequest")
	robj := order.(objects.OrderObject)
	err := r.SaveObject(robj.BuyerId, robj.RequestId, &robj)
	if err != nil {
		return nil, objects.NewError(err.Error(), http.StatusInternalServerError)
	}
	// Build the response
	respObjs := make([]objects.Storable, 1)
	respObjs[0] = &objects.OrderAcceptanceObject{}

	// Send it later
	go responder.waitAndSendResult(respObjs, robj.ResponseUrl, robj.GetKey(), 1)
	return nil, nil
}
Example #7
0
func (c Client) DelRequest(url string) *objects.CodedError {
	if len(url) == 0 {
		return objects.NewError("Could not GET: no URL specified", 500)
	}
	req, err := http.NewRequest("DELETE", url, nil)
	if err != nil {
		return objects.FromError(err, 500)
	}
	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)
}
Example #8
0
func (r InventoryRequestProcessor) ProcessRequest(pathTokens []string, queryTokens []string, rfp objects.Storable, responder *APIResponder) ([]objects.Storable, *objects.CodedError) {
	log.Println("processRequest")
	// Save the request
	robj := rfp.(objects.AvailabilityRequestObject)
	err := r.SaveObject(robj.BuyerId, robj.RequestId, &robj)
	if err != nil {
		return nil, objects.NewError(err.Error(), http.StatusInternalServerError)
	}
	// Build the response
	respObj := &objects.AvailabilityResponseObject{}
	respObj.RequestId = robj.RequestId
	respObj.BuyerId = robj.BuyerId
	respObj.MinImpressions = robj.MinImpressions
	respObj.MaxImpressions = robj.MaxImpressions
	respObj.MinCPM = 15.30

	respObjs := make([]objects.Storable, 1)
	respObjs[0] = respObj

	// Send it later
	go responder.waitAndSendResult(respObjs, robj.ResponseUrl, robj.GetKey(), 1)

	return nil, nil
}