コード例 #1
0
ファイル: ContactApi.cs プロジェクト: enokby/smsghapi-csharp
 /// <summary>
 ///     Updates a contact details based upon a contact object.
 /// </summary>
 /// <param name="data">The <see cref="Contact" /> object. </param>
 /// <returns>true when successful</returns>
 /// <exception cref="Exception">Exception with the appropriate message</exception>
 public bool UpdateContact(Contact data)
 {
     string resource = "/contacts/";
     const string contentType = "application/json";
     if (data == null) throw new Exception("Parameter 'data' cannot be null");
     resource += data.ContactId;
     var stringWriter = new StringWriter();
     new JsonSerializer().Serialize(stringWriter, data);
     HttpResponse response = RestClient.Put(resource, contentType, Encoding.UTF8.GetBytes(stringWriter.ToString()));
     if (response == null) throw new Exception("Request Failed. Unable to get server response");
     if (response.Status == Convert.ToInt32(HttpStatusCode.OK)) return true;
     string errorMessage = String.Format("Status Code={0}, Message={1}", response.Status, response.GetBodyAsString());
     throw new Exception("Request Failed : " + errorMessage);
 }
コード例 #2
0
ファイル: ContactApi.cs プロジェクト: enokby/smsghapi-csharp
        /// <summary>
        ///     Add a new contact. The contact object to add.
        /// </summary>
        /// <param name="contact">The <see cref="Contact" /> object.</param>
        /// <returns>A <see cref="Contact" />  object.</returns>
        /// <exception cref="Exception">Exception with the appropriate message</exception>
        public Contact AddContact(Contact contact)
        {
            const string resource = "/contacts/";
            const string contentType = "application/json";
            if (contact == null) throw new Exception("Parameter 'contact' cannot be null");
            var stringWriter = new StringWriter();
            new JsonSerializer().Serialize(stringWriter, contact);

            HttpResponse response = RestClient.Post(resource, contentType, Encoding.UTF8.GetBytes(stringWriter.ToString()));
            if (response == null) throw new Exception("Request Failed. Unable to get server response");
            if (response.Status == Convert.ToInt32(HttpStatusCode.Created)) return new Contact(JsonConvert.DeserializeObject<ApiDictionary>(response.GetBodyAsString()));
            string errorMessage = String.Format("Status Code={0}, Message={1}", response.Status, response.GetBodyAsString());
            throw new Exception("Request Failed : " + errorMessage);
        }