Page History
...
Code Block |
---|
[
{ "itemIndex": 0, "optionKey": "SKIP", "optionValue": false },
{ "itemIndex": 0, "optionKey": "OPTION\", "optionValue": "ADD" }
] |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace YellowfinAPIExamples
{
public class CreateADataSource
{
static async Task Main(string[] args)
{
string host = "http://localhost:8080/Yellowfin";
string restUsername = "admin@yellowfin.com.au";
string restPassword = "test";
string createDataSourcePayload = @"
{
""sourceName"": ""PostgreSQL Database Created Via Import"",
""sourceDescription"": """",
""sourceType"": ""POSTGRESQL"",
""connectionType"": ""JDBC"",
""connectionTypeCode"": ""GENERICUSER"",
""connectionDriver"": ""org.postgresql.Driver"",
""connectionString"": ""jdbc:postgresql://192.168.1.100:5432/testdata"",
""connectionTimeout"": 180,
""userName"": ""postgres"",
""minimumConnections"": 1,
""maximumConnections"": 5,
""refreshTime"": 180,
""timezone"": ""AUSTRALIA/SYDNEY"",
""accessLevelCode"": ""UNSECURE"",
""maxRows"": 10000,
""maxAnalysisRows"": 0,
""inheritChildSourceFilters"": false,
""sourceLogIndicator"": false,
""sourceOptions"": [
{
""optionKey"": ""ISOLATIONLEVEL"",
""optionValue"": ""1.0"",
""valueDataType"": ""1""
},
{
""optionKey"": ""USESCHEMA"",
""optionValue"": ""true"",
""valueDataType"": ""6""
},
{
""optionKey"": ""HOSTNAME"",
""optionValue"": ""192.168.1.100"",
""valueDataType"": ""2""
},
{
""optionKey"": ""PORT"",
""optionValue"": ""5432"",
""valueDataType"": ""1""
},
{
""optionKey"": ""DATABASE"",
""optionValue"": ""testdata"",
""valueDataType"": ""2""
},
{
""optionKey"": ""YF_DRIVER_SELECTION"",
""optionValue"": ""org.postgresql.Driver"",
""valueDataType"": ""2""
}
]
}";
string token = await GenerateToken(host, restUsername, restPassword);
Console.WriteLine("Payload: " + createDataSourcePayload);
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("YELLOWFIN", $"ts={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}, nonce={new Random().NextInt64()}, token={token}");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.yellowfin.api-v1+json"));
var content = new StringContent(createDataSourcePayload, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.PostAsync($"{host}/api/data-sources", content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("Failed to create data source. Status code: " + response.StatusCode);
}
}
}
static async Task<string> GenerateToken(string host, string restUsername, string restPassword)
{
using (var client = new HttpClient())
{
// Generate nonce
long nonce = new Random().NextInt64();
// Create HTTP request
var request = new HttpRequestMessage(HttpMethod.Post, $"{host}/api/refresh-tokens");
request.Headers.Add("Authorization", $"YELLOWFIN ts={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}, nonce={nonce}");
request.Headers.Add("Accept", "application/vnd.yellowfin.api-v1+json");
request.Content = new StringContent(
JsonConvert.SerializeObject(new { userName = restUsername, password = restPassword }),
System.Text.Encoding.UTF8,
"application/json"
);
// Send request and get response
HttpResponseMessage response = await client.SendAsync(request);
string responseContent = await response.Content.ReadAsStringAsync();
// Parse JSON response
JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseContent);
string accessToken = jsonObject["_embedded"]["accessToken"]["securityToken"].ToString();
if (!string.IsNullOrEmpty(accessToken))
{
Console.WriteLine("Access Token: " + accessToken);
}
else
{
Console.WriteLine("Token not retrieved");
Environment.Exit(-1);
}
return accessToken;
}
}
}
} |
...