/// <summary> /// Formulates the requested Graph snippets and returns it as string for Java /// </summary> /// <param name="snippetModel">Model of the Snippets info <see cref="SnippetModel"/></param> /// <param name="languageExpressions">The language expressions to be used for code Gen</param> /// <returns>String of the snippet in Java code</returns> public string GenerateCodeSnippet(SnippetModel snippetModel, LanguageExpressions languageExpressions) { var snippetBuilder = new StringBuilder(); try { var segment = snippetModel.Segments.Last(); snippetModel.ResponseVariableName = CommonGenerator.EnsureVariableNameIsNotReserved(snippetModel.ResponseVariableName, languageExpressions); /*Auth provider section*/ snippetBuilder.Append("GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();\r\n\r\n"); //append any request options present snippetBuilder.Append(GenerateRequestOptionsSection(snippetModel, languageExpressions)); /*Generate the query section of the request*/ var requestActions = CommonGenerator.GenerateQuerySection(snippetModel, languageExpressions); if (snippetModel.Method == HttpMethod.Get) { var typeName = GetJavaReturnTypeName(segment); snippetBuilder.Append($"{typeName} {snippetModel.ResponseVariableName} = "); if (segment is PropertySegment) { return(GenerateCustomRequestForPropertySegment(snippetBuilder, snippetModel)); } snippetBuilder.Append(GenerateRequestSection(snippetModel, $"{requestActions}\r\n\t.get();")); } else if (snippetModel.Method == HttpMethod.Post) { switch (segment) { case NavigationPropertySegment _: case EntitySetSegment _: case NavigationPropertyLinkSegment _: if (string.IsNullOrEmpty(snippetModel.RequestBody)) { throw new Exception("No request Body present for Java POST request"); } snippetBuilder.Append(JavaGenerateObjectFromJson(segment, snippetModel.RequestBody, new List <string> { snippetModel.ResponseVariableName })); snippetBuilder.Append(GenerateRequestSection(snippetModel, $"{requestActions}\r\n\t.post({snippetModel.ResponseVariableName});")); break; case OperationSegment _: //deserialize the object since the json top level contains the list of parameter objects if (JsonConvert.DeserializeObject(snippetModel.RequestBody) is JObject testObj) { foreach (var(key, jToken) in testObj) { var jsonString = JsonConvert.SerializeObject(jToken); snippetBuilder.Append(JavaGenerateObjectFromJson(segment, jsonString, new List <string> { CommonGenerator.LowerCaseFirstLetter(key) })); } } snippetBuilder.Append(GenerateRequestSection(snippetModel, $"{requestActions}\r\n\t.post();")); break; default: throw new Exception("Unknown Segment Type in URI for method POST"); } } else if (snippetModel.Method == HttpMethod.Patch) { if (string.IsNullOrEmpty(snippetModel.RequestBody)) { throw new Exception("No request Body present for Java Patch request"); } snippetBuilder.Append(JavaGenerateObjectFromJson(segment, snippetModel.RequestBody, new List <string> { snippetModel.ResponseVariableName })); if (segment is PropertySegment) { return(GenerateCustomRequestForPropertySegment(snippetBuilder, snippetModel)); } snippetBuilder.Append(GenerateRequestSection(snippetModel, $"{requestActions}\r\n\t.patch({snippetModel.ResponseVariableName});")); } else if (snippetModel.Method == HttpMethod.Put) { if (string.IsNullOrEmpty(snippetModel.RequestBody)) { throw new Exception("No request Body present for Java Put request"); } if (snippetModel.ContentType?.Contains("json") ?? false) { snippetBuilder.Append(JavaGenerateObjectFromJson(segment, snippetModel.RequestBody, new List <string> { snippetModel.ResponseVariableName })); } else { snippetBuilder.Append($"byte[] {snippetModel.ResponseVariableName} = Base64.getDecoder().decode({AddQuotesIfMising(snippetModel.RequestBody?.Replace("\n", string.Empty)?.Replace("\r", string.Empty))});\r\n\t"); } snippetBuilder.Append(GenerateRequestSection(snippetModel, $"{requestActions}\r\n\t.put({snippetModel.ResponseVariableName});")); } else if (snippetModel.Method == HttpMethod.Delete) { snippetBuilder.Append(GenerateRequestSection(snippetModel, $"{requestActions}\r\n\t.delete();")); } else { throw new NotImplementedException("HTTP method not implemented for Java"); } return(snippetBuilder.ToString()); } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// Formulates the requested Graph snippets and returns it as string for JavaScript /// </summary> /// <param name="snippetModel">Model of the Snippets info <see cref="SnippetModel"/></param> /// <param name="languageExpressions">The language expressions to be used for code Gen</param> /// <returns>String of the snippet in Javascript code</returns> public static string GenerateCodeSnippet(SnippetModel snippetModel, LanguageExpressions languageExpressions) { try { var snippetBuilder = new StringBuilder(); snippetModel.ResponseVariableName = CommonGenerator.EnsureVariableNameIsNotReserved(snippetModel.ResponseVariableName, languageExpressions); //setup the auth snippet section snippetBuilder.Append("const options = {\r\n"); snippetBuilder.Append("\tauthProvider,\r\n};\r\n\r\n"); //init the client snippetBuilder.Append("const client = Client.init(options);\r\n\r\n"); if (snippetModel.Method == HttpMethod.Get) { //append any queries with the actions snippetBuilder.Append($"let {snippetModel.ResponseVariableName} = "); var getActions = CommonGenerator.GenerateQuerySection(snippetModel, languageExpressions) + "\r\n\t.get();"; snippetBuilder.Append(GenerateRequestSection(snippetModel, getActions)); } else if (snippetModel.Method == HttpMethod.Post) { if (!string.IsNullOrEmpty(snippetModel.RequestBody)) { snippetBuilder.Append(JavascriptGenerateObjectFromJson(snippetModel.RequestBody, snippetModel.ResponseVariableName)); snippetBuilder.Append(GenerateRequestSection(snippetModel, $"\r\n\t.post({snippetModel.ResponseVariableName});")); } else { snippetBuilder.Append(GenerateRequestSection(snippetModel, "\r\n\t.post();")); } } else if (snippetModel.Method == HttpMethod.Patch) { if (string.IsNullOrEmpty(snippetModel.RequestBody)) { throw new ArgumentException("No body present for PATCH method in Javascript"); } snippetBuilder.Append(JavascriptGenerateObjectFromJson(snippetModel.RequestBody, snippetModel.ResponseVariableName)); snippetBuilder.Append(GenerateRequestSection(snippetModel, $"\r\n\t.update({snippetModel.ResponseVariableName});")); } else if (snippetModel.Method == HttpMethod.Delete) { snippetBuilder.Append(GenerateRequestSection(snippetModel, "\r\n\t.delete();")); } else if (snippetModel.Method == HttpMethod.Put) { if (string.IsNullOrEmpty(snippetModel.RequestBody)) { throw new ArgumentException("No body present for PUT method in Javascript"); } snippetBuilder.Append(JavascriptGenerateObjectFromJson(snippetModel.RequestBody, snippetModel.ResponseVariableName)); snippetBuilder.Append(GenerateRequestSection(snippetModel, $"\r\n\t.put({snippetModel.ResponseVariableName});")); } else { throw new NotImplementedException("HTTP method not implemented for Javascript"); } return(snippetBuilder.ToString()); } catch (Exception ex) { throw new Exception(ex.Message); } }