private static void AddVariables(RestmanSpec spec, string variableSet, Dictionary <string, string> variables) { var set = spec.variableSets[variableSet]; foreach (var pair in set) { variables[pair.Key] = pair.Value; } }
public static async Task <string> RunRequest(RestmanSpec spec, string requestName, IReadOnlyList <string> variableSets, IReadOnlyDictionary <string, string> variableOverrides, IReadOnlyList <string> orderedVariables, string bifoqlQuery, string body) { var variables = new Dictionary <string, string>(); if (spec.variableSets != null && spec.variableSets.ContainsKey("default")) { AddVariables(spec, "default", variables); } requestName = requestName ?? "default"; var request = spec.requests[requestName]; AddOrdinalVariables(request, orderedVariables, variables); foreach (var pair in variableOverrides) { variables[pair.Key] = pair.Value; } var url = SubstituteTokens(request.url, variables); var client = new HttpClient(); var message = new HttpRequestMessage(); message.RequestUri = new Uri(url); message.Method = new HttpMethod(request.method ?? "GET"); if (request.headers != null) { foreach (var pair in request.headers) { message.Headers.Add(SubstituteTokens(pair.Key, variables), SubstituteTokens(pair.Value, variables)); } } // If body was supplied on the command line, it will replace any body defined in the request spec. if (body != null) { message.Content = new StringContent(body, Encoding.UTF8, "application/json"); } else if (request.body != null) { message.Content = new StringContent(request.body, Encoding.UTF8, "application/json"); } var response = await client.SendAsync(message); if (response.StatusCode != HttpStatusCode.OK) { var builder = new System.Text.StringBuilder(); builder.AppendLine("ERROR " + response.StatusCode); var rawContent = await response.Content.ReadAsStringAsync(); builder.Append(Prettify(rawContent)); return(builder.ToString()); } var content = await response.Content.ReadAsStringAsync(); var query = bifoqlQuery ?? request.bifoqlQuery; if (query != null) { content = await RunBifoqlQuery(content, query); } return(Prettify(content)); }