/// <summary> /// Executes an Asana Request /// </summary> /// <typeparam name="T">The Asana object type to deserialize as.</typeparam> /// <param name="request">The Asana Request to execute.</param> /// <returns>Response from Asana API deserialized as the supplied type.</returns> internal T Execute <T>(Asana.Client client) where T : new() { var startTime = DateTime.Now; var response = client.restClient.Execute(this.restRequest); this.timeToComplete = DateTime.Now - startTime; Console.WriteLine("Asana [" + this.restRequest.Method.ToString() + "] request to [" + this.restRequest.Resource + "] took " + timeToComplete.TotalMilliseconds.ToString() + "ms"); /// For legibility and future-proofing, deserialisation is de-coupled from the execution of the request /// Currently, they are daisy-chained but they might be decoupled at a later date if required. /// Note the JsonTokenOverride is supplied here, but globally set in the Client class return(Deserialize <T>(response, client.JsonTokenOverride)); }
/// <summary> /// Construct an Asana request from a supplied client, web method and resource targeted. /// </summary> /// <param name="client">The Asana client, required for authentication.</param> /// <param name="method">The method to use. Ex : Method.GET, Method.POST, etc.</param> /// <param name="resource">(optional) The URL fragment for the resource targeted. Ex : "tasks/". /// Note : does not require leading slash.</param> internal AsanaRequest(Asana.Client client, Method method, string resource = null) { /// The following sets the correct communication protocol, required as Asana API uses https. /// Otherwise, any request to API fail, irrespective of using RestSharp or System.Web ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; this.restRequest = new RestRequest(); this.restRequest.AddHeader("Authorization", client.Token); this.restRequest.AddHeader("content-type", "application/x-www-form-urlencoded"); /// The following headers are not required by useful. /// Asana-Fast-Api uses the beta version of the new (2017) Asana API, designed to be faster /// cache-control encourages system not to cache requests and fetch new results every time /// DynAsana-token is used by the OAuth authentication flow to verify the authentication request came from the DynAsana library. this.restRequest.AddHeader("Asana-Fast-Api", "true"); this.restRequest.AddHeader("cache-control", "no-cache"); this.restRequest.AddHeader("DynAsana-token", Assembly.GetExecutingAssembly().GetType().GUID.ToString()); /// The properties below point each request to the appropriate (supplied) endpoint /// This abstraction allows re-use of the AsanaRequest class for all endpoints and all methods (GET, POST, etc). this.restRequest.Resource = resource; this.restRequest.Method = method; }