// makes a request to the specified URL using the given access token // and parses the JSON response into a FileList object public async Task <FileList> CallWebApiFileListAndProcessResultAsync(string webApiUrl, string accessToken) { if (string.IsNullOrEmpty(accessToken)) { throw new Exception($"Invalid access token"); } HttpResponseMessage response = await MakeHttpRequestAsync(webApiUrl, accessToken); if (response.IsSuccessStatusCode) { FileList list = new FileList(); string json = await response.Content.ReadAsStringAsync(); // convert entire json response to object JObject result = JObject.Parse(json); // convert useful part of json reponse to array of file data JArray children = (JArray)result["value"]; // for each file for (int i = 0; i < children.Count; ++i) { JObject child = (JObject)children[i]; // make sure child is not actually a folder if (!child.ContainsKey("folder")) { // pull each required piece of data string name = (string)child["name"]; string id = (string)child["id"]; string type = (string)child["file"]["mimeType"]; string eTag = (string)child["eTag"]; long size = (long)child["size"]; // process result into file list object list.AddLine(name, id, type, eTag, size); } } return(list); } // throw an exception if the response has a failure status code throw new Exception($"File list API call failed: {response.StatusCode}"); }
// filters a FileList object down to only files that do exist in the registry // by looping through the File objects in the FileList and calling // Eth function IsRegisteredFileId on each to determine whether to add it to the new filtered FileList public async Task <FileList> OnlyRegistered(string registryAddress, FileList list) { FileList filtered = new FileList(); // for each file for (int i = 0; i < list.NumFiles; ++i) { // check if the file has been registered bool registered = await IsRegisteredFileId.SendRequestAsync(MyWeb3, registryAddress, list.Files[i].FileId); // if it has been registered, add to new list if (registered) { filtered.AddLine(list.Files[i].Name, list.Files[i].FileId, list.Files[i].Type, list.Files[i].Etag, list.Files[i].Size); } } return(filtered); }