api-mocker 0.2.0__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
api_mocker/cli.py CHANGED
@@ -14,6 +14,9 @@ from api_mocker.plugins import PluginManager, BUILTIN_PLUGINS
14
14
  from api_mocker.analytics import AnalyticsManager
15
15
  from api_mocker.dashboard import DashboardManager
16
16
  from api_mocker.advanced import AdvancedFeatures, RateLimitConfig, CacheConfig, AuthConfig
17
+ from api_mocker.scenarios import scenario_manager, Scenario, ScenarioCondition, ScenarioResponse, ScenarioType
18
+ from api_mocker.smart_matching import smart_matcher, ResponseRule, MatchCondition, MatchType
19
+ from api_mocker.enhanced_analytics import EnhancedAnalytics, PerformanceMetrics, UsagePattern, APIDependency, CostOptimizationInsight
17
20
 
18
21
  app = typer.Typer(help="api-mocker: The industry-standard, production-ready, free API mocking and development acceleration tool.")
19
22
  console = Console()
@@ -901,5 +904,407 @@ def testing(
901
904
  console.print(f"[red]✗[/red] Testing error: {e}")
902
905
  raise typer.Exit(1)
903
906
 
907
+
908
+ @app.command()
909
+ def scenarios(
910
+ action: str = typer.Argument(..., help="Scenario action (list, create, activate, export, import, stats)"),
911
+ scenario_name: str = typer.Option(None, "--name", help="Scenario name"),
912
+ scenario_type: str = typer.Option("happy_path", "--type", help="Scenario type (happy_path, error_scenario, edge_case, performance_test, a_b_test)"),
913
+ config_file: str = typer.Option(None, "--config", help="Scenario configuration file"),
914
+ output_file: str = typer.Option(None, "--output", help="Output file for export"),
915
+ ):
916
+ """Manage scenario-based mocking."""
917
+ try:
918
+ if action == "list":
919
+ scenarios = scenario_manager.list_scenarios()
920
+ if not scenarios:
921
+ console.print("[yellow]No scenarios found. Create one with 'scenarios create'[/yellow]")
922
+ return
923
+
924
+ table = Table(title="Available Scenarios")
925
+ table.add_column("Name", style="cyan")
926
+ table.add_column("Type", style="green")
927
+ table.add_column("Active", style="yellow")
928
+ table.add_column("Description", style="white")
929
+
930
+ for name in scenarios:
931
+ scenario = scenario_manager.get_scenario(name)
932
+ if scenario:
933
+ table.add_row(
934
+ name,
935
+ scenario.scenario_type.value,
936
+ "✓" if scenario.active else "✗",
937
+ scenario.description
938
+ )
939
+
940
+ console.print(table)
941
+
942
+ elif action == "create":
943
+ if not scenario_name:
944
+ console.print("[red]✗[/red] Scenario name is required")
945
+ raise typer.Exit(1)
946
+
947
+ if scenario_type == "happy_path":
948
+ scenario = scenario_manager.create_happy_path_scenario()
949
+ elif scenario_type == "error_scenario":
950
+ scenario = scenario_manager.create_error_scenario("server_error")
951
+ elif scenario_type == "performance_test":
952
+ scenario = scenario_manager.create_performance_test_scenario()
953
+ elif scenario_type == "a_b_test":
954
+ scenario = scenario_manager.create_a_b_test_scenario()
955
+ else:
956
+ console.print(f"[red]✗[/red] Unknown scenario type: {scenario_type}")
957
+ raise typer.Exit(1)
958
+
959
+ scenario.name = scenario_name
960
+ scenario_manager.add_scenario(scenario)
961
+ console.print(f"[green]✓[/green] Created scenario: {scenario_name}")
962
+
963
+ elif action == "activate":
964
+ if not scenario_name:
965
+ console.print("[red]✗[/red] Scenario name is required")
966
+ raise typer.Exit(1)
967
+
968
+ if scenario_manager.activate_scenario(scenario_name):
969
+ console.print(f"[green]✓[/green] Activated scenario: {scenario_name}")
970
+ else:
971
+ console.print(f"[red]✗[/red] Scenario not found: {scenario_name}")
972
+ raise typer.Exit(1)
973
+
974
+ elif action == "export":
975
+ if not output_file:
976
+ output_file = "scenarios.json"
977
+
978
+ data = scenario_manager.export_scenarios()
979
+ with open(output_file, 'w') as f:
980
+ f.write(data)
981
+ console.print(f"[green]✓[/green] Exported scenarios to: {output_file}")
982
+
983
+ elif action == "import":
984
+ if not config_file:
985
+ console.print("[red]✗[/red] Config file is required")
986
+ raise typer.Exit(1)
987
+
988
+ with open(config_file, 'r') as f:
989
+ data = f.read()
990
+
991
+ scenario_manager.import_scenarios(data)
992
+ console.print(f"[green]✓[/green] Imported scenarios from: {config_file}")
993
+
994
+ elif action == "stats":
995
+ stats = scenario_manager.get_scenario_statistics()
996
+
997
+ table = Table(title="Scenario Statistics")
998
+ table.add_column("Metric", style="cyan")
999
+ table.add_column("Value", style="green")
1000
+
1001
+ table.add_row("Total Scenarios", str(stats["total_scenarios"]))
1002
+ table.add_row("Active Scenarios", str(stats["active_scenarios"]))
1003
+ table.add_row("Current Active", stats["current_active"] or "None")
1004
+
1005
+ for scenario_type, count in stats["scenario_types"].items():
1006
+ table.add_row(f"Type: {scenario_type}", str(count))
1007
+
1008
+ console.print(table)
1009
+
1010
+ else:
1011
+ console.print(f"[red]✗[/red] Unknown action: {action}")
1012
+ raise typer.Exit(1)
1013
+
1014
+ except Exception as e:
1015
+ console.print(f"[red]✗[/red] Scenario error: {e}")
1016
+ raise typer.Exit(1)
1017
+
1018
+
1019
+ @app.command()
1020
+ def smart_matching(
1021
+ action: str = typer.Argument(..., help="Smart matching action (list, create, test, export, import, stats)"),
1022
+ rule_name: str = typer.Option(None, "--name", help="Rule name"),
1023
+ rule_type: str = typer.Option(None, "--type", help="Rule type (user_type, api_version, premium_user, rate_limit, error, performance)"),
1024
+ config_file: str = typer.Option(None, "--config", help="Rule configuration file"),
1025
+ output_file: str = typer.Option(None, "--output", help="Output file for export"),
1026
+ test_request: str = typer.Option(None, "--test-request", help="Test request JSON"),
1027
+ ):
1028
+ """Manage smart response matching rules."""
1029
+ try:
1030
+ if action == "list":
1031
+ rules = smart_matcher.rules
1032
+ if not rules:
1033
+ console.print("[yellow]No rules found. Create one with 'smart-matching create'[/yellow]")
1034
+ return
1035
+
1036
+ table = Table(title="Smart Matching Rules")
1037
+ table.add_column("Name", style="cyan")
1038
+ table.add_column("Priority", style="green")
1039
+ table.add_column("Weight", style="yellow")
1040
+ table.add_column("Conditions", style="white")
1041
+
1042
+ for rule in rules:
1043
+ conditions = ", ".join([f"{c.field}={c.value}" for c in rule.conditions[:2]])
1044
+ if len(rule.conditions) > 2:
1045
+ conditions += "..."
1046
+
1047
+ table.add_row(
1048
+ rule.name,
1049
+ str(rule.priority),
1050
+ str(rule.weight),
1051
+ conditions
1052
+ )
1053
+
1054
+ console.print(table)
1055
+
1056
+ elif action == "create":
1057
+ if not rule_name or not rule_type:
1058
+ console.print("[red]✗[/red] Rule name and type are required")
1059
+ raise typer.Exit(1)
1060
+
1061
+ # Create sample response based on rule type
1062
+ sample_response = {
1063
+ "status_code": 200,
1064
+ "body": {"message": f"Response for {rule_type} rule"},
1065
+ "headers": {"Content-Type": "application/json"}
1066
+ }
1067
+
1068
+ if rule_type == "user_type":
1069
+ rule = smart_matcher.create_user_type_rule("premium", sample_response)
1070
+ elif rule_type == "api_version":
1071
+ rule = smart_matcher.create_api_version_rule("v2", sample_response)
1072
+ elif rule_type == "premium_user":
1073
+ rule = smart_matcher.create_premium_user_rule(sample_response)
1074
+ elif rule_type == "rate_limit":
1075
+ rule = smart_matcher.create_rate_limit_rule(100, sample_response)
1076
+ elif rule_type == "error":
1077
+ rule = smart_matcher.create_error_rule("invalid_token", sample_response)
1078
+ elif rule_type == "performance":
1079
+ rule = smart_matcher.create_performance_rule((1, 3), sample_response)
1080
+ else:
1081
+ console.print(f"[red]✗[/red] Unknown rule type: {rule_type}")
1082
+ raise typer.Exit(1)
1083
+
1084
+ rule.name = rule_name
1085
+ smart_matcher.add_rule(rule)
1086
+ console.print(f"[green]✓[/green] Created rule: {rule_name}")
1087
+
1088
+ elif action == "test":
1089
+ if not test_request:
1090
+ console.print("[red]✗[/red] Test request is required")
1091
+ raise typer.Exit(1)
1092
+
1093
+ try:
1094
+ request_data = json.loads(test_request)
1095
+ except json.JSONDecodeError:
1096
+ console.print("[red]✗[/red] Invalid JSON in test request")
1097
+ raise typer.Exit(1)
1098
+
1099
+ response, rule = smart_matcher.find_matching_response(request_data)
1100
+
1101
+ if response:
1102
+ console.print(f"[green]✓[/green] Matched rule: {rule.name if rule else 'Default'}")
1103
+ console.print(f"Response: {json.dumps(response, indent=2)}")
1104
+ else:
1105
+ console.print("[yellow]No matching rule found[/yellow]")
1106
+
1107
+ elif action == "export":
1108
+ if not output_file:
1109
+ output_file = "smart_rules.json"
1110
+
1111
+ data = smart_matcher.export_rules()
1112
+ with open(output_file, 'w') as f:
1113
+ f.write(data)
1114
+ console.print(f"[green]✓[/green] Exported rules to: {output_file}")
1115
+
1116
+ elif action == "import":
1117
+ if not config_file:
1118
+ console.print("[red]✗[/red] Config file is required")
1119
+ raise typer.Exit(1)
1120
+
1121
+ with open(config_file, 'r') as f:
1122
+ data = f.read()
1123
+
1124
+ smart_matcher.import_rules(data)
1125
+ console.print(f"[green]✓[/green] Imported rules from: {config_file}")
1126
+
1127
+ elif action == "stats":
1128
+ stats = smart_matcher.get_matching_statistics()
1129
+
1130
+ table = Table(title="Smart Matching Statistics")
1131
+ table.add_column("Metric", style="cyan")
1132
+ table.add_column("Value", style="green")
1133
+
1134
+ table.add_row("Total Rules", str(stats["total_rules"]))
1135
+ table.add_row("No Match Count", str(stats["no_match_count"]))
1136
+
1137
+ for rule_name, count in stats["rule_usage"].items():
1138
+ table.add_row(f"Rule: {rule_name}", str(count))
1139
+
1140
+ console.print(table)
1141
+
1142
+ else:
1143
+ console.print(f"[red]✗[/red] Unknown action: {action}")
1144
+ raise typer.Exit(1)
1145
+
1146
+ except Exception as e:
1147
+ console.print(f"[red]✗[/red] Smart matching error: {e}")
1148
+ raise typer.Exit(1)
1149
+
1150
+
1151
+ @app.command()
1152
+ def enhanced_analytics(
1153
+ action: str = typer.Argument(..., help="Enhanced analytics action (performance, patterns, dependencies, insights, summary, export)"),
1154
+ endpoint: str = typer.Option(None, "--endpoint", help="Specific endpoint to analyze"),
1155
+ hours: int = typer.Option(24, "--hours", help="Time period for analysis (hours)"),
1156
+ output_file: str = typer.Option(None, "--output", help="Output file for export"),
1157
+ format: str = typer.Option("json", "--format", help="Export format (json, csv)"),
1158
+ ):
1159
+ """Enhanced analytics with performance benchmarking and insights."""
1160
+ try:
1161
+ # Create enhanced analytics instance
1162
+ analytics = EnhancedAnalytics()
1163
+ if action == "performance":
1164
+ metrics = analytics.calculate_performance_metrics(endpoint, hours)
1165
+
1166
+ if not metrics:
1167
+ console.print("[yellow]No performance data found[/yellow]")
1168
+ return
1169
+
1170
+ table = Table(title=f"Performance Metrics (Last {hours} hours)")
1171
+ table.add_column("Endpoint", style="cyan")
1172
+ table.add_column("Method", style="green")
1173
+ table.add_column("P50 (ms)", style="yellow")
1174
+ table.add_column("P95 (ms)", style="yellow")
1175
+ table.add_column("P99 (ms)", style="yellow")
1176
+ table.add_column("Throughput", style="blue")
1177
+ table.add_column("Error Rate", style="red")
1178
+
1179
+ for metric in metrics:
1180
+ table.add_row(
1181
+ metric.endpoint,
1182
+ metric.method,
1183
+ f"{metric.response_time_p50:.2f}",
1184
+ f"{metric.response_time_p95:.2f}",
1185
+ f"{metric.response_time_p99:.2f}",
1186
+ f"{metric.throughput:.2f}",
1187
+ f"{metric.error_rate:.2%}"
1188
+ )
1189
+
1190
+ console.print(table)
1191
+
1192
+ elif action == "patterns":
1193
+ patterns = analytics.analyze_usage_patterns(endpoint, hours//24)
1194
+
1195
+ if not patterns:
1196
+ console.print("[yellow]No usage pattern data found[/yellow]")
1197
+ return
1198
+
1199
+ table = Table(title=f"Usage Patterns (Last {hours//24} days)")
1200
+ table.add_column("Endpoint", style="cyan")
1201
+ table.add_column("Method", style="green")
1202
+ table.add_column("Peak Hours", style="yellow")
1203
+ table.add_column("Peak Days", style="blue")
1204
+ table.add_column("Top User Agent", style="white")
1205
+
1206
+ for pattern in patterns:
1207
+ peak_hours = ", ".join(map(str, pattern.peak_hours[:3]))
1208
+ peak_days = ", ".join(pattern.peak_days[:3])
1209
+ top_ua = list(pattern.user_agents.keys())[0] if pattern.user_agents else "N/A"
1210
+
1211
+ table.add_row(
1212
+ pattern.endpoint,
1213
+ pattern.method,
1214
+ peak_hours,
1215
+ peak_days,
1216
+ top_ua[:30] + "..." if len(top_ua) > 30 else top_ua
1217
+ )
1218
+
1219
+ console.print(table)
1220
+
1221
+ elif action == "dependencies":
1222
+ dependencies = analytics.detect_api_dependencies(hours)
1223
+
1224
+ if not dependencies:
1225
+ console.print("[yellow]No API dependencies found[/yellow]")
1226
+ return
1227
+
1228
+ table = Table(title=f"API Dependencies (Last {hours} hours)")
1229
+ table.add_column("Source", style="cyan")
1230
+ table.add_column("Target", style="green")
1231
+ table.add_column("Type", style="yellow")
1232
+ table.add_column("Confidence", style="blue")
1233
+ table.add_column("Frequency", style="white")
1234
+ table.add_column("Avg Latency (ms)", style="red")
1235
+
1236
+ for dep in dependencies:
1237
+ table.add_row(
1238
+ dep.source_endpoint,
1239
+ dep.target_endpoint,
1240
+ dep.dependency_type,
1241
+ f"{dep.confidence:.2%}",
1242
+ str(dep.frequency),
1243
+ f"{dep.avg_latency:.2f}"
1244
+ )
1245
+
1246
+ console.print(table)
1247
+
1248
+ elif action == "insights":
1249
+ insights = analytics.generate_cost_optimization_insights()
1250
+
1251
+ if not insights:
1252
+ console.print("[yellow]No cost optimization insights found[/yellow]")
1253
+ return
1254
+
1255
+ table = Table(title="Cost Optimization Insights")
1256
+ table.add_column("Type", style="cyan")
1257
+ table.add_column("Description", style="white")
1258
+ table.add_column("Potential Savings", style="green")
1259
+ table.add_column("Priority", style="yellow")
1260
+ table.add_column("Recommendation", style="blue")
1261
+
1262
+ for insight in insights:
1263
+ table.add_row(
1264
+ insight.insight_type,
1265
+ insight.description[:50] + "..." if len(insight.description) > 50 else insight.description,
1266
+ f"${insight.potential_savings:.2f}",
1267
+ insight.priority,
1268
+ insight.recommendation[:50] + "..." if len(insight.recommendation) > 50 else insight.recommendation
1269
+ )
1270
+
1271
+ console.print(table)
1272
+
1273
+ elif action == "summary":
1274
+ summary = analytics.get_analytics_summary(hours)
1275
+
1276
+ table = Table(title=f"Enhanced Analytics Summary ({summary['time_period']})")
1277
+ table.add_column("Metric", style="cyan")
1278
+ table.add_column("Value", style="green")
1279
+
1280
+ table.add_row("Total Requests", str(summary["total_requests"]))
1281
+ table.add_row("Total Errors", str(summary["total_errors"]))
1282
+ table.add_row("Error Rate", f"{summary['error_rate']:.2%}")
1283
+ table.add_row("Avg Response Time", f"{summary['avg_response_time']:.2f}ms")
1284
+ table.add_row("Endpoints Analyzed", str(summary["endpoints_analyzed"]))
1285
+ table.add_row("Usage Patterns", str(summary["usage_patterns"]))
1286
+ table.add_row("Dependencies Found", str(summary["dependencies_found"]))
1287
+ table.add_row("Cost Insights", str(summary["cost_insights"]))
1288
+
1289
+ console.print(table)
1290
+
1291
+ elif action == "export":
1292
+ if not output_file:
1293
+ output_file = f"enhanced_analytics_{action}_{hours}h.{format}"
1294
+
1295
+ data = analytics.export_analytics(format, hours)
1296
+ with open(output_file, 'w') as f:
1297
+ f.write(data)
1298
+ console.print(f"[green]✓[/green] Exported analytics to: {output_file}")
1299
+
1300
+ else:
1301
+ console.print(f"[red]✗[/red] Unknown action: {action}")
1302
+ raise typer.Exit(1)
1303
+
1304
+ except Exception as e:
1305
+ console.print(f"[red]✗[/red] Enhanced analytics error: {e}")
1306
+ raise typer.Exit(1)
1307
+
1308
+
904
1309
  if __name__ == "__main__":
905
1310
  app()