Anypoint Platform Development: Fundamentals 4.
2 - Snippets
* MODULE 1 **********************************************************
* ProgrammableWeb--mulesoft owned portal.
http://www.programmableweb.com/
* MuleSoft Training API portal:
https://anypoint.mulesoft.com/exchange/portals/muletraining/
* American Flights web service
http://training4-american-ws.cloudhub.io/api/flights
* American Flights API post body
{
"code": "GQ574",
"price": 399,
"departureDate": "2016/12/20",
"origin": "ORD",
"destination": "SFO",
"emptySeats": 200,
"plane": {"type": "Boeing 747", "totalSeats": 400}
}
* American Flights API
http://training4-american-api.cloudhub.io/flights
* American Flights API client_id for APDev fictitious application
d846d01ea59640108779eaaa532d00ca
* American Flights API client_secret for APDev fictitious application
F7C5ecA48EBA449399b4B140416849B0
* MODULE 2 **********************************************************
* American Flights API configuration values:
Host: training4-american-api.cloudhub.io
Port: 80
Base Path: /
Protocol: HTTP
* MODULE 3 **********************************************************
* American Flights API description text
The American Flights API is a system API for operations on the american table in
the training database.
Supported operations
Get all flights
Get a flight with a specific ID
Add a flight
* American Flights API - /{ID} DELETE and PUT methods
delete:
responses:
200:
body:
application/json:
example:
message: Flight deleted (but not really)
put:
body:
application/json:
type: AmericanFlight
examples:
input: !include examples/AmericanFlightNoIDExample.raml
responses:
200:
body:
application/json:
example:
message: Flight updated (but not really)
* MODULE 4 **********************************************************
* MySQL database
db:
host: "iltdb.learn.mulesoft.com"
port: "3306"
user: "mule"
password: "mule"
database: "training"
American table: american
Account table: accounts
Account list URL: http://ilt.learn.mulesoft.com/accounts/show
or if using mulesoft-training-services.jar application:
http://localhost:9090/accounts/show.html
* MySQL database as URL and driver name
URL: jdbc:mysql://iltdb.learn.mulesoft.com:3306/training?user=mule&password=mule
Driver class name: com.mysql.jdbc.Driver
* Derby database
URL: jdbc:derby://localhost:1527/memory:training
Driver class name: org.apache.derby.jdbc.ClientDriver
* SQL input parameter
{'ID' : attributes.uriParams.ID}
* American Flights API - /flights POST response example
{"message": "Flight added (but not really)"}
* MODULE 5 **********************************************************
Implementation URl ::http://training4-american-ws-ns2020.us-e2.cloudhub.io/api
Proxy Domain: training4-american-api-ns2020.us-e2.cloudhub.io
Consumer Endpoint: http://training4-american-api-ns2020.us-e2.cloudhub.io
Free Tier Credentails::
Client ID:
6697bf1cd47f46018b655b161b66f11f
Client Secret:
9028Fc3469874575b09517B91Aa266E1
Paid Client Credentails
Client ID:
3ea4b88c49de4f4eb377d22c7343cbc2
Client Secret:
1d571B96be514F05bbeD109F76E7Fd45
* MODULE 7 **********************************************************
* Salesforce query
SELECT Name, LastModifiedDate, BillingPostalCode
FROM Account
* MODULE 8 **********************************************************
* American RESTful web service properties
american:
host: "training4-american-api-{lastname}.{region}.cloudhub.io"
port: "80"
basepath: "/"
protocol: "HTTP"
client_id:
client_secret:
* United RESTful web service
http://ilt.learn.mulesoft.com/united/flights
or if using mulesoft-training-services.jar application:
http://localhost:9090/united/flights
* Training web service properties
training:
host: "ilt.learn.mulesoft.com"
port: "80"
basepath: "/"
protocol: "HTTP"
* Delta SOAP web service WSDL
http://ilt.learn.mulesoft.com/delta?wsdl
or if using mulesoft-training-services.jar application:
http://localhost:9191/delta?wsdl
* Delta web service properties
delta:
wsdl: "http://ilt.learn.mulesoft.com/delta?wsdl"
service: "TicketServiceService"
port: "TicketServicePort"
or if using mulesoft-training-services.jar application:
delta:
wsdl: "http://localhost:9191/delta?wsdl"
service: "DeltaFlightsService"
port: "DeltaFlightsServiceImplPort"
* DataWeave code to transform an object to a custom data type
as Object {
class : "com.mulesoft.training.Flight"
}
* MODULE 9 **********************************************************
* Validation expression
#[['SFO','LAX','CLE','PDX','PDF'] contains vars.code]
* Validation error message
'Invalid destination' ++ ' ' ++ (vars.code default ' ')
* MODULE 12 *********************************************************
* MUA Accounts URL
http://ilt.mulesoft-training.com/accounts/show
or if using mulesoft-training-services.jar application:
http://localhost:9090/accounts/show.html
* MODULE 13 *********************************************************
* DataWeave Transformation
payload map ( payload01 , indexOfPayload01 ) -> {
Name: payload01.name,
BillingStreet: payload01.street,
BillingCity: (payload01.city default ""),
BillingState: payload01.state,
BillingPostalCode: payload01.postal,
BillingCountry: payload01.country
}
Blog links for some topics::
https://blogs.mulesoft.com/biz/mule/thread-management-auto-tuning-mule-4/
https://dzone.com/articles/property-file-handling-in-mule4
http://check1.us-e2.cloudhub.io/
https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
https://dzone.com/articles/power-of-arrays-module-in-dataweave
code share::
https://codeshare.io/axQrmX
training4-american-api.cloudhub.io
from Lalit Panwar 3 to All Participants:
d846d01ea59640108779eaaa532d00ca
from Lalit Panwar 3 to All Participants:
F7C5ecA48EBA449399b4B140416849B0
lower(vars.airline) ~= 'american'
lower(vars.airline) ~= 'united'
lower(vars.airline) ~= 'delta'
why we use ~= beacsue
"1"==1;;false
"1"~=1;true(datatypes are ignored)
['SFO','LAX','PDX','CLE','PDF'] contains vars.code
"Invalid Destination " ++ vars.code
Data Transformation
%dw 2.0
output application/json
---
[
{
airline:payload[0].airline,
origin:payload[0].fromAirportCode
},
{
airline:payload[1].airline,
origin:payload[1].fromAirportCode
}
]
------------
//'Sindhu' ++ 'Nangineni'
++('Sindhu',' Nangineni')
-------------------
%dw 2.0
output application/json
---
/*
map(inputCollection,mapping)
inputCollection map mapping
//payload map{}
//payload map ()->{}
//payload map (obj,ind)->{}
map(payload,(obj,ind)->{})
*/
payload map{
'airlineName': $.airline,
'code': $.flightCode,
'origin': $.fromAirportCode,
'destination': $.toAirportCode,
'avilableSeats': $.emptySeats
}
--------------------------
%dw 2.0
output application/json
---
/*
map(inputCollection, mapping)
inputCollection map mapping
payload map {}
payload map ()->{}
payload map (obj, ind)->{}
map(payload, (obj, ind)->{})
payload map {
index: $$,
airlineName: $.airline,
code: $.flightCode,
origin: $.fromAirportCode,
destination: $.toAirportCode,
availableSeats: $.emptySeats
}
*/
payload map (flight, ind)->{
index: ind,
airlineName: flight.airline,
code: flight.flightCode,
origin: flight.fromAirportCode,
destination: flight.toAirportCode,
availableSeats: flight.emptySeats
}
--------------------------
%dw 2.0
output application/xml
---
flights:
{(
payload map (flight, ind)->{
flight:{
index: ind,
airlineName: flight.airline,
code: flight.flightCode,
origin: flight.fromAirportCode,
destination: flight.toAirportCode,
availableSeats: flight.emptySeats
}
)}
------------------------------------------------
%dw 2.0
output application/xml
---
flights:
flight: payload map (flight, ind)->{
index: ind,
airlineName: flight.airline,
code: flight.flightCode,
origin: flight.fromAirportCode,
destination: flight.toAirportCode,
availableSeats: flight.emptySeats
}
--------------------------------------------------
** JAVA does not support multiple variables with the same name
-------------------------------------------------------------
%dw 2.0
output application/json
---
payload..*return map (f, in)->{
airlineName: f.@airlineName,
code: f.code,
departureDate: f.departureDate,
destination: f.destination,
emptySeats: f.emptySeats,
origin: f.origin,
planeType: f.planeType,
price: f.price
}
-------------------------------------------------------------
%dw 2.0
output application/java
---
payload..*return map (f, in)->{
airlineName: f.@airlineName,
code: f.code,
departureDate: f.departureDate
as Date {format: "yyyy/MM/dd"}
as String {format: 'MMMM dd, yyyy'}
,
destination: f.destination,
emptySeats: f.emptySeats as Number,
origin: f.origin,
planeType: f.planeType,
price: f.price as Number //as String {format : '00000000,000'}
//{class: 'java.lang.Double'}
}
-----------------------------------------------------------------------------
%dw 2.0
output application/java
---
payload..*return map (f, in)->{
airlineName: f.@airlineName,
flightCode: f.code,
departureDate: f.departureDate
as Date {format: "yyyy/MM/dd"}
as String {format: 'MMMM dd, yyyy'}
,
destination: f.destination,
availableSeats: f.emptySeats as Number,
origination: f.origin,
planeType: f.planeType,
price: f.price as Number //as String {format : '00000000,000'}
} as Object {
class: 'com.mulesoft.training.Flight'
}
-----------------------------------------------------------------------------------
------
%dw 2.0
output application/json
type Currency = String{format: "-##.00"}
fun getTotalSeats(planeType: String)=
if(planeType contains '737')
180
else
300
---
payload..*return map (f, in)->{
airlineName: f.@airlineName,
flightCode: f.code,
departureDate: f.departureDate
as Date {format: "yyyy/MM/dd"}
as String {format: "MMMM dd, yyyy"}
,
destination: f.destination,
availableSeats: f.emptySeats as Number,
origination: f.origin,
planeType: f.planeType,
price: f.price as Number, //as Currency//as String {format : '00000000,000'}
totalSeats: getTotalSeats(f.planeType)
} //as Object { class: 'com.mulesoft.training.Flight' }
/*
var seats = 180
totalSeats: seats
var totalSeats = ()->180
totalSeats: totalSeats()
var totalSeats = (x=200)->x
totalSeats: totalSeats()
totalSeats: totalSeats(150)
var totalSeats = (planeType: String) ->
if(planeType contains '737')
180
else
300
totalSeats: totalSeats(f.planeType)
*/
-----------------------------------------------------
%dw 2.0
output application/json
type Currency = String{format: "-##.00"}
fun getTotalSeats(planeType: String)=
if(planeType contains '737')
180
else
300
---
using (flights = payload..*return map (f, in)->{
airlineName: f.@airlineName,
flightCode: f.code,
departureDate: f.departureDate as Date {format: "yyyy/MM/dd"}
as String {format: "MMMM dd, yyyy"}
,
destination: f.destination,
availableSeats: f.emptySeats as Number,
origination: f.origin,
planeType: f.planeType,
price: f.price as Number,
totalSeats: getTotalSeats(f.planeType),
//name: fname ++ lname
} )
flights orderBy -$.price
filter($.avilableSeats>0)
distinctBy $
groupBy $.airlineName
planeType: dw::core::Strings::dasherize(f.planeType),
planeType: dw::core::Strings::dasherize(f.planeType)
----DW completed--------
attributes.fileName replace '.csv' with ((java!
java::lang::System::currentTimeMillis() as String) ++ '.bin')