public Schema Solve(Schema inputSchema, bool useMemoryCache) { string solveUrl; var pathType = GetPathType(); if (pathType == PathType.NonresponsiveUrl) { return(null); } if (pathType == PathType.GrasshopperDefinition || pathType == PathType.ComponentGuid) { solveUrl = Servers.GetSolveUrl(); if (!string.IsNullOrEmpty(_cacheKey)) { inputSchema.Pointer = _cacheKey; } } else { int index = Path.LastIndexOf('/'); solveUrl = Path.Substring(0, index + 1) + "solve"; } string inputJson = JsonConvert.SerializeObject(inputSchema); if (useMemoryCache && inputSchema.Algo == null) { var cachedResults = Hops.MemoryCache.Get(inputJson); if (cachedResults != null) { return(cachedResults); } } using (var content = new System.Net.Http.StringContent(inputJson, Encoding.UTF8, "application/json")) { var postTask = HttpClient.PostAsync(solveUrl, content); var responseMessage = postTask.Result; if (responseMessage.StatusCode == System.Net.HttpStatusCode.InternalServerError) { bool fileExists = File.Exists(Path); if (fileExists && string.IsNullOrEmpty(inputSchema.Algo)) { var bytes = System.IO.File.ReadAllBytes(Path); string base64 = Convert.ToBase64String(bytes); inputSchema.Algo = base64; inputJson = JsonConvert.SerializeObject(inputSchema); var content2 = new System.Net.Http.StringContent(inputJson, Encoding.UTF8, "application/json"); postTask = HttpClient.PostAsync(solveUrl, content2); responseMessage = postTask.Result; if (responseMessage.StatusCode == System.Net.HttpStatusCode.InternalServerError) { var badSchema = new Schema(); badSchema.Errors.Add("Unable to solve on compute"); return(badSchema); } } else { if (!fileExists && string.IsNullOrEmpty(inputSchema.Algo) && GetPathType() == PathType.GrasshopperDefinition) { var badSchema = new Schema(); badSchema.Errors.Add($"Unable to find file: {Path}"); return(badSchema); } } } var remoteSolvedData = responseMessage.Content; var stringResult = remoteSolvedData.ReadAsStringAsync().Result; var schema = JsonConvert.DeserializeObject <Resthopper.IO.Schema>(stringResult); if (useMemoryCache && inputSchema.Algo == null) { Hops.MemoryCache.Set(inputJson, schema); } _cacheKey = schema.Pointer; return(schema); } }
void GetRemoteDescription() { bool performPost = false; string address = null; var pathType = GetPathType(); switch (pathType) { case PathType.GrasshopperDefinition: { if (Path.StartsWith("http", StringComparison.OrdinalIgnoreCase) || File.Exists(Path)) { address = Path; performPost = true; } } break; case PathType.ComponentGuid: address = Servers.GetDescriptionUrl(Guid.Parse(Path)); break; case PathType.Server: address = Path; break; case PathType.NonresponsiveUrl: break; } if (address == null) { return; } IoResponseSchema responseSchema = null; System.Threading.Tasks.Task <System.Net.Http.HttpResponseMessage> responseTask = null; IDisposable contentToDispose = null; if (performPost) { string postUrl = Servers.GetDescriptionPostUrl(); var schema = new Schema(); if (Path.StartsWith("http", StringComparison.OrdinalIgnoreCase)) { schema.Pointer = address; } else { var bytes = System.IO.File.ReadAllBytes(address); schema.Algo = Convert.ToBase64String(bytes); } string inputJson = JsonConvert.SerializeObject(schema); var content = new System.Net.Http.StringContent(inputJson, Encoding.UTF8, "application/json"); responseTask = HttpClient.PostAsync(postUrl, content); contentToDispose = content; } else { responseTask = HttpClient.GetAsync(address); } if (responseTask != null) { var responseMessage = responseTask.Result; var remoteSolvedData = responseMessage.Content; var stringResult = remoteSolvedData.ReadAsStringAsync().Result; if (!string.IsNullOrEmpty(stringResult)) { responseSchema = JsonConvert.DeserializeObject <Resthopper.IO.IoResponseSchema>(stringResult); _cacheKey = responseSchema.CacheKey; } } if (contentToDispose != null) { contentToDispose.Dispose(); } if (responseSchema != null) { _description = responseSchema.Description; _customIcon = null; if (!string.IsNullOrWhiteSpace(responseSchema.Icon)) { try { byte[] bytes = Convert.FromBase64String(responseSchema.Icon); using (var ms = new MemoryStream(bytes)) { _customIcon = new System.Drawing.Bitmap(ms); } } catch (Exception) { } } _inputParams = new Dictionary <string, Tuple <InputParamSchema, IGH_Param> >(); _outputParams = new Dictionary <string, IGH_Param>(); foreach (var input in responseSchema.Inputs) { string inputParamName = input.Name; if (inputParamName.StartsWith("RH_IN:")) { var chunks = inputParamName.Split(new char[] { ':' }); inputParamName = chunks[chunks.Length - 1]; } _inputParams[inputParamName] = Tuple.Create(input, ParamFromIoResponseSchema(input)); } foreach (var output in responseSchema.Outputs) { string outputParamName = output.Name; if (outputParamName.StartsWith("RH_OUT:")) { var chunks = outputParamName.Split(new char[] { ':' }); outputParamName = chunks[chunks.Length - 1]; } _outputParams[outputParamName] = ParamFromIoResponseSchema(output); } } }