C#
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
// Replace with your actual token
private const string Token = "YOUR_TOKEN_HERE";
private const string CreateUrl = "https://api.autocontentapi.com/Content/Create";
private const string StatusBaseUrl = "https://api.autocontentapi.com/content/status/";
private static readonly TimeSpan PollInterval = TimeSpan.FromSeconds(5);
private static readonly HttpClient HttpClient = new HttpClient();
// Classes to represent the request and response JSON
public class Resource
{
public string content { get; set; }
public string type { get; set; }
}
public class CreateRequest
{
public Resource[] resources { get; set; }
public string text { get; set; }
public string outputType { get; set; }
}
public class CreateResponse
{
public string request_id { get; set; }
public string error_message { get; set; }
}
public class StatusResponse
{
public string id { get; set; }
public DateTimeOffset requested_on { get; set; }
public int status { get; set; }
public DateTimeOffset updated_on { get; set; }
public string request_json { get; set; }
public string error_message { get; set; }
public string audio_url { get; set; }
public string audio_title { get; set; }
public string response_text { get; set; }
}
static async Task Main()
{
try
{
var requestData = new CreateRequest
{
resources = new[]
{
new Resource { content = "https://autocontentapi.com", type = "website" },
new Resource { content = "74% of businesses expect AI to increase productivity", type = "text" }
},
text = "Explain why automating with AutoContent API is a game changer for any business",
outputType = "audio"
};
// Create the content
var requestId = await CreateContentAsync(requestData);
if (string.IsNullOrEmpty(requestId))
{
return;
}
// Poll until done
await PollStatusAsync(requestId);
}
catch (HttpRequestException ex)
{
Console.WriteLine("HTTP error: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
private static async Task<string> CreateContentAsync(CreateRequest requestData)
{
using var request = new HttpRequestMessage(HttpMethod.Post, CreateUrl)
{
Content = new StringContent(JsonSerializer.Serialize(requestData), Encoding.UTF8, "application/json")
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
using var response = await HttpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
var createResponse = JsonSerializer.Deserialize<CreateResponse>(json);
if (createResponse == null || !string.IsNullOrEmpty(createResponse.error_message))
{
Console.WriteLine("Error from create request: " + createResponse?.error_message);
return null;
}
Console.WriteLine($"Request initiated. Request ID: {createResponse.request_id}");
return createResponse.request_id;
}
private static async Task PollStatusAsync(string requestId)
{
var url = StatusBaseUrl + requestId;
while (true)
{
using var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using var response = await HttpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
var statusResponse = JsonSerializer.Deserialize<StatusResponse>(json);
if (statusResponse == null)
{
Console.WriteLine("No valid status response received.");
return;
}
if (!string.IsNullOrEmpty(statusResponse.error_message))
{
Console.WriteLine("Error from status check: " + statusResponse.error_message);
return;
}
if (statusResponse.status == 100)
{
Console.WriteLine("Content creation complete!");
Console.WriteLine("Audio URL: " + statusResponse.audio_url);
Console.WriteLine("Audio Title: " + statusResponse.audio_title);
return;
}
Console.WriteLine($"Current status: {statusResponse.status}. Waiting for 100...");
await Task.Delay(PollInterval);
}
}
}
Last updated