mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-20 02:58:46 +00:00
feat: Add analytics, security, and currency SDK modules with comprehensive API endpoints across Elixir and Go SDKs
- Add AnalyticsAPI with churn prediction (predict, metrics, at-risk), market analytics (metrics, competitors, opportunities), predictive maintenance (metrics, assets, alerts, predict), and pricing optimization (metrics, optimize, elasticity) endpoints - Add SecurityAPI with fraud detection (analyze, alerts, update status, metrics, patterns) and SIM swap protection (verify, history) endpoints - Add CurrencyAPI with currency conversion (convert, exchange rates, rate
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
import Foundation
|
||||
|
||||
/// Analytics API for churn prediction, market analysis, and pricing optimization
|
||||
public class AnalyticsAPI {
|
||||
private let client: HTTPClient
|
||||
|
||||
public init(client: HTTPClient) {
|
||||
self.client = client
|
||||
}
|
||||
|
||||
// MARK: - Churn Analysis
|
||||
|
||||
/// Predict churn risk for a profile
|
||||
public func predictChurn(profileId: String) async throws -> ChurnPrediction {
|
||||
return try await client.post(
|
||||
"/api/v1/analytics/churn/predict",
|
||||
body: ["profile_id": profileId],
|
||||
responseType: ChurnPrediction.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get churn metrics
|
||||
public func getChurnMetrics(period: String = "monthly") async throws -> ChurnMetrics {
|
||||
return try await client.get(
|
||||
"/api/v1/analytics/churn/metrics",
|
||||
parameters: ["period": period],
|
||||
responseType: ChurnMetrics.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get at-risk customers
|
||||
public func getAtRiskCustomers(
|
||||
riskLevel: ChurnRiskLevel,
|
||||
limit: Int = 100
|
||||
) async throws -> [ChurnPrediction] {
|
||||
return try await client.post(
|
||||
"/api/v1/analytics/churn/at-risk",
|
||||
body: [
|
||||
"risk_level": riskLevel.rawValue,
|
||||
"limit": limit
|
||||
],
|
||||
responseType: [ChurnPrediction].self
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Market Analytics
|
||||
|
||||
/// Get market metrics
|
||||
public func getMarketMetrics(period: String = "monthly") async throws -> MarketMetrics {
|
||||
return try await client.get(
|
||||
"/api/v1/analytics/market/metrics",
|
||||
parameters: ["period": period],
|
||||
responseType: MarketMetrics.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get competitor analysis
|
||||
public func getCompetitors() async throws -> [String: Any] {
|
||||
return try await client.get("/api/v1/analytics/market/competitors")
|
||||
}
|
||||
|
||||
/// Get market opportunities
|
||||
public func getMarketOpportunities() async throws -> [String: Any] {
|
||||
return try await client.get("/api/v1/analytics/market/opportunities")
|
||||
}
|
||||
|
||||
// MARK: - Predictive Maintenance
|
||||
|
||||
/// Get maintenance metrics
|
||||
public func getMaintenanceMetrics(period: String = "monthly") async throws -> MaintenanceMetrics {
|
||||
return try await client.get(
|
||||
"/api/v1/analytics/maintenance/metrics",
|
||||
parameters: ["period": period],
|
||||
responseType: MaintenanceMetrics.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get assets health
|
||||
public func getAssetsHealth() async throws -> [String: Any] {
|
||||
return try await client.get("/api/v1/analytics/maintenance/assets")
|
||||
}
|
||||
|
||||
/// Get maintenance alerts
|
||||
public func getMaintenanceAlerts() async throws -> [String: Any] {
|
||||
return try await client.get("/api/v1/analytics/maintenance/alerts")
|
||||
}
|
||||
|
||||
/// Predict failure for an asset
|
||||
public func predictFailure(assetId: String) async throws -> [String: Any] {
|
||||
return try await client.post("/api/v1/analytics/maintenance/predict/\(assetId)", body: [:])
|
||||
}
|
||||
|
||||
// MARK: - Pricing Optimization
|
||||
|
||||
/// Get pricing metrics
|
||||
public func getPricingMetrics(period: String = "monthly") async throws -> PricingMetrics {
|
||||
return try await client.get(
|
||||
"/api/v1/analytics/pricing/metrics",
|
||||
parameters: ["period": period],
|
||||
responseType: PricingMetrics.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Optimize pricing for rate plans
|
||||
public func optimizePricing(
|
||||
ratePlanIds: [String],
|
||||
strategy: String = "revenue_maximization"
|
||||
) async throws -> [PricingOptimizationResult] {
|
||||
return try await client.post(
|
||||
"/api/v1/analytics/pricing/optimize",
|
||||
body: [
|
||||
"rate_plan_ids": ratePlanIds,
|
||||
"strategy": strategy
|
||||
],
|
||||
responseType: [PricingOptimizationResult].self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get price elasticity data
|
||||
public func getPriceElasticity() async throws -> [String: Any] {
|
||||
return try await client.get("/api/v1/analytics/pricing/elasticity")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Analytics Types
|
||||
|
||||
public struct ChurnPrediction: Codable {
|
||||
public let profileId: String
|
||||
public let riskLevel: String
|
||||
public let riskScore: Double
|
||||
public let predictedChurnDate: String?
|
||||
public let reasons: [String]
|
||||
public let recommendations: [String]
|
||||
public let lastUpdated: String
|
||||
}
|
||||
|
||||
public struct ChurnMetrics: Codable {
|
||||
public let period: String
|
||||
public let totalSubscribers: Int64
|
||||
public let churnedSubscribers: Int64
|
||||
public let churnRate: Double
|
||||
public let monthlyChurnRate: Double
|
||||
public let annualChurnRate: Double
|
||||
public let averageTenureDays: Double
|
||||
public let riskDistribution: [String: Int64]
|
||||
public let generatedAt: String
|
||||
}
|
||||
|
||||
public struct MarketMetrics: Codable {
|
||||
public let period: String
|
||||
public let totalMarketSize: Int64
|
||||
public let ourSubscribers: Int64
|
||||
public let marketShare: Double
|
||||
public let growthRate: Double
|
||||
public let byCountry: [String: [String: Any]]
|
||||
public let generatedAt: String
|
||||
}
|
||||
|
||||
public struct MaintenanceMetrics: Codable {
|
||||
public let period: String
|
||||
public let totalAssets: Int64
|
||||
public let healthyAssets: Int64
|
||||
public let assetsNeedingAttention: Int64
|
||||
public let uptime: Double
|
||||
public let meanTimeToFailure: Double
|
||||
public let meanTimeToRepair: Double
|
||||
public let generatedAt: String
|
||||
}
|
||||
|
||||
public struct PricingMetrics: Codable {
|
||||
public let period: String
|
||||
public let totalRevenue: Double
|
||||
public let arpu: Double
|
||||
public let priceElasticity: Double
|
||||
public let competitiveIndex: Double
|
||||
public let optimizationRoi: Double
|
||||
public let generatedAt: String
|
||||
}
|
||||
|
||||
public struct PricingOptimizationResult: Codable {
|
||||
public let ratePlanId: String
|
||||
public let strategy: String
|
||||
public let currentPrice: Double
|
||||
public let optimalPrice: Double
|
||||
public let priceChangePct: Double
|
||||
public let expectedRevenue: Double
|
||||
public let expectedDemand: Double
|
||||
public let confidence: Double
|
||||
public let reasoning: [String]
|
||||
public let risks: [String]
|
||||
public let recommendations: [String]
|
||||
}
|
||||
|
||||
public enum ChurnRiskLevel: String, CaseIterable {
|
||||
case low = "low"
|
||||
case medium = "medium"
|
||||
case high = "high"
|
||||
case critical = "critical"
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
import Foundation
|
||||
|
||||
/// Currency and Billing API
|
||||
public class CurrencyAPI {
|
||||
private let client: HTTPClient
|
||||
|
||||
public init(client: HTTPClient) {
|
||||
self.client = client
|
||||
}
|
||||
|
||||
// MARK: - Currency Conversion
|
||||
|
||||
/// Convert currency
|
||||
public func convert(from: String, to: String, amount: Double) async throws -> ConvertResponse {
|
||||
return try await client.post(
|
||||
"/api/v1/currency/convert",
|
||||
body: [
|
||||
"from": from,
|
||||
"to": to,
|
||||
"amount": amount
|
||||
],
|
||||
responseType: ConvertResponse.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get exchange rate between currencies
|
||||
public func getExchangeRate(from: String, to: String) async throws -> ExchangeRate {
|
||||
return try await client.get(
|
||||
"/api/v1/currency/exchange/\(from)/\(to)",
|
||||
responseType: ExchangeRate.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get exchange rate history
|
||||
public func getExchangeRateHistory(from: String, to: String, days: Int = 30) async throws -> [ExchangeRate] {
|
||||
return try await client.get(
|
||||
"/api/v1/currency/exchange/\(from)/\(to)/history",
|
||||
parameters: ["days": days],
|
||||
responseType: [ExchangeRate].self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get supported currencies
|
||||
public func getSupportedCurrencies() async throws -> [Currency] {
|
||||
return try await client.get(
|
||||
"/api/v1/currency/currencies",
|
||||
responseType: [Currency].self
|
||||
)
|
||||
}
|
||||
|
||||
/// Refresh exchange rates
|
||||
public func refreshExchangeRates() async throws -> [String: Any] {
|
||||
return try await client.post("/api/v1/currency/exchange/refresh", body: [:])
|
||||
}
|
||||
|
||||
// MARK: - Billing
|
||||
|
||||
/// Process billing transaction
|
||||
public func processBilling(_ billingData: [String: Any]) async throws -> BillingTransaction {
|
||||
return try await client.post(
|
||||
"/api/v1/currency/billing",
|
||||
body: billingData,
|
||||
responseType: BillingTransaction.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get billing history for a profile
|
||||
public func getBillingHistory(profileId: String, limit: Int = 50) async throws -> [BillingTransaction] {
|
||||
return try await client.get(
|
||||
"/api/v1/currency/billing/history/\(profileId)",
|
||||
parameters: ["limit": limit],
|
||||
responseType: [BillingTransaction].self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get billing summary for a profile
|
||||
public func getBillingSummary(profileId: String, period: String = "monthly") async throws -> BillingSummary {
|
||||
return try await client.get(
|
||||
"/api/v1/currency/billing/summary/\(profileId)",
|
||||
parameters: ["period": period],
|
||||
responseType: BillingSummary.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Process refund
|
||||
public func processRefund(transactionId: String, reason: String) async throws -> BillingTransaction {
|
||||
return try await client.post(
|
||||
"/api/v1/currency/billing/refund/\(transactionId)",
|
||||
body: ["reason": reason],
|
||||
responseType: BillingTransaction.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get billing analytics
|
||||
public func getBillingAnalytics(period: String = "monthly") async throws -> [String: Any] {
|
||||
return try await client.get(
|
||||
"/api/v1/currency/billing/analytics",
|
||||
parameters: ["period": period]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Currency Types
|
||||
|
||||
public struct ConvertRequest: Codable {
|
||||
public let from: String
|
||||
public let to: String
|
||||
public let amount: Double
|
||||
}
|
||||
|
||||
public struct ConvertResponse: Codable {
|
||||
public let from: String
|
||||
public let to: String
|
||||
public let amount: Double
|
||||
public let converted: Double
|
||||
public let rate: Double
|
||||
public let timestamp: String
|
||||
}
|
||||
|
||||
public struct ExchangeRate: Codable {
|
||||
public let from: String
|
||||
public let to: String
|
||||
public let rate: Double
|
||||
public let timestamp: String
|
||||
}
|
||||
|
||||
public struct Currency: Codable {
|
||||
public let code: String
|
||||
public let name: String
|
||||
public let symbol: String
|
||||
}
|
||||
|
||||
public struct BillingTransaction: Codable {
|
||||
public let id: String
|
||||
public let profileId: String
|
||||
public let amount: Double
|
||||
public let currency: String
|
||||
public let type: String
|
||||
public let status: String
|
||||
public let description: String
|
||||
public let createdAt: String
|
||||
}
|
||||
|
||||
public struct BillingSummary: Codable {
|
||||
public let profileId: String
|
||||
public let period: String
|
||||
public let totalAmount: Double
|
||||
public let currency: String
|
||||
public let transactionCount: Int
|
||||
public let breakdown: [String: Double]
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
/// API for GraphQL queries
|
||||
public class GraphQLAPI {
|
||||
private let client: HTTPClient
|
||||
|
||||
public init(client: HTTPClient) {
|
||||
self.client = client
|
||||
}
|
||||
|
||||
/// Execute a GraphQL query
|
||||
public func execute(query: String, variables: [String: Any]? = nil) async throws -> [String: Any] {
|
||||
let request = GraphQLRequest(query: query, variables: variables)
|
||||
return try await client.post(path: "/graphql", body: request)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
import Foundation
|
||||
|
||||
/// Security API for fraud detection and SIM swap protection
|
||||
public class SecurityAPI {
|
||||
private let client: HTTPClient
|
||||
|
||||
public init(client: HTTPClient) {
|
||||
self.client = client
|
||||
}
|
||||
|
||||
/// Analyze a transaction for fraud
|
||||
public func analyzeTransaction(_ transaction: [String: Any]) async throws -> FraudAlert? {
|
||||
do {
|
||||
return try await client.post(
|
||||
"/api/v1/security/fraud/analyze",
|
||||
body: transaction,
|
||||
responseType: FraudAlert.self
|
||||
)
|
||||
} catch {
|
||||
if case HTTPClient.APIError.notFound = error {
|
||||
return nil
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/// Get fraud alerts with filtering
|
||||
public func getFraudAlerts(filter: FraudAlertFilter? = nil) async throws -> [FraudAlert] {
|
||||
var body: [String: Any] = [:]
|
||||
if let filter = filter {
|
||||
if let type = filter.type { body["type"] = type }
|
||||
if let severity = filter.severity { body["severity"] = severity }
|
||||
if let status = filter.status { body["status"] = status }
|
||||
body["limit"] = filter.limit
|
||||
}
|
||||
|
||||
return try await client.post(
|
||||
"/api/v1/security/fraud/alerts",
|
||||
body: body,
|
||||
responseType: [FraudAlert].self
|
||||
)
|
||||
}
|
||||
|
||||
/// Update fraud alert status
|
||||
public func updateAlertStatus(
|
||||
alertId: String,
|
||||
status: String,
|
||||
actions: [String] = []
|
||||
) async throws -> [String: Any] {
|
||||
return try await client.put(
|
||||
"/api/v1/security/fraud/alerts/\(alertId)",
|
||||
body: [
|
||||
"status": status,
|
||||
"actions": actions
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
/// Get fraud detection metrics
|
||||
public func getFraudMetrics(period: String = "monthly") async throws -> FraudMetrics {
|
||||
return try await client.get(
|
||||
"/api/v1/security/fraud/metrics",
|
||||
parameters: ["period": period],
|
||||
responseType: FraudMetrics.self
|
||||
)
|
||||
}
|
||||
|
||||
/// Get detected fraud patterns
|
||||
public func getFraudPatterns() async throws -> [String: Any] {
|
||||
return try await client.get("/api/v1/security/fraud/patterns")
|
||||
}
|
||||
|
||||
/// Verify SIM swap request
|
||||
public func verifySIMSwap(profileId: String, msisdn: String) async throws -> [String: Any] {
|
||||
return try await client.post(
|
||||
"/api/v1/security/simswap/verify",
|
||||
body: [
|
||||
"profile_id": profileId,
|
||||
"msisdn": msisdn
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
/// Get SIM swap history for a profile
|
||||
public func getSIMSwapHistory(profileId: String) async throws -> [String: Any] {
|
||||
return try await client.get("/api/v1/security/simswap/history/\(profileId)")
|
||||
}
|
||||
}
|
||||
|
||||
public struct FraudAlert: Codable {
|
||||
public let id: String
|
||||
public let type: String
|
||||
public let severity: String
|
||||
public let profileId: String
|
||||
public let description: String
|
||||
public let riskScore: Double
|
||||
public let evidence: [String]
|
||||
public let ipAddress: String
|
||||
public let timestamp: String
|
||||
public let status: String
|
||||
public let actionsTaken: [String]
|
||||
public let metadata: [String: Any]
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id, type, severity, profileId, description, riskScore
|
||||
case evidence, ipAddress, timestamp, status, actionsTaken, metadata
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.decode(String.self, forKey: .id)
|
||||
type = try container.decode(String.self, forKey: .type)
|
||||
severity = try container.decode(String.self, forKey: .severity)
|
||||
profileId = try container.decode(String.self, forKey: .profileId)
|
||||
description = try container.decode(String.self, forKey: .description)
|
||||
riskScore = try container.decode(Double.self, forKey: .riskScore)
|
||||
evidence = try container.decode([String].self, forKey: .evidence)
|
||||
ipAddress = try container.decode(String.self, forKey: .ipAddress)
|
||||
timestamp = try container.decode(String.self, forKey: .timestamp)
|
||||
status = try container.decode(String.self, forKey: .status)
|
||||
actionsTaken = try container.decode([String].self, forKey: .actionsTaken)
|
||||
metadata = try container.decode([String: Any].self, forKey: .metadata)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(id, forKey: .id)
|
||||
try container.encode(type, forKey: .type)
|
||||
try container.encode(severity, forKey: .severity)
|
||||
try container.encode(profileId, forKey: .profileId)
|
||||
try container.encode(description, forKey: .description)
|
||||
try container.encode(riskScore, forKey: .riskScore)
|
||||
try container.encode(evidence, forKey: .evidence)
|
||||
try container.encode(ipAddress, forKey: .ipAddress)
|
||||
try container.encode(timestamp, forKey: .timestamp)
|
||||
try container.encode(status, forKey: .status)
|
||||
try container.encode(actionsTaken, forKey: .actionsTaken)
|
||||
try container.encode(metadata, forKey: .metadata)
|
||||
}
|
||||
}
|
||||
|
||||
public struct FraudMetrics: Codable {
|
||||
public let period: String
|
||||
public let totalAlerts: Int64
|
||||
public let resolvedAlerts: Int64
|
||||
public let falsePositives: Int64
|
||||
public let resolutionRate: Double
|
||||
public let falsePositiveRate: Double
|
||||
public let byType: [String: Int64]
|
||||
public let bySeverity: [String: Int64]
|
||||
public let generatedAt: String
|
||||
}
|
||||
|
||||
public struct FraudAlertFilter {
|
||||
public let type: String?
|
||||
public let severity: String?
|
||||
public let status: String?
|
||||
public let limit: Int
|
||||
|
||||
public init(type: String? = nil, severity: String? = nil, status: String? = nil, limit: Int = 50) {
|
||||
self.type = type
|
||||
self.severity = severity
|
||||
self.status = status
|
||||
self.limit = limit
|
||||
}
|
||||
}
|
||||
|
||||
public enum FraudType: String, CaseIterable {
|
||||
case accountTakeover = "account_takeover"
|
||||
case subscriptionFraud = "subscription_fraud"
|
||||
case paymentFraud = "payment_fraud"
|
||||
case usageAnomaly = "usage_anomaly"
|
||||
case simSwap = "sim_swap"
|
||||
}
|
||||
|
||||
public enum FraudSeverity: String, CaseIterable {
|
||||
case low = "low"
|
||||
case medium = "medium"
|
||||
case high = "high"
|
||||
case critical = "critical"
|
||||
}
|
||||
@@ -19,9 +19,9 @@ public class TelecomSDK {
|
||||
public let payments: PaymentAPI
|
||||
public let ratingPlans: RatingPlanAPI
|
||||
public let system: SystemAPI
|
||||
public let graphql: GraphQLAPI
|
||||
|
||||
// MARK: - Initialization
|
||||
public let analytics: AnalyticsAPI
|
||||
public let security: SecurityAPI
|
||||
public let currency: CurrencyAPI
|
||||
|
||||
/// Initialize a new TelecomSDK instance
|
||||
/// - Parameter config: Configuration for the SDK
|
||||
@@ -36,7 +36,9 @@ public class TelecomSDK {
|
||||
self.payments = PaymentAPI(client: httpClient)
|
||||
self.ratingPlans = RatingPlanAPI(client: httpClient)
|
||||
self.system = SystemAPI(client: httpClient)
|
||||
self.graphql = GraphQLAPI(client: httpClient)
|
||||
self.analytics = AnalyticsAPI(client: httpClient)
|
||||
self.security = SecurityAPI(client: httpClient)
|
||||
self.currency = CurrencyAPI(client: httpClient)
|
||||
}
|
||||
|
||||
// MARK: - Authentication Methods
|
||||
|
||||
Reference in New Issue
Block a user