반응형

    

    using RestSharp;
    using RestSharp.Authenticators;

    namespace User.Model
    {
        
        public class RestCalling
        {
            public string uri { get; set; }
            public string username { get; set; }
            public string password { get; set; }
            public string endPoint { get; set; } //"users/" + userId + "/identities.json"
            public string methodType { get; set; } //PUT, GET, POST, DELETE.

            //GET & Put & Post & Delete
            public string RestClientComm()
            {
                var _client = new RestClient(uri);
                IRestResponse result = null;
                _client.Authenticator = new HttpBasicAuthenticator(username: username, password: password);
                var request = new RestRequest(resource: endPoint, DataFormat.Json);


                switch (methodType)
                {
                    case "PUT": 
                        request.AddParameter("application/json", "\"identity\": {\"value\": \"" + "email@emailAddress" + "\"}}", ParameterType.RequestBody);
                        result = _client.Put(request);
                        break;

                    case "DELETE": 
                        result = _client.Delete(request);
                        break;

                    case "POST": 
                        request.AddParameter("application/json", "{\"identity\": {\"type\": \"email\", \"value\": \"" + "email@emailAddress"+ "\"}", ParameterType.RequestBody);
                        result = _client.Post(request);
                        break;

                    default: //GET
                        result = _client.Get(request);
                        break;

                }

                return result.Content;

            }
        }
    }

반응형

+ Recent posts