public void Get_Schema_For_Response() { // GIVEN an instance of the LUSID client, and a portfolio created in LUSID var credentials = new TokenCredentials(_apiToken); var client = new LUSIDAPI(new Uri(_apiUrl, UriKind.Absolute), credentials); const string scope = "finbourne"; var code = $"id-{Guid.NewGuid()}"; var request = new CreateTransactionPortfolioRequest($"Portfolio-{code}", code, baseCurrency: "GBP"); client.CreatePortfolio(scope, request); // WHEN the portfolio is queried var portfolioResponse = client.GetPortfolioWithHttpMessagesAsync(scope, code).Result; // THEN the result should include a schema Url var schemaHeaderItem = portfolioResponse.Response.Headers.First(h => h.Key == "lusid-schema-url"); // AND which we we can use to query for the schema of the entity // TODO: Too difficult to convert the returned Url into parameters for the call to GetSchema Regex regex = new Regex(".+/(\\w+)"); var entityType = regex.Match(schemaHeaderItem.Value.First()); var schema = client.GetEntitySchema(entityType.Groups[1].Value); Assert.That(schema, Is.Not.Null); Assert.That(schema.Values, Is.Not.Empty); var fields = typeof(Portfolio).GetProperties().Select(p => p.Name).ToImmutableHashSet(); foreach (var fieldSchema in schema.Values) { Assert.That(fields, Does.Contain(fieldSchema.Value.DisplayName)); } }
public void SetUp() { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("secrets.json") .Build(); config.GetSection("api").Bind(_apiConfiguration); Assert.That(_apiConfiguration, Is.Not.Null); Assert.That(_apiConfiguration.ApiUrl, Is.Not.Null); var tokenUrl = Environment.GetEnvironmentVariable("FBN_TOKEN_URL") ?? _apiConfiguration.TokenUrl; var username = Environment.GetEnvironmentVariable("FBN_USERNAME") ?? _apiConfiguration.Username; var password = Environment.GetEnvironmentVariable("FBN_PASSWORD") ?? _apiConfiguration.Password; var clientId = Environment.GetEnvironmentVariable("FBN_CLIENT_ID") ?? _apiConfiguration.ClientId; var clientSecret = Environment.GetEnvironmentVariable("FBN_CLIENT_SECRET") ?? _apiConfiguration.ClientSecret; _apiUrl = Environment.GetEnvironmentVariable("FBN_LUSID_API_URL") ?? _apiConfiguration.ApiUrl; var tokenRequestBody = $"grant_type=password&username={HttpUtility.UrlEncode(username)}&password={HttpUtility.UrlEncode(password)}&scope=openid client groups&client_id={clientId}&client_secret={clientSecret}"; using (var httpClient = new HttpClient()) { //Only accept JSON httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // gets the response var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl) { Content = new StringContent(tokenRequestBody) }; tokenRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); var response = httpClient.SendAsync(tokenRequest).Result; var body = response.Content.ReadAsStringAsync().Result; response.EnsureSuccessStatusCode(); _apiToken = JsonConvert.DeserializeObject <Dictionary <string, string> >(body)["access_token"]; } var credentials = new TokenCredentials(_apiToken); _client = new LUSIDAPI(new Uri(_apiUrl, UriKind.Absolute), credentials); var instruments = new List <(string Figi, string Name)> { (Figi : "BBG000C6K6G9", Name : "VODAFONE GROUP PLC"), (Figi : "BBG000C04D57", Name : "BARCLAYS PLC"), (Figi : "BBG000FV67Q4", Name : "NATIONAL GRID PLC"), (Figi : "BBG000BF0KW3", Name : "SAINSBURY (J) PLC"), (Figi : "BBG000BF4KL1", Name : "TAYLOR WIMPEY PLC") }; var upsertResponse = _client.UpsertInstruments(instruments.ToDictionary( k => k.Figi, v => new InstrumentDefinition( name: v.Name, identifiers: new Dictionary <string, string> { ["Figi"] = v.Figi } ) )); Assert.That(upsertResponse.Failed.Count, Is.EqualTo(0)); var ids = _client.GetInstruments("Figi", instruments.Select(i => i.Figi).ToList()); _instrumentIds = ids.Values.Select(x => x.Value.LusidInstrumentId).ToList(); }