Search in sources :

Example 1 with ParamsBlock

use of com.robotoworks.mechanoid.net.netModel.ParamsBlock in project mechanoid by robotoworks.

the class RequestGenerator method generateRequestClass.

public CharSequence generateRequestClass(final HttpMethod method, final Model module, final Client client) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("public class ");
    String _name = method.getName();
    String _pascalize = Strings.pascalize(_name);
    _builder.append(_pascalize, "");
    _builder.append("Request extends ");
    {
        boolean _hasBody = ModelExtensions.hasBody(method);
        if (_hasBody) {
            _builder.append("EntityEnclosed");
        }
    }
    _builder.append("ServiceRequest {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("private static final String PATH = \"");
    String _pathAsFormatString = ModelExtensions.getPathAsFormatString(method, this.serializer);
    _builder.append(_pathAsFormatString, "\t");
    _builder.append("\";");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    {
        Path _path = method.getPath();
        EList<SimpleMemberAssignment> _params = null;
        if (_path != null) {
            _params = _path.getParams();
        }
        int _size = 0;
        if (_params != null) {
            _size = _params.size();
        }
        boolean _greaterThan = (_size > 0);
        if (_greaterThan) {
            {
                Path _path_1 = method.getPath();
                EList<SimpleMemberAssignment> _params_1 = _path_1.getParams();
                for (final SimpleMemberAssignment slug : _params_1) {
                    _builder.append("\t");
                    _builder.append("private final ");
                    SimpleMember _member = slug.getMember();
                    IntrinsicType _type = _member.getType();
                    String _signature = ModelExtensions.signature(_type);
                    _builder.append(_signature, "\t");
                    _builder.append(" ");
                    SimpleMember _member_1 = slug.getMember();
                    String _name_1 = _member_1.getName();
                    String _camelize = Strings.camelize(_name_1);
                    _builder.append(_camelize, "\t");
                    _builder.append("Segment;");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.newLine();
        }
    }
    _builder.append("\t");
    ParamsBlock methodParams = ModelExtensions.getParamsBlock(method);
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    ParamsBlock clientParams = ModelExtensions.getParamsBlock(client);
    _builder.newLineIfNotEmpty();
    {
        boolean _notEquals = (!Objects.equal(methodParams, null));
        if (_notEquals) {
            {
                EList<SimpleMemberAssignment> _params_2 = methodParams.getParams();
                for (final SimpleMemberAssignment param : _params_2) {
                    _builder.append("\t");
                    _builder.append("private ");
                    SimpleMember _member_2 = param.getMember();
                    IntrinsicType _type_1 = _member_2.getType();
                    String _signature_1 = ModelExtensions.signature(_type_1);
                    _builder.append(_signature_1, "\t");
                    _builder.append(" ");
                    SimpleMember _member_3 = param.getMember();
                    String _name_2 = _member_3.getName();
                    String _camelize_1 = Strings.camelize(_name_2);
                    _builder.append(_camelize_1, "\t");
                    _builder.append("Param");
                    {
                        Literal _defaultValue = param.getDefaultValue();
                        boolean _notEquals_1 = (!Objects.equal(_defaultValue, null));
                        if (_notEquals_1) {
                            _builder.append(" = ");
                            Literal _defaultValue_1 = param.getDefaultValue();
                            String _convertToJavaLiteral = ModelExtensions.convertToJavaLiteral(_defaultValue_1);
                            _builder.append(_convertToJavaLiteral, "\t");
                        }
                    }
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("private boolean ");
                    SimpleMember _member_4 = param.getMember();
                    String _name_3 = _member_4.getName();
                    String _camelize_2 = Strings.camelize(_name_3);
                    _builder.append(_camelize_2, "\t");
                    _builder.append("ParamSet");
                    {
                        Literal _defaultValue_2 = param.getDefaultValue();
                        boolean _notEquals_2 = (!Objects.equal(_defaultValue_2, null));
                        if (_notEquals_2) {
                            _builder.append(" = true");
                        }
                    }
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.append("\t");
            _builder.newLine();
        }
    }
    {
        boolean _notEquals_3 = (!Objects.equal(clientParams, null));
        if (_notEquals_3) {
            {
                EList<SimpleMemberAssignment> _params_3 = clientParams.getParams();
                for (final SimpleMemberAssignment param_1 : _params_3) {
                    _builder.append("\t");
                    _builder.append("private ");
                    SimpleMember _member_5 = param_1.getMember();
                    IntrinsicType _type_2 = _member_5.getType();
                    String _signature_2 = ModelExtensions.signature(_type_2);
                    _builder.append(_signature_2, "\t");
                    _builder.append(" ");
                    SimpleMember _member_6 = param_1.getMember();
                    String _name_4 = _member_6.getName();
                    String _camelize_3 = Strings.camelize(_name_4);
                    _builder.append(_camelize_3, "\t");
                    _builder.append("Param");
                    {
                        Literal _defaultValue_3 = param_1.getDefaultValue();
                        boolean _notEquals_4 = (!Objects.equal(_defaultValue_3, null));
                        if (_notEquals_4) {
                            _builder.append(" = ");
                            Literal _defaultValue_4 = param_1.getDefaultValue();
                            String _convertToJavaLiteral_1 = ModelExtensions.convertToJavaLiteral(_defaultValue_4);
                            _builder.append(_convertToJavaLiteral_1, "\t");
                        }
                    }
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("private boolean ");
                    SimpleMember _member_7 = param_1.getMember();
                    String _name_5 = _member_7.getName();
                    String _camelize_4 = Strings.camelize(_name_5);
                    _builder.append(_camelize_4, "\t");
                    _builder.append("ParamSet");
                    {
                        Literal _defaultValue_5 = param_1.getDefaultValue();
                        boolean _notEquals_5 = (!Objects.equal(_defaultValue_5, null));
                        if (_notEquals_5) {
                            _builder.append(" = true");
                        }
                    }
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.append("\t");
            _builder.newLine();
        }
    }
    {
        boolean _hasBody_1 = ModelExtensions.hasBody(method);
        if (_hasBody_1) {
            _builder.append("\t");
            BodyBlock _body = ModelExtensions.getBody(method);
            BlockType _type_3 = _body.getType();
            CharSequence _generateFieldForType = this.generateFieldForType(_type_3);
            _builder.append(_generateFieldForType, "\t");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            BodyBlock _body_1 = ModelExtensions.getBody(method);
            BlockType _type_4 = _body_1.getType();
            CharSequence _generateGetterSetterForType = this.generateGetterSetterForType(_type_4);
            _builder.append(_generateGetterSetterForType, "\t");
            _builder.newLineIfNotEmpty();
        }
    }
    {
        boolean _notEquals_6 = (!Objects.equal(methodParams, null));
        if (_notEquals_6) {
            {
                EList<SimpleMemberAssignment> _params_4 = methodParams.getParams();
                for (final SimpleMemberAssignment param_2 : _params_4) {
                    _builder.append("\t");
                    _builder.append("public ");
                    String _name_6 = method.getName();
                    String _pascalize_1 = Strings.pascalize(_name_6);
                    _builder.append(_pascalize_1, "\t");
                    _builder.append("Request set");
                    SimpleMember _member_8 = param_2.getMember();
                    String _name_7 = _member_8.getName();
                    String _pascalize_2 = Strings.pascalize(_name_7);
                    _builder.append(_pascalize_2, "\t");
                    _builder.append("Param(");
                    SimpleMember _member_9 = param_2.getMember();
                    IntrinsicType _type_5 = _member_9.getType();
                    String _signature_3 = ModelExtensions.signature(_type_5);
                    _builder.append(_signature_3, "\t");
                    _builder.append(" value) {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("this.");
                    SimpleMember _member_10 = param_2.getMember();
                    String _name_8 = _member_10.getName();
                    String _camelize_5 = Strings.camelize(_name_8);
                    _builder.append(_camelize_5, "\t\t");
                    _builder.append("Param = value;");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("this.");
                    SimpleMember _member_11 = param_2.getMember();
                    String _name_9 = _member_11.getName();
                    String _camelize_6 = Strings.camelize(_name_9);
                    _builder.append(_camelize_6, "\t\t");
                    _builder.append("ParamSet = true;");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("return this;");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("}");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("public boolean is");
                    SimpleMember _member_12 = param_2.getMember();
                    String _name_10 = _member_12.getName();
                    String _pascalize_3 = Strings.pascalize(_name_10);
                    _builder.append(_pascalize_3, "\t");
                    _builder.append("ParamSet() {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("return ");
                    SimpleMember _member_13 = param_2.getMember();
                    String _name_11 = _member_13.getName();
                    String _camelize_7 = Strings.camelize(_name_11);
                    _builder.append(_camelize_7, "\t\t");
                    _builder.append("ParamSet;");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
            _builder.append("\t");
            _builder.append("\t");
            _builder.newLine();
        }
    }
    {
        boolean _notEquals_7 = (!Objects.equal(clientParams, null));
        if (_notEquals_7) {
            {
                EList<SimpleMemberAssignment> _params_5 = clientParams.getParams();
                for (final SimpleMemberAssignment param_3 : _params_5) {
                    _builder.append("\t");
                    _builder.append("public ");
                    String _name_12 = method.getName();
                    String _pascalize_4 = Strings.pascalize(_name_12);
                    _builder.append(_pascalize_4, "\t");
                    _builder.append("Request set");
                    SimpleMember _member_14 = param_3.getMember();
                    String _name_13 = _member_14.getName();
                    String _pascalize_5 = Strings.pascalize(_name_13);
                    _builder.append(_pascalize_5, "\t");
                    _builder.append("Param(");
                    SimpleMember _member_15 = param_3.getMember();
                    IntrinsicType _type_6 = _member_15.getType();
                    String _signature_4 = ModelExtensions.signature(_type_6);
                    _builder.append(_signature_4, "\t");
                    _builder.append(" value) {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("this.");
                    SimpleMember _member_16 = param_3.getMember();
                    String _name_14 = _member_16.getName();
                    String _camelize_8 = Strings.camelize(_name_14);
                    _builder.append(_camelize_8, "\t\t");
                    _builder.append("Param = value;");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("this.");
                    SimpleMember _member_17 = param_3.getMember();
                    String _name_15 = _member_17.getName();
                    String _camelize_9 = Strings.camelize(_name_15);
                    _builder.append(_camelize_9, "\t\t");
                    _builder.append("ParamSet = true;");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("return this;");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("}");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("public boolean is");
                    SimpleMember _member_18 = param_3.getMember();
                    String _name_16 = _member_18.getName();
                    String _pascalize_6 = Strings.pascalize(_name_16);
                    _builder.append(_pascalize_6, "\t");
                    _builder.append("ParamSet() {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("return ");
                    SimpleMember _member_19 = param_3.getMember();
                    String _name_17 = _member_19.getName();
                    String _camelize_10 = Strings.camelize(_name_17);
                    _builder.append(_camelize_10, "\t\t");
                    _builder.append("ParamSet;");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
            _builder.append("\t");
            _builder.append("\t\t");
            _builder.newLine();
        }
    }
    _builder.append("\t");
    _builder.append("public ");
    String _name_18 = method.getName();
    String _pascalize_7 = Strings.pascalize(_name_18);
    _builder.append(_pascalize_7, "\t");
    _builder.append("Request(");
    Path _path_2 = method.getPath();
    BodyBlock _body_2 = ModelExtensions.getBody(method);
    String _generateRequestConstructorArgs = this.generateRequestConstructorArgs(_path_2, _body_2);
    _builder.append(_generateRequestConstructorArgs, "\t");
    _builder.append("){");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    HeaderBlock methodHeaders = ModelExtensions.getHeaderBlock(method);
    _builder.newLineIfNotEmpty();
    {
        boolean _notEquals_8 = (!Objects.equal(methodHeaders, null));
        if (_notEquals_8) {
            {
                EList<Header> _headers = methodHeaders.getHeaders();
                for (final Header header : _headers) {
                    _builder.append("\t\t");
                    _builder.append("setHeader(\"");
                    String _name_19 = header.getName();
                    _builder.append(_name_19, "\t\t");
                    _builder.append("\",\"");
                    String _value = header.getValue();
                    _builder.append(_value, "\t\t");
                    _builder.append("\");");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t\t");
            _builder.newLine();
        }
    }
    {
        Path _path_3 = method.getPath();
        EList<SimpleMemberAssignment> _params_6 = null;
        if (_path_3 != null) {
            _params_6 = _path_3.getParams();
        }
        int _size_1 = 0;
        if (_params_6 != null) {
            _size_1 = _params_6.size();
        }
        boolean _greaterThan_1 = (_size_1 > 0);
        if (_greaterThan_1) {
            {
                Path _path_4 = method.getPath();
                EList<SimpleMemberAssignment> _params_7 = _path_4.getParams();
                for (final SimpleMemberAssignment slug_1 : _params_7) {
                    _builder.append("\t\t");
                    _builder.append("this.");
                    SimpleMember _member_20 = slug_1.getMember();
                    String _name_20 = _member_20.getName();
                    String _camelize_11 = Strings.camelize(_name_20);
                    _builder.append(_camelize_11, "\t\t");
                    _builder.append("Segment = ");
                    SimpleMember _member_21 = slug_1.getMember();
                    String _name_21 = _member_21.getName();
                    String _camelize_12 = Strings.camelize(_name_21);
                    _builder.append(_camelize_12, "\t\t");
                    _builder.append("Segment;");
                    _builder.newLineIfNotEmpty();
                }
            }
        }
    }
    {
        boolean _hasBody_2 = ModelExtensions.hasBody(method);
        if (_hasBody_2) {
            _builder.append("\t\t");
            BodyBlock _body_3 = ModelExtensions.getBody(method);
            BlockType _type_7 = _body_3.getType();
            CharSequence _generateConstructorAssignmentForType = this.generateConstructorAssignmentForType(_type_7);
            _builder.append(_generateConstructorAssignmentForType, "\t\t");
            _builder.newLineIfNotEmpty();
        }
    }
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    {
        boolean _hasBody_3 = ModelExtensions.hasBody(method);
        if (_hasBody_3) {
            _builder.append("\t");
            this.imports.addImport("com.robotoworks.mechanoid.util.Closeables");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            this.imports.addImport("java.io.OutputStream");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            this.imports.addImport("java.io.IOException");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public void writeBody(JsonEntityWriterProvider provider, OutputStream stream) throws IOException {");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("\t");
            BodyBlock _body_4 = ModelExtensions.getBody(method);
            BodyBlock _body_5 = ModelExtensions.getBody(method);
            BlockType _type_8 = _body_5.getType();
            CharSequence _generateSerializationStatementForType = this.generateSerializationStatementForType(method, _body_4, _type_8);
            _builder.append(_generateSerializationStatementForType, "\t\t");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
        }
    }
    _builder.append("\t");
    _builder.append("@Override");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public String createUrl(String baseUrl){");
    _builder.newLine();
    {
        Path _path_5 = method.getPath();
        EList<SimpleMemberAssignment> _params_8 = null;
        if (_path_5 != null) {
            _params_8 = _path_5.getParams();
        }
        int _size_2 = 0;
        if (_params_8 != null) {
            _size_2 = _params_8.size();
        }
        boolean _greaterThan_2 = (_size_2 > 0);
        if (_greaterThan_2) {
            _builder.append("\t\t");
            _builder.append("Uri.Builder uriBuilder = Uri.parse(baseUrl + String.format(PATH, ");
            {
                Path _path_6 = method.getPath();
                EList<SimpleMemberAssignment> _params_9 = _path_6.getParams();
                boolean _hasElements = false;
                for (final SimpleMemberAssignment slug_2 : _params_9) {
                    if (!_hasElements) {
                        _hasElements = true;
                    } else {
                        _builder.appendImmediate(", ", "\t\t");
                    }
                    SimpleMember _member_22 = slug_2.getMember();
                    String _name_22 = _member_22.getName();
                    String _camelize_13 = Strings.camelize(_name_22);
                    _builder.append(_camelize_13, "\t\t");
                    _builder.append("Segment");
                }
            }
            _builder.append(")).buildUpon();");
            _builder.newLineIfNotEmpty();
        } else {
            _builder.append("\t\t");
            _builder.append("Uri.Builder uriBuilder = Uri.parse(baseUrl + PATH).buildUpon();");
            _builder.newLine();
        }
    }
    _builder.append("\t\t\t");
    _builder.newLine();
    {
        boolean _notEquals_9 = (!Objects.equal(methodParams, null));
        if (_notEquals_9) {
            {
                EList<SimpleMemberAssignment> _params_10 = methodParams.getParams();
                for (final SimpleMemberAssignment param_4 : _params_10) {
                    _builder.append("\t\t");
                    _builder.append("if(");
                    SimpleMember _member_23 = param_4.getMember();
                    String _name_23 = _member_23.getName();
                    String _camelize_14 = Strings.camelize(_name_23);
                    _builder.append(_camelize_14, "\t\t");
                    _builder.append("ParamSet){");
                    _builder.newLineIfNotEmpty();
                    {
                        SimpleMember _member_24 = param_4.getMember();
                        IntrinsicType _type_9 = _member_24.getType();
                        if ((_type_9 instanceof StringType)) {
                            _builder.append("\t\t");
                            _builder.append("\t");
                            _builder.append("uriBuilder.appendQueryParameter(\"");
                            SimpleMember _member_25 = param_4.getMember();
                            String _name_24 = _member_25.getName();
                            _builder.append(_name_24, "\t\t\t");
                            _builder.append("\", ");
                            SimpleMember _member_26 = param_4.getMember();
                            String _name_25 = _member_26.getName();
                            String _camelize_15 = Strings.camelize(_name_25);
                            _builder.append(_camelize_15, "\t\t\t");
                            _builder.append("Param);");
                            _builder.newLineIfNotEmpty();
                        } else {
                            _builder.append("\t\t");
                            _builder.append("\t");
                            _builder.append("uriBuilder.appendQueryParameter(\"");
                            SimpleMember _member_27 = param_4.getMember();
                            String _name_26 = _member_27.getName();
                            _builder.append(_name_26, "\t\t\t");
                            _builder.append("\", String.valueOf(");
                            SimpleMember _member_28 = param_4.getMember();
                            String _name_27 = _member_28.getName();
                            String _camelize_16 = Strings.camelize(_name_27);
                            _builder.append(_camelize_16, "\t\t\t");
                            _builder.append("Param));");
                            _builder.newLineIfNotEmpty();
                        }
                    }
                    _builder.append("\t\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
            _builder.append("\t\t");
            _builder.newLine();
        }
    }
    {
        boolean _notEquals_10 = (!Objects.equal(clientParams, null));
        if (_notEquals_10) {
            {
                EList<SimpleMemberAssignment> _params_11 = clientParams.getParams();
                for (final SimpleMemberAssignment param_5 : _params_11) {
                    _builder.append("\t\t");
                    _builder.append("if(");
                    SimpleMember _member_29 = param_5.getMember();
                    String _name_28 = _member_29.getName();
                    String _camelize_17 = Strings.camelize(_name_28);
                    _builder.append(_camelize_17, "\t\t");
                    _builder.append("ParamSet){");
                    _builder.newLineIfNotEmpty();
                    {
                        SimpleMember _member_30 = param_5.getMember();
                        IntrinsicType _type_10 = _member_30.getType();
                        if ((_type_10 instanceof StringType)) {
                            _builder.append("\t\t");
                            _builder.append("\t");
                            _builder.append("uriBuilder.appendQueryParameter(\"");
                            SimpleMember _member_31 = param_5.getMember();
                            String _name_29 = _member_31.getName();
                            _builder.append(_name_29, "\t\t\t");
                            _builder.append("\", ");
                            SimpleMember _member_32 = param_5.getMember();
                            String _name_30 = _member_32.getName();
                            String _camelize_18 = Strings.camelize(_name_30);
                            _builder.append(_camelize_18, "\t\t\t");
                            _builder.append("Param);");
                            _builder.newLineIfNotEmpty();
                        } else {
                            _builder.append("\t\t");
                            _builder.append("\t");
                            _builder.append("uriBuilder.appendQueryParameter(\"");
                            SimpleMember _member_33 = param_5.getMember();
                            String _name_31 = _member_33.getName();
                            _builder.append(_name_31, "\t\t\t");
                            _builder.append("\", String.valueOf(");
                            SimpleMember _member_34 = param_5.getMember();
                            String _name_32 = _member_34.getName();
                            String _camelize_19 = Strings.camelize(_name_32);
                            _builder.append(_camelize_19, "\t\t\t");
                            _builder.append("Param));");
                            _builder.newLineIfNotEmpty();
                        }
                    }
                    _builder.append("\t\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
            _builder.append("\t\t");
            _builder.newLine();
        }
    }
    _builder.append("\t\t");
    _builder.append("return uriBuilder.toString();\t\t\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
}
Also used : Path(com.robotoworks.mechanoid.net.netModel.Path) HeaderBlock(com.robotoworks.mechanoid.net.netModel.HeaderBlock) StringType(com.robotoworks.mechanoid.net.netModel.StringType) BodyBlock(com.robotoworks.mechanoid.net.netModel.BodyBlock) IntrinsicType(com.robotoworks.mechanoid.net.netModel.IntrinsicType) EList(org.eclipse.emf.common.util.EList) Header(com.robotoworks.mechanoid.net.netModel.Header) BlockType(com.robotoworks.mechanoid.net.netModel.BlockType) SimpleMemberAssignment(com.robotoworks.mechanoid.net.netModel.SimpleMemberAssignment) ParamsBlock(com.robotoworks.mechanoid.net.netModel.ParamsBlock) ComplexTypeLiteral(com.robotoworks.mechanoid.net.netModel.ComplexTypeLiteral) Literal(com.robotoworks.mechanoid.net.netModel.Literal) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) SimpleMember(com.robotoworks.mechanoid.net.netModel.SimpleMember)

Example 2 with ParamsBlock

use of com.robotoworks.mechanoid.net.netModel.ParamsBlock in project mechanoid by robotoworks.

the class ClientGenerator method generateClientClass.

public CharSequence generateClientClass(final Client client, final Model module) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("public abstract class Abstract");
    String _name = client.getName();
    _builder.append(_name, "");
    _builder.append(" extends ServiceClient {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("private static final String LOG_TAG = \"");
    String _name_1 = client.getName();
    _builder.append(_name_1, "\t");
    _builder.append("\";");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    {
        String _baseUrl = client.getBaseUrl();
        boolean _notEquals = (!Objects.equal(_baseUrl, null));
        if (_notEquals) {
            _builder.append("\t");
            _builder.append("protected static final String DEFAULT_BASE_URL = \"");
            String _baseUrl_1 = client.getBaseUrl();
            _builder.append(_baseUrl_1, "\t");
            _builder.append("\";");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.newLine();
        }
    }
    _builder.append("\t");
    ParamsBlock params = ModelExtensions.getParamsBlock(client);
    _builder.newLineIfNotEmpty();
    {
        boolean _notEquals_1 = (!Objects.equal(params, null));
        if (_notEquals_1) {
            {
                EList<SimpleMemberAssignment> _params = params.getParams();
                for (final SimpleMemberAssignment param : _params) {
                    _builder.append("\t");
                    _builder.append("private ");
                    SimpleMember _member = param.getMember();
                    IntrinsicType _type = _member.getType();
                    String _signature = ModelExtensions.signature(_type);
                    _builder.append(_signature, "\t");
                    _builder.append(" ");
                    SimpleMember _member_1 = param.getMember();
                    String _name_2 = _member_1.getName();
                    String _camelize = Strings.camelize(_name_2);
                    _builder.append(_camelize, "\t");
                    _builder.append("Param");
                    {
                        Literal _defaultValue = param.getDefaultValue();
                        boolean _notEquals_2 = (!Objects.equal(_defaultValue, null));
                        if (_notEquals_2) {
                            _builder.append(" = ");
                            Literal _defaultValue_1 = param.getDefaultValue();
                            String _convertToJavaLiteral = ModelExtensions.convertToJavaLiteral(_defaultValue_1);
                            _builder.append(_convertToJavaLiteral, "\t");
                        }
                    }
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("private boolean ");
                    SimpleMember _member_2 = param.getMember();
                    String _name_3 = _member_2.getName();
                    String _camelize_1 = Strings.camelize(_name_3);
                    _builder.append(_camelize_1, "\t");
                    _builder.append("ParamSet");
                    {
                        Literal _defaultValue_2 = param.getDefaultValue();
                        boolean _notEquals_3 = (!Objects.equal(_defaultValue_2, null));
                        if (_notEquals_3) {
                            _builder.append(" = true");
                        }
                    }
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.newLine();
        }
    }
    {
        boolean _notEquals_4 = (!Objects.equal(params, null));
        if (_notEquals_4) {
            {
                EList<SimpleMemberAssignment> _params_1 = params.getParams();
                for (final SimpleMemberAssignment param_1 : _params_1) {
                    _builder.append("\t");
                    _builder.append("public void set");
                    SimpleMember _member_3 = param_1.getMember();
                    String _name_4 = _member_3.getName();
                    String _pascalize = Strings.pascalize(_name_4);
                    _builder.append(_pascalize, "\t");
                    _builder.append("Param(");
                    SimpleMember _member_4 = param_1.getMember();
                    IntrinsicType _type_1 = _member_4.getType();
                    String _signature_1 = ModelExtensions.signature(_type_1);
                    _builder.append(_signature_1, "\t");
                    _builder.append(" value) {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("this.");
                    SimpleMember _member_5 = param_1.getMember();
                    String _name_5 = _member_5.getName();
                    String _camelize_2 = Strings.camelize(_name_5);
                    _builder.append(_camelize_2, "\t\t");
                    _builder.append("Param = value;");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("this.");
                    SimpleMember _member_6 = param_1.getMember();
                    String _name_6 = _member_6.getName();
                    String _camelize_3 = Strings.camelize(_name_6);
                    _builder.append(_camelize_3, "\t\t");
                    _builder.append("ParamSet = true;");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
            _builder.append("\t");
            _builder.newLine();
        }
    }
    _builder.append("\t");
    _builder.append("@Override");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("protected String getLogTag() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return LOG_TAG;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@Override");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("protected JsonEntityWriterProvider createWriterProvider() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return new Default");
    String _name_7 = client.getName();
    _builder.append(_name_7, "\t\t");
    _builder.append("WriterProvider();");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@Override");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("protected JsonEntityReaderProvider createReaderProvider() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return new Default");
    String _name_8 = client.getName();
    _builder.append(_name_8, "\t\t");
    _builder.append("ReaderProvider();");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public Abstract");
    String _name_9 = client.getName();
    _builder.append(_name_9, "\t");
    _builder.append("(String baseUrl, boolean debug){");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("super(baseUrl, debug);");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    HeaderBlock headers = ModelExtensions.getHeaderBlock(client);
    _builder.newLineIfNotEmpty();
    {
        boolean _notEquals_5 = (!Objects.equal(headers, null));
        if (_notEquals_5) {
            {
                EList<Header> _headers = headers.getHeaders();
                for (final Header header : _headers) {
                    _builder.append("\t\t");
                    _builder.append("setHeader(\"");
                    String _name_10 = header.getName();
                    _builder.append(_name_10, "\t\t");
                    _builder.append("\",\"");
                    String _value = header.getValue();
                    _builder.append(_value, "\t\t");
                    _builder.append("\");");
                    _builder.newLineIfNotEmpty();
                }
            }
        }
    }
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    CharSequence _generateClientMethods = this.generateClientMethods(client, module);
    _builder.append(_generateClientMethods, "\t");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    return _builder;
}
Also used : IntrinsicType(com.robotoworks.mechanoid.net.netModel.IntrinsicType) HeaderBlock(com.robotoworks.mechanoid.net.netModel.HeaderBlock) Header(com.robotoworks.mechanoid.net.netModel.Header) ParamsBlock(com.robotoworks.mechanoid.net.netModel.ParamsBlock) SimpleMemberAssignment(com.robotoworks.mechanoid.net.netModel.SimpleMemberAssignment) Literal(com.robotoworks.mechanoid.net.netModel.Literal) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) SimpleMember(com.robotoworks.mechanoid.net.netModel.SimpleMember)

Example 3 with ParamsBlock

use of com.robotoworks.mechanoid.net.netModel.ParamsBlock in project mechanoid by robotoworks.

the class ClientGenerator method generateClientMethods.

public CharSequence generateClientMethods(final Client client, final Model model) {
    StringConcatenation _builder = new StringConcatenation();
    {
        EList<ClientBlock> _blocks = client.getBlocks();
        Iterable<HttpMethod> _filter = Iterables.<HttpMethod>filter(_blocks, HttpMethod.class);
        for (final HttpMethod method : _filter) {
            {
                boolean _and = false;
                boolean _hasBody = ModelExtensions.hasBody(method);
                boolean _not = (!_hasBody);
                if (!_not) {
                    _and = false;
                } else {
                    boolean _or = false;
                    Path _path = method.getPath();
                    boolean _equals = Objects.equal(_path, null);
                    if (_equals) {
                        _or = true;
                    } else {
                        Path _path_1 = method.getPath();
                        EList<SimpleMemberAssignment> _params = _path_1.getParams();
                        int _size = _params.size();
                        boolean _equals_1 = (_size == 0);
                        _or = _equals_1;
                    }
                    _and = _or;
                }
                if (_and) {
                    _builder.append("public Response<");
                    String _name = method.getName();
                    String _pascalize = Strings.pascalize(_name);
                    _builder.append(_pascalize, "");
                    _builder.append("Result> ");
                    String _name_1 = method.getName();
                    String _camelize = Strings.camelize(_name_1);
                    _builder.append(_camelize, "");
                    _builder.append("()");
                    _builder.newLineIfNotEmpty();
                    _builder.append("  ");
                    _builder.append("throws ServiceException {");
                    _builder.newLine();
                    _builder.append("  \t");
                    _builder.append("return ");
                    String _name_2 = method.getName();
                    String _camelize_1 = Strings.camelize(_name_2);
                    _builder.append(_camelize_1, "  \t");
                    _builder.append("(new ");
                    String _name_3 = method.getName();
                    String _pascalize_1 = Strings.pascalize(_name_3);
                    _builder.append(_pascalize_1, "  \t");
                    _builder.append("Request());");
                    _builder.newLineIfNotEmpty();
                    _builder.append("}");
                    _builder.newLine();
                    _builder.newLine();
                }
            }
            _builder.append("public Response<");
            String _name_4 = method.getName();
            String _pascalize_2 = Strings.pascalize(_name_4);
            _builder.append(_pascalize_2, "");
            _builder.append("Result> ");
            String _name_5 = method.getName();
            String _camelize_2 = Strings.camelize(_name_5);
            _builder.append(_camelize_2, "");
            _builder.append("(");
            String _name_6 = method.getName();
            String _pascalize_3 = Strings.pascalize(_name_6);
            _builder.append(_pascalize_3, "");
            _builder.append("Request request)");
            _builder.newLineIfNotEmpty();
            _builder.append("  ");
            _builder.append("throws ServiceException {");
            _builder.newLine();
            _builder.append("  \t");
            ParamsBlock params = ModelExtensions.getParamsBlock(client);
            _builder.newLineIfNotEmpty();
            {
                boolean _notEquals = (!Objects.equal(params, null));
                if (_notEquals) {
                    {
                        EList<SimpleMemberAssignment> _params_1 = params.getParams();
                        for (final SimpleMemberAssignment param : _params_1) {
                            _builder.append("\t");
                            _builder.append("if(this.");
                            SimpleMember _member = param.getMember();
                            String _name_7 = _member.getName();
                            String _camelize_3 = Strings.camelize(_name_7);
                            _builder.append(_camelize_3, "\t");
                            _builder.append("ParamSet && !request.is");
                            SimpleMember _member_1 = param.getMember();
                            String _name_8 = _member_1.getName();
                            String _pascalize_4 = Strings.pascalize(_name_8);
                            _builder.append(_pascalize_4, "\t");
                            _builder.append("ParamSet()){");
                            _builder.newLineIfNotEmpty();
                            _builder.append("\t");
                            _builder.append("\t");
                            _builder.append("request.set");
                            SimpleMember _member_2 = param.getMember();
                            String _name_9 = _member_2.getName();
                            String _pascalize_5 = Strings.pascalize(_name_9);
                            _builder.append(_pascalize_5, "\t\t");
                            _builder.append("Param(this.");
                            SimpleMember _member_3 = param.getMember();
                            String _name_10 = _member_3.getName();
                            String _camelize_4 = Strings.camelize(_name_10);
                            _builder.append(_camelize_4, "\t\t");
                            _builder.append("Param);");
                            _builder.newLineIfNotEmpty();
                            _builder.append("\t");
                            _builder.append("}");
                            _builder.newLine();
                        }
                    }
                    _builder.append("\t");
                    _builder.newLine();
                }
            }
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("Parser<");
            String _name_11 = method.getName();
            String _pascalize_6 = Strings.pascalize(_name_11);
            _builder.append(_pascalize_6, "\t");
            _builder.append("Result> parser = new Parser<");
            String _name_12 = method.getName();
            String _pascalize_7 = Strings.pascalize(_name_12);
            _builder.append(_pascalize_7, "\t");
            _builder.append("Result>() {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("public ");
            String _name_13 = method.getName();
            String _pascalize_8 = Strings.pascalize(_name_13);
            _builder.append(_pascalize_8, "\t\t");
            _builder.append("Result parse(InputStream inStream) throws IOException {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t");
            _builder.append("return new ");
            String _name_14 = method.getName();
            String _pascalize_9 = Strings.pascalize(_name_14);
            _builder.append(_pascalize_9, "\t\t\t");
            _builder.append("Result(getReaderProvider(), inStream);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("};");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            CharSequence _generateServiceMethod = this.generateServiceMethod(method);
            _builder.append(_generateServiceMethod, "\t");
            _builder.newLineIfNotEmpty();
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
        }
    }
    return _builder;
}
Also used : Path(com.robotoworks.mechanoid.net.netModel.Path) EList(org.eclipse.emf.common.util.EList) SimpleMemberAssignment(com.robotoworks.mechanoid.net.netModel.SimpleMemberAssignment) ParamsBlock(com.robotoworks.mechanoid.net.netModel.ParamsBlock) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) SimpleMember(com.robotoworks.mechanoid.net.netModel.SimpleMember) HttpMethod(com.robotoworks.mechanoid.net.netModel.HttpMethod)

Aggregations

ParamsBlock (com.robotoworks.mechanoid.net.netModel.ParamsBlock)3 SimpleMember (com.robotoworks.mechanoid.net.netModel.SimpleMember)3 SimpleMemberAssignment (com.robotoworks.mechanoid.net.netModel.SimpleMemberAssignment)3 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 Header (com.robotoworks.mechanoid.net.netModel.Header)2 HeaderBlock (com.robotoworks.mechanoid.net.netModel.HeaderBlock)2 IntrinsicType (com.robotoworks.mechanoid.net.netModel.IntrinsicType)2 Literal (com.robotoworks.mechanoid.net.netModel.Literal)2 Path (com.robotoworks.mechanoid.net.netModel.Path)2 EList (org.eclipse.emf.common.util.EList)2 BlockType (com.robotoworks.mechanoid.net.netModel.BlockType)1 BodyBlock (com.robotoworks.mechanoid.net.netModel.BodyBlock)1 ComplexTypeLiteral (com.robotoworks.mechanoid.net.netModel.ComplexTypeLiteral)1 HttpMethod (com.robotoworks.mechanoid.net.netModel.HttpMethod)1 StringType (com.robotoworks.mechanoid.net.netModel.StringType)1