Exemple #1
0
        public void SignIn(RestRequest _request)
        {
            string _coreengine_id, _organization_id, _access_key, _secret_key;

            while (true)
            {
                TripleDESOAuth2TokenCryptor _token_cryptor = new TripleDESOAuth2TokenCryptor();
                ICoreEngineSettings         _core_settings = new CoreEngineSettings();
                _coreengine_id   = _core_settings.CoreEngineID;
                _organization_id = _core_settings.OrganizationID;
                _access_key      = _core_settings.AccessKey;
                _secret_key      = _core_settings.SecretKey;

                List <String> _auth_variables = new List <String>()
                {
                    "CoreEngineID", "OrganizationID", "AccessKey", "SecretKey"
                };
                var authorName = AttributeHelpers.GetPropValue(_core_settings, "CoreEngineID");
                if (_auth_variables.Any(x => AttributeHelpers.IsPropValueEmpty(_core_settings, x)))
                {
                    _logger.Error(string.Format("Missing authentication attributes {0}", string.Join(", ", _auth_variables.Where(x => AttributeHelpers.IsPropValueEmpty(_core_settings, x)))));
                    Thread.Sleep(TimeSpan.FromSeconds(10));
                }
                else
                {
                    break;
                }
            }
            IRestResponse _auth_response = _oculi_api.Authorization.Signin();

            OculiTokenFactory.StoreToken(OculiTokenFactory.SetToken(_auth_response));
        }
Exemple #2
0
        public IRestResponse PerformSimpleApiCall <T>(Method _method, Object _object, String _authentication) where T : new()
        {
            RestClient  _rest_client = new RestClient();
            RestRequest _request     = new RestRequest();

            RestClientFactory.Create(Resource, _method, _object, out _rest_client, out _request);
            switch (_authentication)
            {
            case "creds":
                _request = OculiTokenFactory.SetCredentialRequest(_logger, _oculi_api, _request);
                break;

            case "token":
                _request = OculiTokenFactory.SetTokenRequest(_request);
                break;

            default:
                throw new Exception("No authentication method specified");
            }
            return(_rest_client.Execute(_request));
        }
Exemple #3
0
        public object PerformApiCall <T>(Method _method, Object _object) where T : new()
        {
            RestClient  _rest_client = new RestClient();
            RestRequest _request     = new RestRequest();

            RestClientFactory.Create(Resource, _method, _object, out _rest_client, out _request);
            if (!OculiTokenFactory.IsTokenConfigured())
            {
                IOculiOAuth2 _signin = new OculiOAuth2(_oculi_api, _logger);
                _signin.SignIn(_request);
            }
            object responseobject = null;

            while (true)
            {
                _request = OculiTokenFactory.SetTokenRequest(_request);
                var restResponse = _rest_client.Execute(_request);
                if (typeof(T) == typeof(RestResponse))
                {
                    return(restResponse);
                }
                if (restResponse.StatusCode == HttpStatusCode.OK)
                {
                    try
                    {
                        responseobject = JsonConvert.DeserializeObject <T>(restResponse.Content, new JsonSerializerSettings
                        {
                            MissingMemberHandling = MissingMemberHandling.Ignore,
                            NullValueHandling     = NullValueHandling.Ignore,
                            Error = new OculiException().DeserializationExpection
                        });
                    }
                    catch (Exception ex)
                    {
                        //Logger.log(ex.ToString(), Logger.Severity.Error);
                        //Logger.log(JsonConvert.SerializeObject(_object), Logger.Severity.Error);
                        //Logger.log(restResponse.Content, Logger.Severity.Error);
                        throw new Exception(ex.GetBaseException().Message);
                    }
                    break;
                }
                else if (restResponse.StatusCode == HttpStatusCode.Unauthorized)
                {
                    IOculiOAuth2 _signin = new OculiOAuth2(_oculi_api, _logger);
                    _signin.SignIn(_request);
                    continue;
                }
                else if (restResponse.StatusCode == (HttpStatusCode)422)
                {
                    responseobject = JsonConvert.DeserializeObject <T>(restResponse.Content, new JsonSerializerSettings
                    {
                        MissingMemberHandling = MissingMemberHandling.Ignore,
                        NullValueHandling     = NullValueHandling.Ignore,
                        Error = new OculiException().DeserializationExpection
                    });
                    string _error_field_list = responseobject.GetType().GetProperty("errors") == null ? "no errors definition found" : String.Join(", ", ((List <String>)responseobject.GetType().GetProperty("errors").GetValue(responseobject, null)));
                    throw new Exception(String.Format("Error while processing portal transaction: {0}", _error_field_list));
                }

                else if (restResponse.StatusCode == HttpStatusCode.BadRequest)
                {
                    //ResultType _result = new ResultType();
                    try
                    {
                        //_result = JsonConvert.DeserializeObject<ResultType>(restResponse.Content);
                    }
                    catch (Exception ex)
                    {
                        //Logger.log(ex.ToString(), Logger.Severity.Error);
                        //Logger.log(restResponse.Content, Logger.Severity.Error);
                        //throw new Exception(_result.result.message.ToString());
                    }
                }
                else if (restResponse.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new Exception(String.Format("{0} {1}", restResponse.StatusCode, restResponse.ErrorMessage));
                }
                else if (restResponse.StatusCode == HttpStatusCode.RequestTimeout)
                {
                    //Logger.log(String.Format("Connection timeout to {0}", client.BuildUri(request).ToString()), Logger.Severity.Error);
                    Thread.Sleep(new TimeSpan(0, 0, 30));
                }
                else if (restResponse.StatusCode == 0)
                {
                    //Logger.log(String.Format("Unexpected error connecting to {0} with error ({1})", client.BuildUri(request).ToString(), restResponse.ErrorMessage), Logger.Severity.Error);

                    Thread.Sleep(new TimeSpan(0, 0, 30));
                }
                else
                {
                    //Logger.log(String.Format("Unexpected API error on {0} with error ({1})", client.BuildUri(request).ToString(), restResponse.ErrorMessage), Logger.Severity.Error);
                    throw new Exception(String.Format("{0} {1}", restResponse.StatusCode, restResponse.ErrorMessage));
                }
            }
            return(responseobject);
        }