Skip to content

Instantly share code, notes, and snippets.

@rajesh-smartwebtech
Last active March 5, 2022 05:40
Show Gist options
  • Save rajesh-smartwebtech/c8b3e3e3ce9ba749e463045e545132d1 to your computer and use it in GitHub Desktop.
Save rajesh-smartwebtech/c8b3e3e3ce9ba749e463045e545132d1 to your computer and use it in GitHub Desktop.
RestShart easy function to multiple request
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using System.Web;
using RestSharp;
using System.Reflection;
namespace Api
{
public enum FormReqType
{
FormData = 1,
xWWWFormUrlEncoded = 2,
Payload = 3,
None = 4
}
public class ResponseResult
{
public HttpStatusCode StatusCode { get; set; }
public String StatusDescription { get; set; }
public String ResponseText { get; set; }
public void UpdateOtherObject(dynamic obj )
{
obj.ResponseText = this.ResponseText;
obj.StatusCode = this.StatusCode;
obj.StatusDescription = this.StatusDescription;
}
}
public class ResponseResult<T>
{
public HttpStatusCode StatusCode { get; set; }
public String StatusDescription { get; set; }
public String ResponseText { get; set; }
public T parseJson
{
get
{
return JsonConvert.DeserializeObject<T>(this.ResponseText);
}
}
}
public class Request
{
private static int DefaultTimeOut = 100000; // 100000 Default (1000* 2) //2 seconds
public static string ApiBaseUrl = "API BASE URL";
protected static String DictionaryToString(Dictionary<String, Object> parameter)
{
List<String> postData = new List<string>();
foreach (string key in parameter.Keys)
{
var pairs = HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(parameter[key].ToString());
postData.Add(pairs);
}
return String.Join("&", postData.ToArray());
}
protected static ResponseResult APICall(String Url, Method method, Object myObject, FormReqType formType = FormReqType.FormData, string payloadContentType = "")
{
Dictionary<String, Object> parameter = new Dictionary<string, object>();
Type myType = myObject.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
{
object propValue = prop.GetValue(myObject, null);
parameter.Add(prop.Name, propValue);
}
return APICall(Url,method, parameter, formType, payloadContentType);
}
protected static ResponseResult APICall(String Url, Method method, Dictionary<String, Object> parameter, FormReqType formType = FormReqType.FormData, string payloadContentType = "")
{
var res = new ResponseResult();
var client = new RestClient();
client.Timeout = DefaultTimeOut;
var request = new RestRequest(Url, method);
//request.Parameters.Clear();
if (formType == FormReqType.FormData)
{
foreach (var item in parameter)
{
request.AddParameter(item.Key, item.Value);
}
}
else if (formType == FormReqType.xWWWFormUrlEncoded)
{
string postDataString = DictionaryToString(parameter);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", postDataString);
//request.AddParameter("application/x-www-form-urlencoded", "customer=Data00");
}
else if (formType == FormReqType.Payload)
{
string Content = JsonConvert.SerializeObject(parameter);
request.AddHeader("content-type", payloadContentType);
request.Parameters.Clear();
//var data = Encoding.ASCII.GetBytes(Content);
request.AddParameter(payloadContentType, Content, ParameterType.RequestBody);
}
else if (formType == FormReqType.None)
{
}
try
{
IRestResponse response = client.Execute(request);
res.StatusCode = response.StatusCode;
res.StatusDescription = response.StatusDescription;
string json = string.Empty;
if (response.StatusCode == HttpStatusCode.OK)
{
json = response.Content;
}
res.ResponseText = json;
}
catch (Exception err)
{
res.StatusCode = HttpStatusCode.NotFound;
res.StatusDescription = err.Message;
}
return res;
}
public static ResponseResult<Model> getStatus()
{
ResponseResult< Model> res = new ResponseResult<Model>();
string url = $"{ApiBaseUrl}/status/null";
Dictionary<String, Object> parameter = new Dictionary<string, object>();
parameter.Add("email", "");
parameter.Add("password", "");
var res1 = APICall(url, Method.POST, parameter, FormReqType.Payload, "application/json");
res1.UpdateOtherObject( res );
return res;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment