// ====================================================================
// 602TechSec List Management API - C# Client Example
// ====================================================================
// This is a complete example showing how to integrate the List Management
// API into your C# application.
//
// USAGE:
// 1. Copy this file to your project
// 2. Replace "your-api-key-here" with your actual API key
// 3. Customize as needed for your requirements
// ====================================================================
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace YourProject.ApiClients
{
public class ListManagementClient
{
private readonly HttpClient _client;
private readonly string _apiKey;
private const string BaseUrl = "https://sec.602.tech";
public ListManagementClient(string apiKey)
{
_apiKey = apiKey;
_client = new HttpClient();
_client.Timeout = TimeSpan.FromSeconds(30);
}
#region IP Management
///
/// Add an IP address to whitelist or blacklist
///
public async Task AddIpAddressAsync(string ipAddress, string listType)
{
var request = new
{
apiKey = _apiKey,
ipAddress = ipAddress,
listType = listType
};
return await PostAsync("/api/listmanagement/ip/add", request);
}
///
/// Remove an IP address from whitelist or blacklist
///
public async Task RemoveIpAddressAsync(string ipAddress, string listType)
{
var request = new
{
apiKey = _apiKey,
ipAddress = ipAddress,
listType = listType
};
return await PostAsync("/api/listmanagement/ip/remove", request);
}
#endregion
#region Host Management
///
/// Add a host to whitelist or blacklist
///
public async Task AddHostAsync(string host, string listType)
{
var request = new
{
apiKey = _apiKey,
host = host,
listType = listType
};
return await PostAsync("/api/listmanagement/host/add", request);
}
///
/// Remove a host from whitelist or blacklist
///
public async Task RemoveHostAsync(string host, string listType)
{
var request = new
{
apiKey = _apiKey,
host = host,
listType = listType
};
return await PostAsync("/api/listmanagement/host/remove", request);
}
#endregion
#region Bulk Operations
///
/// Bulk add multiple entries
///
public async Task BulkAddAsync(List entries, string entryType, string listType)
{
var request = new
{
apiKey = _apiKey,
entries = entries,
entryType = entryType,
listType = listType
};
return await PostAsync("/api/listmanagement/bulk/add", request);
}
#endregion
#region Query Operations
///
/// Get all lists for the API key
///
public async Task GetAllListsAsync()
{
try
{
var response = await _client.GetAsync($"{BaseUrl}/api/listmanagement/lists?apiKey={_apiKey}");
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject(json);
}
catch (Exception ex)
{
return new ListContentsResponse
{
Success = false,
Message = ex.Message
};
}
}
///
/// Check if an entry exists in whitelist or blacklist
///
public async Task CheckEntryAsync(string entry, string entryType)
{
var request = new
{
apiKey = _apiKey,
entry = entry,
entryType = entryType
};
return await PostAsync("/api/listmanagement/check", request);
}
#endregion
#region Helper Methods
private async Task PostAsync(string endpoint, object data) where T : new()
{
try
{
var json = JsonConvert.SerializeObject(data);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _client.PostAsync($"{BaseUrl}{endpoint}", content);
var responseJson = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject(responseJson);
}
catch (Exception ex)
{
// Return error response
var errorResponse = new T();
if (errorResponse is ApiResponse apiResponse)
{
apiResponse.Success = false;
apiResponse.Message = ex.Message;
}
return errorResponse;
}
}
#endregion
#region Convenience Methods
///
/// Add an IP to whitelist (convenience method)
///
public Task WhitelistIpAsync(string ipAddress)
=> AddIpAddressAsync(ipAddress, "whitelist");
///
/// Add an IP to blacklist (convenience method)
///
public Task BlacklistIpAsync(string ipAddress)
=> AddIpAddressAsync(ipAddress, "blacklist");
///
/// Add a host to whitelist (convenience method)
///
public Task WhitelistHostAsync(string host)
=> AddHostAsync(host, "whitelist");
///
/// Add a host to blacklist (convenience method)
///
public Task BlacklistHostAsync(string host)
=> AddHostAsync(host, "blacklist");
///
/// Check if IP is whitelisted (convenience method)
///
public async Task IsIpWhitelistedAsync(string ipAddress)
{
var result = await CheckEntryAsync(ipAddress, "ip");
return result.InWhitelist;
}
///
/// Check if IP is blacklisted (convenience method)
///
public async Task IsIpBlacklistedAsync(string ipAddress)
{
var result = await CheckEntryAsync(ipAddress, "ip");
return result.InBlacklist;
}
#endregion
public void Dispose()
{
_client?.Dispose();
}
}
#region Response Models
public class ApiResponse
{
public bool Success { get; set; }
public string Message { get; set; }
public string Entry { get; set; }
public string ListType { get; set; }
public DateTime Timestamp { get; set; }
}
public class BulkApiResponse
{
public bool Success { get; set; }
public string Message { get; set; }
public int SuccessCount { get; set; }
public int FailureCount { get; set; }
public List FailedEntries { get; set; }
public DateTime Timestamp { get; set; }
}
public class CheckEntryResponse
{
public bool Success { get; set; }
public string Entry { get; set; }
public bool InWhitelist { get; set; }
public bool InBlacklist { get; set; }
public string Message { get; set; }
public DateTime Timestamp { get; set; }
}
public class ListContentsResponse
{
public bool Success { get; set; }
public string Message { get; set; }
public List IpWhitelist { get; set; }
public List IpBlacklist { get; set; }
public List HostWhitelist { get; set; }
public List HostBlacklist { get; set; }
public int TotalWhitelistCount { get; set; }
public int TotalBlacklistCount { get; set; }
public DateTime Timestamp { get; set; }
}
#endregion
}
// ====================================================================
// USAGE EXAMPLE
// ====================================================================
//
// class Program
// {
// static async Task Main()
// {
// var client = new ListManagementClient("your-api-key-here");
//
// // Add IP to whitelist
// var result = await client.WhitelistIpAsync("192.168.1.100");
// Console.WriteLine($"Success: {result.Success}, Message: {result.Message}");
//
// // Add host to blacklist
// var result2 = await client.BlacklistHostAsync("malicious.com");
// Console.WriteLine($"Success: {result2.Success}, Message: {result2.Message}");
//
// // Check if IP is whitelisted
// var isWhitelisted = await client.IsIpWhitelistedAsync("192.168.1.100");
// Console.WriteLine($"IP is whitelisted: {isWhitelisted}");
//
// // Get all lists
// var lists = await client.GetAllListsAsync();
// Console.WriteLine($"IP Whitelist: {lists.IpWhitelist.Count}");
// Console.WriteLine($"Host Blacklist: {lists.HostBlacklist.Count}");
// }
// }