Esempio n. 1
0
        /// <summary>
        /// Update an entity into table
        /// </summary>
        /// <param name="table">Table name</param>
        /// <param name="entity">Entity object</param>
        /// <param name="noscript">NoScript flag</param>
        /// <returns>JSON string object result</returns>
        internal string Update(string table, IMobileServiceEntity entity, bool noscript = false)
        {
            // fill body
            this.body.Clear();
            this.body.Append(entity.ToJson());

            // build URI
            this.uri.Clear();
            this.uri.Append(this.applicationUri.AbsoluteUri)
            .Append("tables/")
            .Append(table)
            .Append("/")
            .Append(entity.Id);

            if (noscript)
            {
                if (this.masterKey == null)
                {
                    throw new ArgumentException("For noscript you must also supply the service master key");
                }
                this.uri.Append("?noscript=true");
            }

            this.httpRequest.Uri           = new Uri(this.uri.ToString());
            this.httpRequest.Method        = HttpMethod.Patch;
            this.httpRequest.Body          = Encoding.UTF8.GetBytes(body.ToString());
            this.httpRequest.ContentLength = this.httpRequest.Body.Length;

            HttpResponse httpResp = this.httpClient.Send(this.httpRequest);

            return(httpResp.GetBodyAsString());
        }
Esempio n. 2
0
 public string Update(IMobileServiceEntity entity, bool noscript = false)
 {
     return(this.Client.Update(this.TableName, entity, noscript));
 }
        /// <summary>
        /// Insert a new object into a table.
        /// </summary>
        /// <param name="instance">
        /// The instance to insert into the table.
        /// </param>
        /// <returns>
        /// A task that will complete when the insert finishes.
        /// </returns>
        internal string SendInsert(IMobileServiceEntity instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            
            // Make sure the instance doesn't have its ID set for an insertion
            if (instance.Id > 0)
            {
                throw new ArgumentException(                   
                    //TODO:NH use regex to implement format as an extension method on string
                    Resources.CannotInsertWithExistingIdMessage + IdPropertyName, "instance");
                    /*string.Format(
                        CultureInfo.InvariantCulture,
                        Resources.CannotInsertWithExistingIdMessage,
                        IdPropertyName),
                    "instance");*/
            }

            string url = GetUriFragment(this.TableName);            
         
            //JToken response = await this.MobileServiceClient.RequestAsync("POST", url, instance);
            var responseJson = this.MobileServiceClient.Request("POST", url, instance);

            //TODO: do only if worthwhile as will have mem+perf hit.. i think most apps just want to offload the data..
            //var result = JSONSerializer.Deserialize(responseJson, instance.GetType());                        
            //JToken patched = Patch(instance, response);                            
            //return patched;

            return responseJson;
        }
        /// <summary>
        /// Perform a web request and include the standard Mobile Services
        /// headers.
        /// </summary>
        /// <param name="method">
        /// The HTTP method used to request the resource.
        /// </param>
        /// <param name="uriFragment">
        /// URI of the resource to request (relative to the Mobile Services
        /// runtime).
        /// </param>
        /// <param name="entity">
        /// Optional content to send to the resource.
        /// </param>
        /// <returns>The JSON value of the response.</returns>
        //internal async Task<JToken> RequestAsync(string method, string uriFragment, JToken content)
        internal string Request(string method, string uriFragment, IMobileServiceEntity entity)
        {
            Debug.Assert(!method.IsNullOrEmpty(), "method cannot be null or empty!");
            Debug.Assert(!uriFragment.IsNullOrEmpty(), "uriFragment cannot be null or empty!");

            string jsonResult = null;

            // Create the web request            
            //IServiceFilterRequest request = new ServiceFilterRequest();
            var uri = new Uri(this.ApplicationUri.AbsoluteUri + uriFragment);
            using(HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest)
            {
                request.Method = method.ToUpper();
                request.Accept = RequestJsonContentType;

                // Set Mobile Services authentication, application, and telemetry
                // headers
                request.Headers.Add(RequestInstallationIdHeader, applicationInstallationId);
                if (!this.ApplicationKey.IsNullOrEmpty())
                {
                    request.Headers.Add(RequestApplicationKeyHeader, this.ApplicationKey);
                }

                if (!this.currentUserAuthenticationToken.IsNullOrEmpty())
                {
                    request.Headers.Add(RequestAuthenticationHeader, this.currentUserAuthenticationToken);
                }

                // Add any request as JSON
                if (entity != null)
                {
                    var content = MobileServicesTableSerializer.Serialize(entity);

                    request.ContentType = RequestJsonContentType;
                    request.ContentLength = Encoding.UTF8.GetBytes(content).Length;
                    request.UserAgent = "Micro Framework";

                    try
                    {
                        using (var requestStream = request.GetRequestStream())
                        using (var streamWriter = new StreamWriter(requestStream))
                        {
                            streamWriter.Write(content);                            
                        }

                        using (var response = (HttpWebResponse)request.GetResponse())
                        using (var streamReader = new StreamReader(response.GetResponseStream()))
                        {                            
                            if ((int)response.StatusCode >= 400)
                            {
                                //TODO: NH implement
                                // ThrowInvalidResponse(request, response, body);
                                throw new ApplicationException("Status Code: " + response.StatusCode);
                            }
                           
                             jsonResult = streamReader.ReadToEnd();
                           // result = GetResponseJson(json);                          
                        }
                    }
                    catch (WebException)
                    {
                        //TODO: handle web ex
                    }
                }
      
                return jsonResult;//TODO NH: not yet implemented implement w/ JSON deserialize support + patch
            }
        } 
Esempio n. 5
0
        /// <summary>
        /// Update an entity into table
        /// </summary>
        /// <param name="table">Table name</param>
        /// <param name="entity">Entity object</param>
        /// <param name="noscript">NoScript flag</param>
        /// <returns>JSON string object result</returns>
        internal string Update(string table, IMobileServiceEntity entity, bool noscript = false)
        {
            // fill body
            this.body.Clear();
            this.body.Append(entity.ToJson());

            // build URI
            this.uri.Clear();
            this.uri.Append(this.applicationUri.AbsoluteUri)
                .Append("tables/")
                .Append(table)
                .Append("/")
                .Append(entity.Id);

            if (noscript)
            {
                if (this.masterKey == null)
                    throw new ArgumentException("For noscript you must also supply the service master key");
                this.uri.Append("?noscript=true");
            }

            this.httpRequest.Uri = new Uri(this.uri.ToString());
            this.httpRequest.ContentLength = body.Length;
            this.httpRequest.Method = HttpMethod.Patch;

            HttpResponse httpResp = this.httpClient.Send(this.httpRequest);

            return body.ToString();
        }
 /// <summary>
 /// Insert a new object into a table.
 /// </summary>
 /// <param name="instance">
 /// The instance to insert into the table.
 /// </param>
 /// <returns>
 /// A task that will complete when the insert finishes.
 /// </returns>
 //TODO: return type to be updated once json deserializer in place
 public string Insert(IMobileServiceEntity instance)
 {
     return this.SendInsert(instance);
 }
Esempio n. 7
0
 public string Update(IMobileServiceEntity entity, bool noscript = false)
 {
     return this.Client.Update(this.TableName, entity, noscript);
 }