public async Task <GitResponse> GetRepository() { List <GitRepositoryDto> repositories = new List <GitRepositoryDto>(); RestClient restClient = new RestClient(_gitConfig.RepositoryUrl); RestRequest request = new RestRequest(Method.GET); TaskCompletionSource <IRestResponse> taskCompletion = new TaskCompletionSource <IRestResponse>(); RestRequestAsyncHandle handle = restClient.ExecuteAsync(request, r => taskCompletion.SetResult(r)); RestResponse response = (RestResponse)(await taskCompletion.Task); if (response.StatusCode == System.Net.HttpStatusCode.OK) { repositories = JsonConvert.DeserializeObject <List <GitRepositoryDto> >(response.Content); } GitResponse resp = new GitResponse() { Repositories = repositories, User = new UserInfo() { Email = _authConfig.UserName, UserName = _authConfig.Email } }; return(resp); }
public void UpsertResource(string resourcePath, GitResponse newResource) { if (!CachableResources.Contains(resourcePath)) { throw new InvalidOperationException($"Resource {resourcePath} cannot be cached"); } resourceCache.AddOrUpdate(resourcePath, newResource, (key, oldValue) => newResource); this.apiViewManager.UpdateView(resourcePath, newResource); }
public bool TryGetResourceFromCache(string resourcePath, out GitResponse resource) { if (!CachableResources.Contains(resourcePath)) { throw new InvalidOperationException($"Resource {resourcePath} cannot be cached"); } if (resourceCache.TryGetValue(resourcePath, out resource)) { return(true); } return(false); }
public void UpdateView(string resourcePath, GitResponse resourceValue) { // TODO: do not hardcode, also move this validation in a more generic base class if (resourcePath != "orgs/netflix/repos" || !(resourceValue is GitResponseMultiPage)) { // We support caching for results coming from this api, that are of type multipage (array of Jsons) return; } var multiPageResult = resourceValue as GitResponseMultiPage; UpdateForksView(multiPageResult); UpdateUpdatedTimeView(multiPageResult); UpdateIssuesView(multiPageResult); }
/// <summary> /// filter out extra files which are not required to be copied into history folder /// </summary> /// <param name="jSonString">complete json string receives from git</param> /// <returns></returns> public List <string> GetDesirePushedFiles(string jSonString) { filesToFilter = System.Configuration.ConfigurationManager.AppSettings["desiredFiles"].ToString().Split(',').ToList(); foldersToIgnore = System.Configuration.ConfigurationManager.AppSettings["ignoreFolder"].ToString().Split(',').ToList(); filesToIgnore = System.Configuration.ConfigurationManager.AppSettings["ignoreFile"].ToString().Split(',').ToList(); List <string> lstDesiredPushedFiles = new List <string>(); GitResponse _gitResponse = this.ReadJsonString(jSonString); foreach (GitResponseCommits commits in _gitResponse.Commits) { foreach (string commitedFile in commits.added) { GetFilteredFiles(lstDesiredPushedFiles, commitedFile); } foreach (string commitedFile in commits.modified) { GetFilteredFiles(lstDesiredPushedFiles, commitedFile); } } // TODO log push information eg. who pushed it, when pushed it, commit number etc return(lstDesiredPushedFiles); }
/// <summary> /// Forwards a GET request to the underlying endpoint at the relative URI location. /// Retry and Pagination are taken care of by the handler implementation /// </summary> /// <param name="resourceUri">The resource URI relative to the Base URI</param> public async Task <GitResponse> Get(string resourcePath) { var client = GetHttpClient(); GitResponse gitResponse = null; JToken mergedJsonResult = null; JToken currentParseResult = null; List <string> etags = new List <string>(); var isPaged = false; var pageIndex = 1; do { var nextPageResourcePath = this.GetPagedResourcePath(resourcePath, pageIndex); var response = await this.SendRequest(this.throttledTill, client, nextPageResourcePath); response.EnsureSuccessStatusCode(); var responseStream = await response.Content.ReadAsStreamAsync(); string json = new StreamReader(responseStream).ReadToEnd(); currentParseResult = JToken.Parse(json); if (response.Headers.TryGetValues("ETag", out IEnumerable <string> etag) && etag != null && etag.Any()) { etags.Add(etag.ElementAt(0)); } if (mergedJsonResult == null) { mergedJsonResult = currentParseResult; if (currentParseResult.GetType() == typeof(JArray)) { // This api could potentially return paginated results isPaged = true; } } else { ((JArray)mergedJsonResult).Merge((JArray)currentParseResult); } pageIndex++; }while (this.HasNext(currentParseResult)); if (isPaged) { gitResponse = new GitResponseMultiPage { JsonResult = mergedJsonResult, PageETags = etags }; } else { gitResponse = new GitResponseSinglePage { JsonResult = mergedJsonResult, ETag = etags[0] }; } return(gitResponse); }
/// <summary> /// Returns a flag indicating if the resource has changed /// </summary> /// <param name="resourceUri">The relative resource path for the resource to be verified</param> /// <returns>A flag indicating if the resource has changed</returns> public async Task <bool> HasChanged(string resourcePath, GitResponse oldResourceValue) { if (oldResourceValue == null) { return(true); } var hasChanged = false; List <string> etags; if (oldResourceValue is GitResponseSinglePage) { etags = new List <string> { ((GitResponseSinglePage)oldResourceValue).ETag }; } else { etags = ((GitResponseMultiPage)oldResourceValue).PageETags; } // Validate that none of the etags has changed var client = GetHttpClient(); var pageIndex = 1; foreach (var etag in etags) { client.DefaultRequestHeaders.Add("If-None-Match", etag); var nextResourcePage = GetPagedResourcePath(resourcePath, pageIndex); var response = await this.SendRequest(this.throttledTill, client, nextResourcePage); if (response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCode.NotModified) { // OK means that the resource has been modified if (response.StatusCode == System.Net.HttpStatusCode.OK) { hasChanged = true; break; } } else { throw new InvalidOperationException("Failed HTTP request"); } pageIndex++; } // If this is a paginated result, we also want to check if a new page has exists if (oldResourceValue is GitResponseMultiPage) { client = GetHttpClient(); // access the next page var lastPage = GetPagedResourcePath(resourcePath, pageIndex); var response = await this.SendRequest(this.throttledTill, client, lastPage); var responseStream = await response.Content.ReadAsStreamAsync(); string json = new StreamReader(responseStream).ReadToEnd(); var lastPageResult = JToken.Parse(json); // If we found a new extra page, then we deem the resource as changed and refresh everything // TODO: We could optimize and refresh only those pages that have changed return(lastPageResult == null || lastPageResult.HasValues); } return(hasChanged); }
private static FlowResponse ParseGitResponse(GitResponse response) { return(!response.Success ? new FlowResponse(false, EnumFlowResponse.GIT_ERROR, response.Response, response.Message) : new FlowResponse(true, EnumFlowResponse.GIT_ERROR, response.Response, response.Message)); }