Exemplo n.º 1
0
        /// <summary>
        /// Executes a SOQL query and returns the result.
        /// </summary>
        /// <param name="query">The SOQL query.</param>
        /// <param name="action">Action to call after getting a non error response.</param>
        /// <param name="altUrl">The url to use without the instance url</param>
        /// <returns>The API result for the query.</returns>
        public IList <T> QueryActionBatch <T>(string query, Action <IList <T> > action, string altUrl = "") where T : new()
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            ExceptionHelper.ThrowIfNullOrEmpty("query", query);

            var escapedQuery = query.UrlEncode();

            var url = "{0}?q={1}".With(string.IsNullOrEmpty(altUrl) ? GetUrl("query") : GetAltUrl(altUrl), escapedQuery);

            var returns = new List <T>();
            IRestResponse <SalesforceQueryResult <T> > response = null;

            do
            {
                if (response != null)
                {
                    url      = GetNextRecordsUrl(response);
                    response = null;
                }

                if (string.IsNullOrEmpty(url))
                {
                    break;
                }

                response = Request <SalesforceQueryResult <T> >(url);
                if (response == null || response.Data == null)
                {
                    continue;
                }

                if (!response.Data.Records.Any())
                {
                    continue;
                }
                try
                {
                    var customResponse = genericJsonDeserializer.Deserialize <SalesforceQueryResult <T> >(response);
                    if (customResponse == null)
                    {
                        continue;
                    }
                    action(customResponse.Records);
                    returns.AddRange(customResponse.Records);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            } while (response != null && response.Data != null && !response.Data.Done && !string.IsNullOrEmpty(response.Data.NextRecordsUrl));

            return(returns);
        }
        /// <summary>
        /// Authenticate in the Salesforce REST's API.
        /// </summary>
        /// <returns>
        /// The authentication info with access token and instance url for futher API calls.
        /// </returns>
        /// <remarks>
        /// If authentiaction fails an SalesforceException will be throw.
        /// </remarks>
        public AuthenticationInfo Authenticate()
        {
            if (ServicePointManager.SecurityProtocol != 0)
            {
                ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
            }
            Uri uri = new Uri(TokenRequestEndpointUrl);

            m_restClient.BaseUrl = uri;

            var request = new RestRequest(Method.POST)
            {
                RequestFormat = DataFormat.Json
            };

            request.AddParameter("grant_type", "password");
            request.AddParameter("client_id", m_clientId);
            request.AddParameter("client_secret", m_clientSecret);
            request.AddParameter("username", m_username);
            request.AddParameter("password", m_password);

            var response        = m_restClient.Post(request);
            var isAuthenticated = response.StatusCode == HttpStatusCode.OK;

            var deserializer = new GenericJsonDeserializer(new SalesforceContractResolver(false));
            var responseData = deserializer.Deserialize <dynamic>(response);

            if (responseData == null)
            {
                throw new SalesforceException(response.ErrorException.Message, response.ErrorMessage);
            }

            if (isAuthenticated)
            {
                return(new AuthenticationInfo(responseData.access_token.Value, responseData.instance_url.Value));
            }
            else
            {
                throw new SalesforceException(responseData.error.Value, responseData.error_description.Value);
            }
        }