Repository navigation
Expand file tree
/
Copy pathbasic.cs
More file actions
86 lines (74 loc) · 3.14 KB
/
Copy pathbasic.cs
File metadata and controls
86 lines (74 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Parts Of Speech API - Basic Usage Example
*
* This example demonstrates the basic usage of the Parts Of Speech API.
* API Documentation: https://docs.apiverve.com/ref/partsofspeech
*/
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Linq;
namespace APIVerve.Examples
{
class PartsOfSpeechExample
{
private static readonly string API_KEY = Environment.GetEnvironmentVariable("APIVERVE_API_KEY") ?? "YOUR_API_KEY_HERE";
private static readonly string API_URL = "https://api.apiverve.com/v1/partsofspeech";
/// <summary>
/// Make a POST request to the Parts Of Speech API
/// </summary>
static async Task<JsonDocument> CallPartsOfSpeechAPI()
{
try
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("x-api-key", API_KEY);
// Request body
var requestBody = new { text = "My friend from college hails from a small countryside village located at the heart of the state. During the last summer vacation, I had an opportunity to visit my friend in his village. I packed up my belongings and boarded the bus bound for the village", partofspeech = "verbs" };
var jsonContent = JsonSerializer.Serialize(requestBody);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
var response = await client.PostAsync(API_URL, content);
// Check if response is successful
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
var data = JsonDocument.Parse(responseBody);
// Check API response status
if (data.RootElement.GetProperty("status").GetString() == "ok")
{
Console.WriteLine("✓ Success!");
Console.WriteLine("Response data: " + data.RootElement.GetProperty("data"));
return data;
}
else
{
var error = data.RootElement.TryGetProperty("error", out var errorProp)
? errorProp.GetString()
: "Unknown error";
Console.WriteLine($"✗ API Error: {error}");
return null;
}
}
catch (HttpRequestException e)
{
Console.WriteLine($"✗ Request failed: {e.Message}");
return null;
}
}
static async Task Main(string[] args)
{
Console.WriteLine("📤 Calling Parts Of Speech API...\n");
var result = await CallPartsOfSpeechAPI();
if (result != null)
{
Console.WriteLine("\n📊 Final Result:");
Console.WriteLine(result.RootElement.GetProperty("data"));
}
else
{
Console.WriteLine("\n✗ API call failed");
}
}
}
}