Skip to content

Commit

Permalink
update request configuration class (#2034)
Browse files Browse the repository at this point in the history
* Merge dev to master (#2013)

Bump apidoctor from 1708be5 to 1916d8c (#2004)

Add use import statements for PHP Snippets (#2000)

Bump Microsoft.OpenApi.OData from 1.6.0 to 1.6.1 (#2007)

Bump Microsoft.VisualStudio.Azure.Containers.Tools.Targets (#2008)

Bump MSTest.TestFramework from 3.2.2 to 3.3.0 (#2009)

Bump MSTest.TestFramework from 3.3.0 to 3.3.1 (#2011)

Bump Microsoft.VisualStudio.Azure.Containers.Tools.Targets (#2010)

Chore: change drop location (#2012)

* Remove built request configiguation name

* Update unit tests for updated request configuration name

* Update imports to inlcude base request configuration

* update tests

---------

Co-authored-by: Millicent Achieng <achieng.milli@gmail.com>
Co-authored-by: Charles Wahome <thewahome.cw@gmail.com>
  • Loading branch information
3 people authored May 9, 2024
1 parent 867cb0e commit 73429c1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
16 changes: 9 additions & 7 deletions CodeSnippetsReflection.OpenAPI.Test/PythonGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public async Task GeneratesSelectQueryParameters()
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("RequestBuilderGetQueryParameters(", result);
Assert.Contains("select = [\"displayName\",\"id\"]", result);
Assert.Contains("RequestBuilderGetRequestConfiguration(", result);
Assert.Contains("RequestConfiguration(", result);
Assert.Contains("query_parameters = query_params,", result);
}
[Fact]
Expand All @@ -182,7 +182,7 @@ public async Task GeneratesCountBooleanQueryParameters()
Assert.Contains("count = True", result);
Assert.Contains("select = [\"displayName\",\"id\"]", result);
Assert.DoesNotContain("\"true\"", result);
Assert.Contains("RequestBuilderGetRequestConfiguration(", result);
Assert.Contains("RequestConfiguration(", result);
Assert.Contains("query_parameters = query_params,", result);
}
[Fact]
Expand Down Expand Up @@ -211,7 +211,8 @@ public async Task GeneratesRequestHeaders()
requestPayload.Headers.Add("ConsistencyLevel", "eventual");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("request_configuration = GroupsRequestBuilder.GroupsRequestBuilderGetRequestConfiguration()", result);
Assert.Contains("request_configuration = RequestConfiguration()", result);
Assert.Contains("from kiota_abstractions.base_request_configuration import RequestConfiguration", result);
Assert.Contains("request_configuration.headers.add(\"ConsistencyLevel\", \"eventual\")", result);
}
[Fact]
Expand Down Expand Up @@ -244,7 +245,8 @@ public async Task GeneratesSnippetForRequestWithDeltaAndSkipToken()
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("DeltaRequestBuilderGetQueryParameters(", result);
Assert.Contains("skiptoken = \"R0usmcCM996atia_s\",", result);
Assert.Contains("request_configuration = DeltaRequestBuilder.DeltaRequestBuilderGetRequestConfiguration(", result);
Assert.Contains("request_configuration = RequestConfiguration(", result);
Assert.Contains("from kiota_abstractions.base_request_configuration import RequestConfiguration", result);
Assert.Contains("query_parameters = query_params,", result);
Assert.Contains("request_configuration.headers.add(\"Prefer\", \"odata.maxpagesize=2\")", result);
Assert.Contains("result = await graph_client.me.calendar_view.delta.get", result);
Expand Down Expand Up @@ -651,7 +653,7 @@ public async Task CorrectlyHandlesOdataFunction()
Assert.Contains("await graph_client.users.delta.get(request_configuration = request_configuration)", result);
Assert.Contains("query_params = DeltaRequestBuilder.DeltaRequestBuilderGetQueryParameters(", result);
Assert.Contains("select = [\"displayName\",\"jobTitle\",\"mobilePhone\"]", result);
Assert.Contains("request_configuration = DeltaRequestBuilder.DeltaRequestBuilderGetRequestConfiguration(", result);
Assert.Contains("request_configuration = RequestConfiguration(", result);
}
[Fact]
public async Task CorrectlyHandlesDateTimeOffsetInUrl()
Expand Down Expand Up @@ -886,7 +888,7 @@ public async Task CorrectlyHandlesTypeFromInUrl()

Assert.Contains("query_params = MailFoldersRequestBuilder.MailFoldersRequestBuilderGetQueryParameters(", result);
Assert.Contains("include_hidden_folders = \"true\"", result);
Assert.Contains("request_configuration = MailFoldersRequestBuilder.MailFoldersRequestBuilderGetRequestConfiguration(", result);
Assert.Contains("request_configuration = RequestConfiguration(", result);
}
[Fact]
public async Task MatchesPathWithPathParameter()
Expand Down Expand Up @@ -1197,6 +1199,6 @@ public async Task GeneratesCorrectRequestBuilderNameForIndexedCollections()
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("query_params = UserItemRequestBuilder.UserItemRequestBuilderGetQueryParameters(", result);
Assert.Contains("request_configuration = UserItemRequestBuilder.UserItemRequestBuilderGetRequestConfiguration(", result);
Assert.Contains("request_configuration = RequestConfiguration(", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public async Task GeneratesRequestBuilderImports()
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("from msgraph import GraphServiceClient", result);
Assert.Contains("from kiota_abstractions.base_request_configuration import RequestConfiguration", result);
Assert.Contains("from msgraph.generated.users.item.calendar.events.events_request_builder import EventsRequestBuilder", result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class PythonGenerator : ILanguageGenerator<SnippetModel, OpenApiUrlTreeNo
private const string ScopesVarName = "scopes";
private const string RequestBodyVarName = "request_body";
private const string RequestConfigurationVarName = "request_configuration";
private const string RequestConfigurationType = "RequestConfiguration";
private const string RequestParametersPropertyName = "query_params";

private static readonly HashSet<string> ReservedTypeNames = new(StringComparer.OrdinalIgnoreCase)
Expand Down Expand Up @@ -79,6 +80,7 @@ private static HashSet<string> GetImportStatements(SnippetModel snippetModel)
{
const string modelImportPrefix = "from msgraph.generated.models";
const string requestBuilderImportPrefix = "from msgraph.generated";
const string BaseRequestConfigImport = "from kiota_abstractions.base_request_configuration import RequestConfiguration";

var snippetImports = new HashSet<string>();

Expand All @@ -96,6 +98,7 @@ private static HashSet<string> GetImportStatements(SnippetModel snippetModel)
var namespaceParts = import.ModelProperty.NamespaceName.Split('.').Select((s, i) => i == import.ModelProperty.NamespaceName.Split('.').Length - 1 ? s.ToSnakeCase() : s.ToLowerInvariant());
var importString = $"{requestBuilderImportPrefix}.{string.Join(".", namespaceParts)}.{typeDefinition.ToSnakeCase()} import {typeDefinition}";
snippetImports.Add($"{importString.Replace(".me.", ".users.item.")}");

}
else{
snippetImports.Add($"{modelImportPrefix}.{typeDefinition.ToSnakeCase()} import {typeDefinition}");
Expand All @@ -119,6 +122,8 @@ private static HashSet<string> GetImportStatements(SnippetModel snippetModel)
{
//construct path to request builder
snippetImports.Add($"{requestBuilderImportPrefix}{Regex.Replace(import.Path.Replace(".me.", ".users.item."), @"(\B[A-Z])", "_$1", RegexOptions.Compiled, TimeSpan.FromSeconds(60)).ToLower()}.{import.RequestBuilderName.ToSnakeCase()} import {import.RequestBuilderName}");
snippetImports.Add($"{BaseRequestConfigImport}");

}
break;
}
Expand Down Expand Up @@ -188,7 +193,7 @@ private static string GetRequestConfiguration(SnippetCodeGraph codeGraph, Indent
if (codeGraph.HasParameters() || codeGraph.HasHeaders())
{
snippetBuilder.AppendLine(queryParamsPayload);
snippetBuilder.Append($"{RequestConfigurationVarName} = {requestBuilderName}.{requestConfigurationName}(");
snippetBuilder.Append($"{RequestConfigurationVarName} = {RequestConfigurationType}(");

if (codeGraph.HasParameters())
{
Expand Down

0 comments on commit 73429c1

Please sign in to comment.