use of com.palantir.conjure.spec.HttpMethod in project conjure by palantir.
the class ConjureParserUtils method parseEndpoint.
private static EndpointDefinition parseEndpoint(String name, com.palantir.conjure.parser.services.EndpointDefinition def, PathString basePath, Optional<AuthType> defaultAuth, ReferenceTypeResolver typeResolver, DealiasingTypeVisitor dealiasingVisitor) {
HttpPath httpPath = parseHttpPath(def, basePath);
EndpointDefinition endpoint = EndpointDefinition.builder().endpointName(EndpointName.of(name)).httpMethod(HttpMethod.valueOf(def.http().method())).httpPath(httpPath).auth(def.auth().map(ConjureParserUtils::parseAuthType).orElse(defaultAuth)).args(parseArgs(def.args(), httpPath, typeResolver)).tags(def.tags().stream().peek(tag -> Preconditions.checkArgument(!tag.isEmpty(), "tag must not be empty")).collect(Collectors.toSet())).markers(parseMarkers(def.markers(), typeResolver)).returns(def.returns().map(t -> t.visit(new ConjureTypeParserVisitor(typeResolver)))).docs(def.docs().map(Documentation::of)).deprecated(def.deprecated().map(Documentation::of)).build();
EndpointDefinitionValidator.validateAll(endpoint, dealiasingVisitor);
return endpoint;
}
use of com.palantir.conjure.spec.HttpMethod in project conjure-python by palantir.
the class PythonEndpointDefinition method emit.
@SuppressWarnings({ "CyclomaticComplexity", "MethodLength" })
@Override
default void emit(PythonPoetWriter poetWriter) {
poetWriter.maintainingIndent(() -> {
// if auth type is header, insert it as a fake param
boolean isHeaderType = auth().isPresent() && auth().get().accept(AuthTypeVisitor.IS_HEADER);
List<PythonEndpointParam> paramsWithHeader = isHeaderType ? ImmutableList.<PythonEndpointParam>builder().add(PythonEndpointParam.builder().paramName("authHeader").pythonParamName("auth_header").myPyType("str").isOptional(false).paramType(ParameterType.header(HeaderParameterType.of(ParameterId.of("Authorization")))).build()).addAll(params()).build() : params();
poetWriter.writeIndentedLine("def %s(self, %s) -> %s:", pythonMethodName(), Joiner.on(", ").join(paramsWithHeader.stream().sorted(new PythonEndpointParamComparator()).map(param -> {
String typedParam = String.format("%s: %s", param.pythonParamName(), param.myPyType());
if (param.isOptional()) {
return String.format("%s = None", typedParam);
}
return typedParam;
}).collect(Collectors.toList())), myPyReturnType().orElse("None"));
poetWriter.increaseIndent();
docs().ifPresent(docs -> {
poetWriter.writeIndentedLine("\"\"\"");
poetWriter.writeIndentedLine(docs.get().trim());
poetWriter.writeIndentedLine("\"\"\"");
});
// header
poetWriter.writeLine();
poetWriter.writeIndentedLine("_headers: Dict[str, Any] = {");
poetWriter.increaseIndent();
poetWriter.writeIndentedLine("'Accept': '%s',", isResponseBinary() ? MediaType.APPLICATION_OCTET_STREAM : MediaType.APPLICATION_JSON);
// body
Optional<PythonEndpointParam> bodyParam = paramsWithHeader.stream().filter(param -> param.paramType().accept(ParameterTypeVisitor.IS_BODY)).findAny();
if (bodyParam.isPresent()) {
poetWriter.writeIndentedLine("'Content-Type': '%s',", isRequestBinary() ? MediaType.APPLICATION_OCTET_STREAM : MediaType.APPLICATION_JSON);
}
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());
});
poetWriter.decreaseIndent();
poetWriter.writeIndentedLine("}");
// params
poetWriter.writeLine();
poetWriter.writeIndentedLine("_params: Dict[str, Any] = {");
poetWriter.increaseIndent();
paramsWithHeader.stream().filter(param -> param.paramType().accept(ParameterTypeVisitor.IS_QUERY)).forEach(param -> {
poetWriter.writeIndentedLine("'%s': %s,", param.paramType().accept(ParameterTypeVisitor.QUERY).getParamId().get(), param.pythonParamName());
});
poetWriter.decreaseIndent();
poetWriter.writeIndentedLine("}");
// path params
poetWriter.writeLine();
poetWriter.writeIndentedLine("_path_params: Dict[str, Any] = {");
poetWriter.increaseIndent();
// TODO(qchen): no need for param name twice?
paramsWithHeader.stream().filter(param -> param.paramType().accept(ParameterTypeVisitor.IS_PATH)).forEach(param -> {
poetWriter.writeIndentedLine("'%s': %s,", param.paramName(), param.pythonParamName());
});
poetWriter.decreaseIndent();
poetWriter.writeIndentedLine("}");
if (bodyParam.isPresent()) {
if (!isRequestBinary()) {
poetWriter.writeLine();
poetWriter.writeIndentedLine("_json: Any = ConjureEncoder().default(%s)", bodyParam.get().pythonParamName());
} else {
poetWriter.writeLine();
poetWriter.writeIndentedLine("_data: Any = %s", bodyParam.get().pythonParamName());
}
} else {
poetWriter.writeLine();
poetWriter.writeIndentedLine("_json: Any = None");
}
// fix the path, add path params
poetWriter.writeLine();
HttpPath fullPath = httpPath();
String fixedPath = fullPath.toString().replaceAll("\\{(.*):[^}]*}", "{$1}");
poetWriter.writeIndentedLine("_path = '%s'", fixedPath);
poetWriter.writeIndentedLine("_path = _path.format(**_path_params)");
poetWriter.writeLine();
poetWriter.writeIndentedLine("_response: Response = self._request(");
poetWriter.increaseIndent();
poetWriter.writeIndentedLine("'%s',", httpMethod());
poetWriter.writeIndentedLine("self._uri + _path,");
poetWriter.writeIndentedLine("params=_params,");
poetWriter.writeIndentedLine("headers=_headers,");
if (isResponseBinary()) {
poetWriter.writeIndentedLine("stream=True,");
}
if (!isRequestBinary()) {
poetWriter.writeIndentedLine("json=_json)");
} else {
poetWriter.writeIndentedLine("data=_data)");
}
poetWriter.decreaseIndent();
poetWriter.writeLine();
if (isResponseBinary()) {
poetWriter.writeIndentedLine("_raw = _response.raw");
poetWriter.writeIndentedLine("_raw.decode_content = True");
poetWriter.writeIndentedLine("return _raw");
} else if (pythonReturnType().isPresent()) {
poetWriter.writeIndentedLine("_decoder = ConjureDecoder()");
if (isOptionalReturnType()) {
poetWriter.writeIndentedLine("return None if _response.status_code == 204 else _decoder.decode(_response.json(), %s)", pythonReturnType().get());
} else {
poetWriter.writeIndentedLine("return _decoder.decode(_response.json(), %s)", pythonReturnType().get());
}
} else {
poetWriter.writeIndentedLine("return");
}
poetWriter.decreaseIndent();
});
}
Aggregations