Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make auth_header optional to enable using _request_session.headers #632

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ default void emit(PythonPoetWriter poetWriter) {
.add(PythonEndpointParam.builder()
.paramName("authHeader")
.pythonParamName("auth_header")
.myPyType("str")
.isOptional(false)
.myPyType("Optional[str]")
.isOptional(true)
.isCollection(false)
.paramType(ParameterType.header(
HeaderParameterType.of(ParameterId.of("Authorization"))))
Expand All @@ -98,8 +98,14 @@ default void emit(PythonPoetWriter poetWriter) {
.map(param -> {
String typedParam =
String.format("%s: %s", param.pythonParamName(), param.myPyType());
if (param.isOptional() || param.isCollection()) {
if (param.isOptional()) {
return String.format("%s = None", typedParam);
} else if (param.isCollection()) {
return String.format("%s = []", typedParam);
} else if (param.paramType().accept(ParameterTypeVisitor.IS_HEADER)) {
return String.format(
"%s: Optional[%s] = None",
param.pythonParamName(), param.myPyType());
}
return typedParam;
})
Expand All @@ -112,15 +118,6 @@ default void emit(PythonPoetWriter poetWriter) {
poetWriter.writeIndentedLine("\"\"\"");
});

// replace "None" with "[]"
for (PythonEndpointParam param : paramsWithHeader) {
if (param.isCollection()) {
poetWriter.writeIndentedLine(String.format(
"%s = %s if %s is not None else []",
param.pythonParamName(), param.pythonParamName(), param.pythonParamName()));
}
}

// header
poetWriter.writeLine();
poetWriter.writeIndentedLine("_headers: Dict[str, Any] = {");
Expand All @@ -138,19 +135,26 @@ default void emit(PythonPoetWriter poetWriter) {
"'Content-Type': '%s',",
isRequestBinary() ? MediaType.APPLICATION_OCTET_STREAM : MediaType.APPLICATION_JSON);
}
poetWriter.decreaseIndent();
poetWriter.writeIndentedLine("}");

// optional header params
poetWriter.writeLine();
poetWriter.writeIndentedLine("_header_params: Dict[str, Any] = {");
poetWriter.increaseIndent();
paramsWithHeader.stream()
.filter(param -> param.paramType().accept(ParameterTypeVisitor.IS_HEADER))
.forEach(param -> {
poetWriter.writeIndentedLine(
"'%s': %s,",
param.paramType()
.accept(ParameterTypeVisitor.HEADER)
.getParamId()
.get(),
param.pythonParamName());
});
.forEach(param -> poetWriter.writeIndentedLine(
"'%s': %s,",
param.paramType()
.accept(ParameterTypeVisitor.HEADER)
.getParamId()
.get(),
param.pythonParamName()));
poetWriter.decreaseIndent();
poetWriter.writeIndentedLine("}");
poetWriter.writeLine();
poetWriter.writeIndentedLine("_headers.update((k, v) for k, v in _header_params.items() if v is not None)");

// params
poetWriter.writeLine();
Expand Down Expand Up @@ -279,12 +283,30 @@ static Builder builder() {
final class PythonEndpointParamComparator implements Comparator<PythonEndpointParam> {
@Override
public int compare(PythonEndpointParam o1, PythonEndpointParam o2) {
if (o1.isOptional() || o2.isOptional()) {
return compareOptional(o1, o2);
}
if (o1.isCollection() || o2.isCollection()) {
return compareCollection(o1, o2);
}
if (o1.paramType().accept(ParameterTypeVisitor.IS_HEADER)
|| o2.paramType().accept(ParameterTypeVisitor.IS_HEADER)) {
return compareHeader(o1, o2);
}
return o1.pythonParamName().compareTo(o2.pythonParamName());
}

private int compareOptional(PythonEndpointParam o1, PythonEndpointParam o2) {
if (o1.isOptional() && !o2.isOptional()) {
return 1;
}
if (!o1.isOptional() && o2.isOptional()) {
return -1;
}
return o1.pythonParamName().compareTo(o2.pythonParamName());
}

private int compareCollection(PythonEndpointParam o1, PythonEndpointParam o2) {
if (o1.isCollection() && !o2.isCollection()) {
return 1;
}
Expand All @@ -293,5 +315,17 @@ public int compare(PythonEndpointParam o1, PythonEndpointParam o2) {
}
return o1.pythonParamName().compareTo(o2.pythonParamName());
}

private int compareHeader(PythonEndpointParam o1, PythonEndpointParam o2) {
if (o1.paramType().accept(ParameterTypeVisitor.IS_HEADER)
&& !o2.paramType().accept(ParameterTypeVisitor.IS_HEADER)) {
return 1;
}
if (!o1.paramType().accept(ParameterTypeVisitor.IS_HEADER)
&& o2.paramType().accept(ParameterTypeVisitor.IS_HEADER)) {
return -1;
}
return o1.pythonParamName().compareTo(o2.pythonParamName());
}
}
}
Loading