using Newtonsoft.Json;
using RetailCrm.Exceptions;
using RetailCrm.Extra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RetailCrm.Response
{
///
/// Response from retailCRM API
///
public class ApiResponse
{
///
/// HTTP response status code
///
protected int statusCode;
///
/// Response
///
protected Dictionary response;
///
/// Creating ApiResponse
///
///
///
public ApiResponse(int statusCode, string responseBody = null)
{
this.statusCode = statusCode;
if (responseBody != null && responseBody.Length > 0)
{
Dictionary response = new Dictionary();
try
{
response = Tools.jsonDecode(responseBody);
}
catch (JsonReaderException e)
{
throw new InvalidJsonException("Invalid JSON in the API response body. " + e.ToString());
}
this.response = response;
}
}
///
/// Return HTTP response status code
///
/// int
public int getStatusCode()
{
return this.statusCode;
}
///
/// HTTP request was successful
///
/// boolean
public bool isSuccessful()
{
return this.statusCode < 400;
}
///
/// Return response
///
/// Dictionary
public object this[string code]
{
get {
if (this.response.ContainsKey(code))
{
return this.response[code];
}
else
{
return new Dictionary();
}
}
set
{
throw new ArgumentException("Property \"" + code + "\" is not writable");
}
}
}
}