11package com .sap .ai .sdk .app .controllers ;
22
3+ import com .fasterxml .jackson .annotation .JsonAutoDetect ;
4+ import com .fasterxml .jackson .annotation .PropertyAccessor ;
5+ import com .fasterxml .jackson .core .JsonProcessingException ;
6+ import com .fasterxml .jackson .databind .ObjectMapper ;
7+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
38import com .sap .ai .sdk .core .client .ScenarioApi ;
49import com .sap .ai .sdk .core .model .AiModelList ;
510import com .sap .ai .sdk .core .model .AiScenarioList ;
611import javax .annotation .Nonnull ;
12+
13+ import org .springframework .http .MediaType ;
14+ import org .springframework .http .ResponseEntity ;
715import org .springframework .web .bind .annotation .GetMapping ;
16+ import org .springframework .web .bind .annotation .RequestHeader ;
817import org .springframework .web .bind .annotation .RestController ;
918
1019/** Endpoint for Scenario operations */
1120@ RestController
1221@ SuppressWarnings ("unused" ) // debug method that doesn't need to be tested
1322class ScenarioController {
1423
24+ // TODO: Fix javadocs
25+
1526 private static final ScenarioApi CLIENT = new ScenarioApi ();
27+ private final ObjectMapper mapper =
28+ new ObjectMapper ()
29+ .setVisibility (PropertyAccessor .FIELD , JsonAutoDetect .Visibility .ANY )
30+ .registerModule (new JavaTimeModule ());
1631
1732 /**
1833 * Get the list of available scenarios
@@ -21,8 +36,21 @@ class ScenarioController {
2136 */
2237 @ GetMapping ("/scenarios" )
2338 @ Nonnull
24- AiScenarioList getScenarios () {
25- return CLIENT .query ("default" );
39+ ResponseEntity <String > getScenarios (
40+ @ RequestHeader (value = "accept" , required = false ) final String accept )
41+ throws JsonProcessingException {
42+ var scenarioList = CLIENT .query ("default" );
43+ if ("application/json" .equals (accept )) {
44+ return ResponseEntity .ok ()
45+ .contentType (MediaType .APPLICATION_JSON )
46+ .body (mapper .writeValueAsString (scenarioList ));
47+ }
48+ return ResponseEntity .ok (buildScenarioMessage (scenarioList ));
49+ }
50+
51+ @ Nonnull
52+ AiModelList getModels () {
53+ return CLIENT .queryModels ("foundation-models" , "default" );
2654 }
2755
2856 /**
@@ -32,7 +60,33 @@ AiScenarioList getScenarios() {
3260 */
3361 @ GetMapping ("/models" )
3462 @ Nonnull
35- AiModelList getModels () {
36- return CLIENT .queryModels ("foundation-models" , "default" );
63+ ResponseEntity <String > getModels (
64+ @ RequestHeader (value = "accept" , required = false ) final String accept )
65+ throws JsonProcessingException {
66+ var modelList = CLIENT .queryModels ("foundation-models" , "default" );
67+ if ("application/json" .equals (accept )) {
68+ return ResponseEntity .ok ()
69+ .contentType (MediaType .APPLICATION_JSON )
70+ .body (mapper .writeValueAsString (modelList ));
71+ }
72+ return ResponseEntity .ok (buildModelMessage (modelList ));
73+ }
74+
75+ String buildScenarioMessage (AiScenarioList scenarioList ) {
76+ var message = new StringBuilder ("The following scenarios are available: " );
77+ for (var resource : scenarioList .getResources ()) {
78+ message .append (resource .getName ()).append (", " );
79+ }
80+ message .setCharAt (message .length () - 2 , '.' );
81+ return message .toString ();
82+ }
83+
84+ String buildModelMessage (AiModelList modelList ) {
85+ var message = new StringBuilder ("The following models are available: " );
86+ for (var resource : modelList .getResources ()) {
87+ message .append (resource .getModel ()).append (", " );
88+ }
89+ message .setCharAt (message .length () - 2 , '.' );
90+ return message .toString ();
3791 }
3892}
0 commit comments