Efficiency and Quality: Anypoint Code Builder for API Development
Written by Frederico Stilpen
Anypoint Code Builder is a web-based integrated development environment (IDE) that allows developers to design, implement and deploy apis from a single environment. It is a zero-install IDE, which means that it runs entirely on your browser.
Disclaimer:?This article was composed on June 26, 2023, during the beta phase of Anypoint Code Builder. It is important to note that certain functionalities may have been introduced subsequent to the writing of this article.
Anypoint Code Builder
Mulesoft continues to expand Anypoint Plataform capabilities. Now we have access to its next generation IDE, with a Visual Studio Code user experience.
Anypoint Code Builder has a Design platform to craft our API specification and an intuitive drag-and-drop interface that allows us to quickly connect different applications, systems, and data sources without having to write complex code. This innovative IDE allows us to accelerate the workflow and adhere to best practices in development, all running directly from the browser without any installation.
Anypoint Cold Builder is a powerful tool that can help developers streamline the integration process, reduce development time, and improve overall integration performance. In this article we will explore a little bit of the main features that Code Builder has to offer in three stages of Api development: Specification, Implementation and Deployment.
API Specification
API specification is a critical component of modern software development, serving as a blueprint for the design, development, and deployment of APIs.
In Design interface, very similar to Design Center, developers can easily create a new project specification and define the resources as the example image:
As we can see the nested resources and the associated methods also show up in API Console.
It is also important to mention that the code builder and the design center are synchronized, therefore when access the Design Center we can see the same project there.
Testing Specificaition
Within an api specification ready, we can use API Console to test the requests and responses.
In your api raml file, click on the API Console icon, on the top right of your page, select the resource you want to test and click on Try It.
At this point we can set the query parameters to your HTTP request and send it to receive a defined response. We can also add headers to simulate different scenarios, such as timeouts and errors.
Source Control
As the project grows and more modifications are made, the Source Control icon in the Activity Bar on the left shows an overview of how many changes you currently have, and it is also possible to review the modifications in each file by clicking on it.
It is important to understand that, although the project remains saved in Code Builder, we always need to commit the changes to Design Center and keep them synchronized. Otherwise, it will not be possible to publish the specification to the exchange.
Reuse Fragments
Same way as Design Center, it is also possible to reuse API fragments from exchange which can provide timesaving, consistency and reduced errors importing tested functionalities to the project.
You can use the Design Interface to create, test and publish to exchange a data type as a global fragment, for example, then import it in all your projects.
Code Builder provides a Command Palette, a new feature based on VsCode that allows us to access all Mulesoft specific commands and help on all stages. To access this tool, we need to press Ctrl + Shift + P to open the dialog box.
In this case we are using it to publish the data type to exchange making it discoverable. Search for Publish API specification to exchange, select the project name and press enter.
After completing the process, the fragment will be available in Exchange as we can see in the following image:
Now with the fragment available, we can use the command palette to import it to our project pressing ctrl + shit + P, then search for add dependency from exchange and type the fragment name. The dependency will be imported, and we now can see the fragment available in our project in exchange modules folder.
API Implementation
Code builder has a visual flow editor interface that allows for intuitive and timesaving api implementations. We can use it to implement a RAML or OAS API specification, by importing it as a dependency, and then create a Mule application with logic in each endpoint just like in Anypoint studio.
To import our example specification, we need to go to Anypoint Code Builder main tab and select Implement an API. Type the project name, search your project specification by name, select it and click in Create Project.
It is important to mention that the specification is mandatory for API implementations in Code Builder. At this point, it is not possible to implement an API without a specification in this beta version.
As we can see in the project main page, we have the explorer structure with the source main and test folders, and the xml’s files.
Here we can see the main flow. We can use drag-and-drop to select a component and it shows the xml code in the right-side tab. It is worth noting that, unlike Anypoint Studio, we currently do not have direct access to view connector properties, but only the xml code.
To view other flows, we can click on the Flow List button under the File tab which displays all the flows we have created, including the main flow.
From here we can craft our business logic, error and logging strategies adding components to our flow by clicking the plus button (+).
领英推荐
Intellisense
Anypoint code builder brings the Intellisense code editing feature within. IntelliSense is a code-completion aid that includes several features: List Members, Parameter Info, Quick Info, and Complete Word.
In a short way this feature gives us suggestions of whatever core processor or connectors we can add to our code. If we press Ctrl + Space, we will see a list of options to use in our code.
To add a Choice component, for example, we press C, and the tool gives us the xml code for it.
DataWeave
DataWeave is the Mulesoft programming language used to transform data that flows within a Mule application. Code Builder provides the ability to interact with DataWeave libraries, preview the transformation and to define your custom sample data to enable auto-completion of your context variables.
In this example, we are transforming an array of flights. To see the results, we can click at the bulb icon and select run preview, it will open a new tab with the preview output as the image below.
Dataweave Validations
Code Builder help developers by automatically highlighting dataweave syntactic or semantic errors. When an error is detected, Code Builder not only flags it but also provides helpful suggestions and recommendations to correct the error. To see the suggestions, we just need to click on Quick Fix.
This feature can reduce a lot of debugging time and helps to improve code quality.
Similar to Anypoint Studio, Code Builder allows us to create a DWL file containing a DataWeave script for utilization within our project.
The process of importing the .dwl file into our code is akin to the procedure followed in Anypoint Studio, as illustrated in the accompanying example image.
Run and Debug
In the left side bar, we have the embedded debugger in Anypoint Code Builder where we can see valuable information when we run or debug our code. In the Variables Panel we will be able to see information about the Mule event and variables, in the Watch Panel we can evaluate variables and expressions, the Call Stack Panel shows us the functions or procedures that are currently in the stack and finally the Breakpoints Panel which shows a summary of all breakpoints in your Mule application.
When the application is running, we can see the debug toolbar where we can navigate through the application.
Deploy Application
To deploy a mule application from Anypoint Code Builder, first we need to create a file named deploy.json in our src/main/resources directory. In this file we need to set the deployment configurations which are runtime version, worker size, worker number, application name, and set the autoStart property true. If the project does not have this file Code Builder prompts to create one.
Note that the application name property defines part of the URL that we use to access our application on Cloudhub. This name must be unique across all applications deployed.
From here, there are two ways to deploy a mule application to Cloudhub from Anypoint Code Builder.
The first and more intuitive one is clicking on the rocket icon on top of main tab, it shows the environments you have on runtime manager. Select the environment and it will deploy your application automatically.
The second one is using the command palette. Select Mulesoft: Deploy to Cloudhub and the environment option. After reviewing the deployment configuration, select Deploy and It will start packaging the application for deploy to selected Cloudhub environment.
After deployment completes, we can select Open in Runtime Manager to check the application on Cloudhub.
Restrictions & Limitations
Anypoint code builder still is an open beta release, so it has a few restrictions and limitations:
·??????It is limited to mule 4 applications and does not support Mule 3.
·??????Code builder provides basic debugging capabilities such as log viewing and error tracking. It may not offer the same level of functionality as dedicated IDEs or development environments.
·??????It does not support deployment to on-premises servers.
·??????It only supports deployment to Cloudhub 1.0.
·??????Access to the mocking service is only available through the Design Center platform.
·??????Despite the fact that Code Builder offers support for Munits, it is currently non-functional.
It is important to understand that, as we mentioned before, Anypoint code builder is an open beta, thus these restrictions and limitations may evolve over time as Mulesoft continues to enhance and expand Anypoint Plataform.
Further reading:
Scrum Master | Project Manager | PSM II | PMP? | MBA
1 年Good reading!
??