/// <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() { Uri uri = new Uri(TokenRequestEndpointUrl); m_restClient.BaseUrl = uri; var request = new RestRequest(Method.POST); request.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 DynamicJsonDeserializer(); var responseData = deserializer.Deserialize <dynamic>(response); if (isAuthenticated) { return(new AuthenticationInfo(responseData.access_token.Value, responseData.instance_url.Value)); } else { throw new SalesforceException(responseData.error.Value, responseData.error_description.Value); } }
public Task AlgorithmUpdated(string json, int id) { var hero = game.GetHeroById(id); hero.Algorithm = DynamicJsonDeserializer.ToBehaviourAlgorithm(json, hero); return(Task.CompletedTask); }
/// <summary> /// Initializes a new instance of the <see cref="SalesforceClient"/> class. /// </summary> /// <param name="restClient">The rest client.</param> protected internal SalesforceClient(IRestClient restClient) { m_restClient = restClient; ApiVersion = "v28.0"; m_deserializer = new DynamicJsonDeserializer(); genericJsonDeserializer = new GenericJsonDeserializer(new SalesforceContractResolver(false)); updateJsonSerializer = new GenericJsonSerializer(new SalesforceContractResolver(true)); }
/// <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() { Uri uri = new Uri(TokenRequestEndpointUrl); m_restClient.BaseUrl = uri; var request = new RestRequest(Method.POST); request.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 DynamicJsonDeserializer(); var responseData = deserializer.Deserialize<dynamic>(response); if (isAuthenticated) { return new AuthenticationInfo(responseData["access_token"].Value, responseData["instance_url"].Value); } else { throw new SalesforceException(responseData.error.Value, responseData.error_description.Value); } }
/// <summary> /// Private constructor /// Performs base setup /// </summary> private SugarClient() { client = new RestClient(); client.UserAgent = "SugarSharp"; dynamicSerializer = new DynamicJsonDeserializer(); }
public void DynamicDeserializationTest() { var h = new Hero(0, 0, 0, null, null); string json; BehaviourAlgorithm algorithm; json = "[{\"type\":\"ShootElement\", \"isActive\":\"true\"}]"; algorithm = DynamicJsonDeserializer.ToBehaviourAlgorithm(json, h); Assert.NotNull(algorithm); CollectionAssert.IsNotEmpty(algorithm.CodeElements); CollectionAssert.AllItemsAreNotNull(algorithm.CodeElements); Assert.AreEqual(1, algorithm.CodeElements.Count); Assert.IsInstanceOf <ShootElement>(algorithm.CodeElements[0]); json = "[{\"type\":\"CodeBlockElement\", \"isActive\":\"true\", \"elements\":[{\"type\":\"ShootElement\", \"isActive\":\"true\"}, {\"type\":\"IdleElement\", \"isActive\":\"true\"}]}]"; algorithm = DynamicJsonDeserializer.ToBehaviourAlgorithm(json, h); Assert.NotNull(algorithm); CollectionAssert.IsNotEmpty(algorithm.CodeElements); CollectionAssert.AllItemsAreNotNull(algorithm.CodeElements); Assert.AreEqual(1, algorithm.CodeElements.Count); Assert.IsInstanceOf <CodeBlockElement>(algorithm.CodeElements[0]); { var block = algorithm.CodeElements[0] as CodeBlockElement; Assert.NotNull(block); Assert.NotNull(block.CodeElements); CollectionAssert.IsNotEmpty(block.CodeElements); CollectionAssert.AllItemsAreNotNull(block.CodeElements); Assert.AreEqual(2, block.CodeElements.Count); Assert.IsInstanceOf <ShootElement>(block.CodeElements.ElementAt(0)); Assert.IsInstanceOf <IdleElement>(block.CodeElements.ElementAt(1)); } json = "[" + "{\"type\":\"ShootElement\", \"isActive\":\"true\"}, " + "{\"type\":\"CodeBlockElement\", \"isActive\":\"true\", \"elements\":[{\"type\":\"ShootElement\", \"isActive\":\"true\"}]}," + "{\"type\":\"BranchingElement\", \"isActive\":\"true\", " + "\"cond\":{\"type\":\"IsEnemyNearCondition\", \"isActive\":\"true\"}, " + "\"then\":{\"type\":\"CodeBlockElement\", \"isActive\":\"true\", \"elements\":[{\"type\":\"ShootElement\", \"isActive\":\"true\"}, {\"type\":\"IdleElement\", \"isActive\":\"true\"}]}, " + "\"else\":{\"type\":\"CodeBlockElement\", \"isActive\":\"true\", \"elements\":[{\"type\":\"ShootElement\", \"isActive\":\"true\"}, {\"type\":\"IdleElement\", \"isActive\":\"true\"}]}" + "}" + "]"; algorithm = DynamicJsonDeserializer.ToBehaviourAlgorithm(json, null); Assert.NotNull(algorithm); CollectionAssert.IsNotEmpty(algorithm.CodeElements); CollectionAssert.AllItemsAreNotNull(algorithm.CodeElements); Assert.AreEqual(3, algorithm.CodeElements.Count); Assert.IsInstanceOf <ShootElement>(algorithm.CodeElements[0]); Assert.IsInstanceOf <CodeBlockElement>(algorithm.CodeElements[1]); { var block = algorithm.CodeElements[1] as CodeBlockElement; Assert.NotNull(block); Assert.NotNull(block.CodeElements); CollectionAssert.IsNotEmpty(block.CodeElements); CollectionAssert.AllItemsAreNotNull(block.CodeElements); Assert.AreEqual(1, block.CodeElements.Count); Assert.IsInstanceOf <ShootElement>(block.CodeElements.First()); } Assert.IsInstanceOf <BranchingElement>(algorithm.CodeElements[2]); { var branching = algorithm.CodeElements[2] as BranchingElement; Assert.NotNull(branching); Assert.NotNull(branching.Condition); Assert.NotNull(branching.ThenBlock); Assert.NotNull(branching.ElseBlock); CollectionAssert.IsNotEmpty(branching.ThenBlock.CodeElements); CollectionAssert.IsNotEmpty(branching.ElseBlock.CodeElements); CollectionAssert.AllItemsAreNotNull(branching.ThenBlock.CodeElements); CollectionAssert.AllItemsAreNotNull(branching.ElseBlock.CodeElements); Assert.AreEqual(2, branching.ThenBlock.CodeElements.Count); Assert.AreEqual(2, branching.ElseBlock.CodeElements.Count); Assert.IsInstanceOf <IsEnemyNearCondition>(branching.Condition); Assert.IsInstanceOf <ShootElement>(branching.ThenBlock.CodeElements.ElementAt(0)); Assert.IsInstanceOf <IdleElement>(branching.ThenBlock.CodeElements.ElementAt(1)); Assert.IsInstanceOf <ShootElement>(branching.ElseBlock.CodeElements.ElementAt(0)); Assert.IsInstanceOf <IdleElement>(branching.ElseBlock.CodeElements.ElementAt(1)); } }