public async Task <GtMetrics> Test(string url, int companyId) { GtMetricsPostResponce Post = PostTest(url); if (Post == null) { throw new Exception("Error posting a url to External api"); } GtMetrics Get = await GetTest(Post.poll_state_url, companyId); if (Get == null) { throw new Exception("Error getting gtmetric from external api"); } return(Get); }
public GtMetricsPostResponce PostTest(string url) { var client = new RestClient("https://gtmetrix.com/api/0.1/test"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Authorization", "Basic c3NjaG9vckBzbW9vdGhmdXNpb24uY29tOjA1YzYyNmE4OTEyMWQwZGM2MzY1Y2E4OTAwMDE0N2Zk"); request.AlwaysMultipartFormData = true; request.AddParameter("url", url); IRestResponse response = client.Execute(request); if (response.IsSuccessful) { string responseStream = response.Content; int index = responseStream.IndexOf(':'); string temp = responseStream.Remove(0, index + 1); index = temp.IndexOf(','); string creditsLeft = temp.Substring(0, index); index = temp.IndexOf(':'); temp = temp.Remove(0, index + 2); index = temp.IndexOf('\"'); string testId = temp.Substring(0, index); index = temp.IndexOf(':'); temp = temp.Remove(0, index + 2); index = temp.IndexOf('\"'); string pollStateUrl = temp.Substring(0, index); GtMetricsPostResponce deserializedResponse = new GtMetricsPostResponce { credits_left = int.Parse(creditsLeft), test_id = testId, poll_state_url = pollStateUrl }; return(deserializedResponse); } return(null); }