예제 #1
0
        public static T GetAuthentication <T>(string baseUrl, string url, Method httpMethod
                                              , string userName, string password, OAuthHeader oAuthHeader = null)
        {
            var    client      = new RestClient(baseUrl);
            var    request     = new RestRequest(url, Method.POST);
            string encodedBody = string.Format("username={0}&password={1}&grant_type={2}",
                                               userName, password, "password");

            request.AddParameter("application/x-www-form-urlencoded", encodedBody, ParameterType.RequestBody);
            request.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader);
            var response = client.Execute(request);

            return(JsonConvert.DeserializeObject <T>(response.Content));
        }
예제 #2
0
 public void SetAuthHeader(string token)
 {
     ApiHeader = new OAuthHeader(token);
 }
예제 #3
0
 protected BaseApiConsumer(string token)
 {
     ApiHeader = new OAuthHeader(token);
 }
예제 #4
0
 public static T InvokeApi <T>(string baseUrl, string url, Method httpMethod, OAuthHeader oAuthHeader = null,
                               object body = null)
 {
     return(InvokeApiAsync <T>(baseUrl, url, httpMethod, oAuthHeader, body));
 }
예제 #5
0
        public static T InvokeApiAsync <T>(string baseUrl, string url, Method httpMethod, OAuthHeader oAuthHeader = null,
                                           object body = null)
        {
            var client = new RestClient(baseUrl);

            var request = new RestRequest(url, httpMethod);


            ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };
            if (oAuthHeader != null)
            {
                foreach (var header in oAuthHeader.GetHeaders())
                {
                    request.AddHeader(header.Key, header.Value);
                }
            }
            else
            {
                var userCredentailsKeyValue = body as ApiTokenObject;
                if (userCredentailsKeyValue != null)
                {
                    client.Authenticator = new HttpBasicAuthenticator(userCredentailsKeyValue.username,
                                                                      userCredentailsKeyValue.password);
                }
            }

            if (SessionHelper.UserEntity != null)
            {
                request.AddHeader("Id_User", SessionHelper.UserEntity.Id_User.ToString());
            }

            if ((httpMethod == Method.POST || httpMethod == Method.PUT || httpMethod == Method.DELETE) && body != null)
            {
                request.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader);
                request.AddParameter("application/json", JsonConvert.SerializeObject(body), ParameterType.RequestBody);
                //request.AddBody(body);
            }


            var response = client.Execute(request);

            return(JsonConvert.DeserializeObject <T>(response.Content));
        }