Exemplo n.º 1
0
        /// <summary>
        /// Method to process the request that has come into the API
        /// </summary>
        /// <param name="req">Request object</param>
        /// <param name="table">Table object on which operations will be performed</param>
        /// <param name="log">Logger interface</param>
        /// <param name="identifier">Identifier passed into the API</param>
        /// <param name="category">Category passed into the request</param>
        /// <returns>HttpResponseMessage to return to the client</returns>
        public async Task <HttpResponseMessage> Process(HttpRequest req,
                                                        CloudTable table,
                                                        ILogger log,
                                                        string identifier,
                                                        string category)
        {
            // Initialise variables
            HttpResponseMessage response = null;
            ResponseMessage     msg      = new ResponseMessage();

            // Create a dataservice object to use
            DataService ds = new DataService(table, this);

            // Using the method of the request determine if retrieving or upserting an item
            switch (req.Method)
            {
            case "GET":

                dynamic result;

                // attempt to get the data based on whether the identifier is set or not
                if (String.IsNullOrEmpty(identifier))
                {
                    log.LogInformation("All configs have been requested from the '{0}' category", category);

                    // no identifier has been supplied so retrieve all records
                    result = await ds.GetAll(category);
                }
                else
                {
                    log.LogInformation("Configuration item has been requested from the '{0}' category: {1}", category, identifier);

                    result = await ds.Get(identifier, category);
                }

                // get the response from the datservice and setup the response for the client
                // check that the result is not null, if it is then set the response accordlingly
                msg = ds.GetResponseMessage();
                if (result == null)
                {
                    // set the properties on the message object
                    msg.SetError(String.Format("Item cannot be found: {0}", identifier), true, HttpStatusCode.NotFound);
                    response = msg.CreateResponse();
                }
                else
                {
                    response = msg.CreateResponse(result);
                }

                break;

            case "POST":
            case "PUT":

                // get the json data from the request
                string json = await new StreamReader(req.Body).ReadToEndAsync();

                // parse the json
                Parse(json);

                // Determine if there are any errors
                msg = GetResponseMessage();
                if (msg.IsError())
                {
                    response = msg.CreateResponse();
                }
                else
                {
                    // attempt to insert the data
                    bool status = await ds.Insert();
                }
                break;

            case "DELETE":

                // attempt to remove the item from the data store
                result = await ds.Delete(identifier, category);

                msg      = ds.GetResponseMessage();
                response = msg.CreateResponse();

                break;
            }

            // Return the response
            return(response);
        }