Thursday, October 22, 2020

1.3 API Data Formats

There are many different data formats used for your applications to communicate with a wide range of APIs available on the Internet. Each format represents syntax coding data that could be read by another machine but in such a way that is easy to understand for humans, too.

For instance, if you want to use an API to configure a Cisco router, you have to check which data types are supported by that API. Then, you can start writing a request to be handled by that API that has an effect on your router configuration. An API server comprehends your written code and translates it into instructions that are suitable for your router to process and create an action.



You will most likely encounter these common data formats:

  • YAML Ain't Markup Language (YAML)

  • JavaScript Object Notation (JSON)

  • eXtensible Markup Language (XML)

Common Use Cases

Take a look at some of the most common use cases of XML, JSON, and YAML. The first thing you will notice is that their files have the same extension as their name (.xml for XML, .json for JSON and .yaml for YAML).

Data Formats

Most common use

XML

Transformation with XSL

Applying XML schemas

JSON

Communication server—web page

Configuration files

YAML

Configuration files

XML has been recognized many times as hefty and not so humanly readable as the other two formats. XML is verbose, redundant, and complex to use. It is mostly used to interexchange highly structured data between applications (machine-to-machine communication). When you are talking about programming in Java, XML is widely used.

JSON is serving as an alternative to XML because it is often smaller and easier to read. It is mostly used for transmitting data between a server and a web page. The JSON syntax is the same as in the JavaScript programming language; therefore, you can very easily convert JSON data into JavaScript objects. JSON syntax is also useful for YAML, because JSON is basically a subset of YAML. Parsing JSON files with a YAML parser is therefore very intuitive.

If you are building your first API and you are a nondeveloper, YAML would be a way to go with when choosing which data format to use. YAML is made for people who are starting to write code from scratch. XML and JSON are mostly for your programming code to be more machine readable; that is why you export YAML into one of these two formats. YAML is used in many configuration files today. Because of its similar indentation styles, YAML files resonate with people that know Python.

Common Characteristics

Data formats are the foundation of API. They define the syntax and semantics, including constraints of working with the API.

A syntax is a way to represent a specific data format in textual form. You notice that some formats use curly braces or square brackets, and others have tags marking the beginning and end of an element. In some formats, quotation marks or commas are heavily used, while others do not require them at all. But no matter which syntax is used, each of these data formats has a concept of an object. You can think of an object as a packet of information, an element that has characteristics. An object can have one or more attributes attached to it.

Many characteristics will be broken down to the key-value concept, the key and value often being separated by a colon. The key identifies a set of data and it is often positioned on the left side of the colon. The values are the actual data that you are trying to represent. Usually, the data appears on the right side of the colon.

To extract the meaning of the syntax, it is crucial that you recognize how keys and values are notated when looking at the data format. A key must be a string, while a value could be a string, a number, or a Boolean (for instance, true or false). Other values could be more complicated, containing an array or an entirely new object that represents a lot of its own data.

Another thing to notice when looking at a particular data format is the importance of whitespaces and case sensitivity. Sometimes, whitespaces and case sensitivity could be of high importance, and in others, it could carry no significance whatsoever, as you will get to know through some future examples.

One of the main points about data formats that you should bear in mind is that you can represent any kind of data in any given format.



In the figure, there are the three previously mentioned common data formats—YAML, JSON, and XML. Each of these examples provides details about a specific user, providing their name, role, and location.

You can quickly recognize that the same data is represented in all three formats, so it really comes down to two factors when considering which format to choose:

  • If the system you are working with prefers one format over the other, pick the format that the language uses.

  • If the system can support any of them, pick the format that you are more comfortable working with.

In other words, if the API that you are addressing uses one specific format or a handful of them, you will have to choose one of them. If the API supports any given format, it is up to you which one you prefer to use.

YAML

The first data format you will learn about is YAML which, as the name suggests, is not a markup language like JSON and XML. With its minimalistic format, it was more heavily weighted to be humanly writable and readable but works the same way as other data formats. In general, YAML is the most humanly readable of all the formats and at the same time is just as easy for programs to use, which is why it is gaining increasing popularity among engineers working with programmability.

---
user:
  name: john
  location:
    city: Austin
    state: TX
  roles:
    - admin
    - user

Whitespaces are significant to YAML because whitespace indentation defines a structure of a YAML file. All the data inside a particular object is at the same indentation level. In this example, the first object that is indented is "name" which is a child node of "user" . All data at the same indentation level are attributes of that same object. The next level of indentation starts at the location property, denoting an object that represents a location, with properties city and state. Typically, YAML uses indentation of two whitespaces for every newly defined object, but you can also use your preferred indentation system.

Note

Tab indentations are not allowed in YAML because they are treated differently by different tools.

In YAML, keys and values are being separated only by a colon and space, which makes it very intuitive for humans to read or write. YAML will also try to assume which data type is intended as the value, so there are no quotes necessary. If it is a value such as john, YAML will assume that it is a string; you do not have to be explicit with quotes. The same concept applies to numbers and other types of values.

Note here that no commas are ending any of the values attached to the key. YAML automatically knows when there is an end of a value. Also, intuitively, "-" (dash) syntax in YAML denotes lists. They might appear so natural to you that it is like writing a shopping list for your groceries. Put a dash with the same indentation space in front of every element to add it to the same list.

JSON

Another heavily used data format is JSON. JSON was derived from the JavaScript programming language. Because of that historical background, JavaScript code can easily convert data from a JSON file into native JavaScript objects.

JSON syntax uses curly braces, square brackets, and quotes for its data representation. Typically, the very first character in a JSON file is a curly brace defining a new object structure. Below that, other objects can be defined in the same way, starting with a name of an object in quotes following a colon and a curly bracket. Underneath, you will find all information about that object.

Note

There are also some exceptions regarding the very first character in a JSON file. You could come across some small JSON files containing only values—for instance: Hello World!, 100 or true. All three options are regular JSON documents.

With YAML, the whitespaces are important, but that is not the case with JSON. All whitespaces that you see are just for humans consuming and reading the data; they have nothing to do with how the JSON file will be consumed by an application or script. Here, you are free to choose which kind of formatting style you want to use with JSON, as long as the other syntax rules remain satisfied.

Note

The explained information is true for all the whitespaces that are not part of a value itself. In this case, the values "john" and "j o h n" would not be considered the same because the whitespace is inside quotation marks. That way, whitespace carries importance.

{
    "user": {
        "name": "john", 
        "location": {
            "city": "Austin", 
            "state": "TX"
        },
        "roles": [
            "admin",
            "user"
        ]
    }
}

You will notice that all the data in a JSON file format is presented similarly as in YAML, using a key-value notation. Every object starts and ends with a curly bracket, and inside that main object in this figure is user. That object defines all information that you would like to configure for a user. You can see here that the john user is given a name and is assigned a location and a list of roles. You will also notice that all values attributed to this user are separated by a comma. Separating values by comma is obligatory for all objects except the last one; there is no comma at the end of a list of objects in JSON code.

XML

Another data serialization format that is broadly used for interchanging information over the Internet within two machines is XML. The beginnings of XML go back to the previous century, with the first version initially defined in 1998. It is very similar to HTML; they are both markup languages, meaning that they indicate which parts of a document are present and not how the data is going to be shown in your system specifically.

For that purpose, XML code heavily uses <tags></tags> to surround elements in form as <key>value</key>. All the information about an object is defined inside the opening <tag> and </tag> with a slash to indicate a closing tag. When using tags, you have to be careful that the beginning and ending tags match up both in the name itself and the same letter case. For instance: <tag></TAG> would not work properly, whereas <tag>.</tag> or <TAG></TAG> would work perfectly. Note that it is the same tag name that only differs in case. That said, each of the tags represents a completely different element.

Usually in XML, tag names are all written in lower case.

Whitespaces can be quite important in some formats, or they carry no significance in others.

XML is a combination of both. Significant whitespaces are a part of the document content and are always preserved. A good example is the whitespaces inside the value or opening and closing tag (<t1>John Wayne</t1> is not the same as <t1>JohnWayne</t1>). Whitespaces that are mostly meant to make XML documents more humanly readable are insignificant whitespaces and are used between different tags (<t1><t2> is considered the same as <t1> <t2>).

<?xml version="1.0" encoding="UTF-8" ?>
<user>
  <name>john</name>
  <location>
    <city>Austin</city>
    <state>TX</state>
  </location>
  <roles>admin</roles>
  <roles>user</roles>
</user>

An object usually contains multiple other objects inside it, as shown in the figure. The main object is <user> that ends with </user> tag at the end of an output. It is composed of many other tags such as <name><location>, and <roles> to provide all the information needed about that specific user. An object can either contain basic information (such as a name) or more complicated data with tags being nested inside that object (such as location in this case).

XML Namespaces

By having an increasing number of XML files exchanged through the Internet, you can quickly come into a situation where two or more applications use the same tag names but represent a completely different object (from that tag) at the same time. Here, you see a conflict with systems trying to parse some information from a specific tag that uses different hierarchy than the system expects to get. Solving that issue requires the use of namespaces and prefixes.



In this figure, you can see the same <table> element in two separate XML documents. Even though the starting tag name is the same, each element represents different information, which could cause a name conflict. The upper XML code carries HTML table information, whereas the lower one carries information about a table as a piece of furniture. You can easily avoid that by defining a prefix and a namespace for each of those two elements, as shown in the right side of the figure.

A prefix is an alphabetic character or a string put before the actual tag name followed by a colon (<a:tag_name> or <b:tag_name>). That way, you are defining an exact tag name for your application to parse correctly. When you are using prefixes in XML, you also have to define namespaces for those prefixes. The name of a namespace is a Uniform Resource Identifier (URI), which provides uniquely named elements and attributes in XML documents.

Namespaces are defined with the xmlns attribute in the starting tag of an element and a syntax as follows: xmlns:prefix='URI'. A URI can be any arbitrary string as long as they are different from any other URI. It can also be a URL linking to a specific page with a definition of that namespace. However, there is no need for the URL to be accessed. The only thing that matters is that the URI uniquely represents a logical namespace name.

Similar to XML, both YAML and JSON can also use namespaces that define the syntax and semantics of a name element, and in that way avoid element name conflicts. Take a look at the example codes from each format.



In this figure, you can find the same namespace encoded in each of the formats. In general, you will see that YAML and JSON usually do not use namespaces often, as is the case with XML. However, JSON has one exception, which is the Representational State Transfer Configuration Protocol (RESTCONF). When you are using RESTCONF, it requires a namespace. RESTCONF is a subset of Network Configuration Protocol (NETCONF), which is an IETF network management protocol designed specifically for transactional-based network management. It basically allows you to configure specific network devices such as routers, switches, and so on.

Note

The namespace myapp is not a valid namespace name because it has to be in the correct URI format. This example is just for the sake of a simplified illustration.

1.2 Rise of APIs in Software Design

 Software development is a broad field, and there are many different types of software that you encounter daily. Compare the firmware running on the car you drive with the mobile banking applications you use on your mobile phone—each serves a specific purpose. Similarly, the development process for each type of software has its own specifics. Mobile application development is nothing like embedded programming. Yet, you can still identify some similarities across many areas.

In the software development industry, you can observe these trends:

  • Web applications: Replacing traditional desktop (fat) clients, such as Microsoft Office 365

  • Proliferation of mobile applications: Often providing an alternative, seamless access to web applications

  • Integration with social media: To apply existing functionalities of social platforms

  • Cloud services: For data sharing and processing

  • Free software and libraries: To save cost in applications and services rather than implementing everything from scratch

APIs separate functionality into building blocks. Web applications, for example, rely on web servers to store data but use a web browser for presenting it. Single-page applications (SPAs) are modern web applications, which dynamically load data on demand into a single web page. This approach requires communication with the web server. Likewise, use of cloud services, software libraries, and social media require some form of communication, either to the server hosting the service or to the library code that you want to use. The details of communication are specified by the application programming interface (API).

With software trends, the use of APIs is also spreading. One of the reasons more and more developers are relying on APIs is the fact that they allow faster prototyping and development of software. APIs allow you to simply use an existing service or library to create a working prototype or the final program, without having to implement the functionality yourself.

APIs enable communication between computer systems or programs by specifying the details of how the information is exchanged, thereby facilitating code and functionality reuse.



This benefit is the reason so many developer-focused cloud services exist today. Not only is the functionality already implemented, they also take care of maintaining the service for you. The increasing number of applications are really integrations of existing cloud services in interesting new ways. Such cloud services were designed to be used as part of some other application. Applications can also reuse functionality of existing standalone systems if they provide an API. This capability is most useful for implementing custom applications and automating various tasks, such as notifying users via the Cisco Webex Teams messaging application or publishing information on a web dashboard. APIs are often used to streamline the development process as well. Examples include building with automated processes, bug tracking, testing, and more.

Using APIs

An increasing number of systems that expose functionality through APIs has enabled building more complex solutions and driving automation and integration unlike anything seen in the past. Therefore, consuming and building APIs has become a foundational skill for most professional software developers.

To use APIs effectively, keep in mind these considerations:

  • Modular software design

  • Prototyping and testing API integration

  • Challenges in consuming networked APIs

  • Distributed computing patterns

Prototyping and testing API actions allows you to quickly verify the feasibility of a chosen design, determine if the design meets stakeholder needs, and ensure that integration keeps working over time, especially across version upgrades. Many APIs rely on the network for communication, so it is also important that you understand the kinds of challenges an unreliable network presents and how to address them.

APIs also make more complex software architectures possible. However, to tackle the additional complexity, you must become familiar with modular software design, which will make the software you write maintainable and testable. This design familiarity holds true for either a single program code base or across distributed systems. Distributed systems in particular present many trade-offs, and adhering to best practices for their implementation will save you many headaches in the end.

One example of a distributed system that is of interest to developers and DevOps engineers is infrastructure automation workflow. It reduces management overhead by provisioning servers and networks in an automated way, either for a specific application or for shared infrastructure. It is commonly used as part of the application build process, such as in continuous integration and continuous deployment (CI/CD) pipelines.



The main purpose of APIs is to expose functionality, so documentation is at least as important as implementation.

Developers rely on API documentation to provide information such as:

  • Which functions or endpoints to call

  • Which data to provide as parameters or expect as output

  • How to encode protocol messages and data objects

The first two are inevitably bound to each individual API, and you will have to consult the API documentation for your particular use case. However, many networked APIs have settled on a common, standardized set of encodings and data formats.

Configure ip dns trace telnet to specific port using VPC GNS3/EVE

 This article will demonstrate on how to configure VPC on EVE and GNS3 to ping to another network device, telnet to one of the ports of the other devices, and trace route of remote device.


1-configure ip address on VPC, use ip (ip address)(Subnet Mask)(Default Gateway) command
VPC1>ip 192.168.1.10 255.255.255.0 192.168.1.1

2-configure dns on VPC, use ip dns (ip of dns) command
VPC1>ip dns 8.8.8.8

3-Configure hostname / Set hostname for the VPC, use set pcname (Name) command
VPC1>set pcname tariq2
tariq2>

4- show ip configuration
tariq2> show ip

NAME        : tariq2[1]
IP/MASK     : 192.168.1.10/24
GATEWAY     : 192.168.1.1
DNS         : 8.8.8.8
MAC         : 00:50:79:66:68:07
LPORT       : 20000
RHOST:PORT  : 127.0.0.1:30000

MTU         : 1500

5- To telnet to specific port of other devices, the following example show port 23 is opened on the ip address device 192.168.1.9
tariq2> rlogin 192.168.1.9 23

Connect 192.168.1.9:23, press Ctrl+X to quit
NOTES: you will be back to the starting point, NOT THE LAST,
       if using Ctrl+X to quit.
User Access Verification
Password:

6-The following example show port 80 is opened on the ip address device 192.168.1.9
tariq2> rlogin 192.168.1.9 80

Connect 192.168.1.9:80, press Ctrl+X to quit
NOTES: you will be back to the starting point, NOT THE LAST,
       if using Ctrl+X to quit.
HTTP/1.1 400 Bad Request
Date: Fri, 23 Aug 2019 11:25:06 GMT
Server: cisco-IOS
Accept-Ranges: none


7- The following example show port 25 is closed on the ip address device 192.168.1.9
tariq2> rlogin 192.168.1.9 25

Connect 192.168.1.9:25, press Ctrl+X to quit
NOTES: you will be back to the starting point, NOT THE LAST,
       if using Ctrl+X to quit.

Disconnected from 192.168.1.9:25

8- To trace to another ip, use trace (ip address) command
tariq2> trace 8.8.8.8
trace to 8.8.8.8, 8 hops max, press Ctrl+C to stop
 1   192.168.1.1   1.383 ms  1.164 ms  1.321 ms
 2   217.21.5.38   78.627 ms  99.847 ms  138.456 ms
 3   192.168.16.69   130.238 ms  127.061 ms  131.245 ms
 4   192.168.16.33   120.213 ms  141.827 ms  121.358 ms
 5   10.75.60.33   117.147 ms  *  64.133 ms
 6     *  *  *

9- To ping to another device, use ping (ip address) command
tariq> ping 192.168.1.9

84 bytes from 192.168.1.9 icmp_seq=1 ttl=255 time=0.421 ms
84 bytes from 192.168.1.9 icmp_seq=2 ttl=255 time=0.349 ms
84 bytes from 192.168.1.9 icmp_seq=3 ttl=255 time=0.425 ms
84 bytes from 192.168.1.9 icmp_seq=4 ttl=255 time=0.454 ms
84 bytes from 192.168.1.9 icmp_seq=5 ttl=255 time=0.489 ms

10- To save Configuration
tariq> save
Saving startup configuration to startup.vpc
.  done

To get automatic updates on your facebook, join a facebook group
https://www.facebook.com/groups/netsyshorizon/
To get automatic updates on your youtube guide, subscribe to the channel
http://www.youtube.com/channel/UCl25WPPKY4jnkGf32DnXU5
Related Links:
How to copy files from local machine to eve host windows or linux
EVE-NG Maximum nodes reached (128) Fix error / Add new hard drive to eve-ng
EVE Chnage ip address of eve-ng / configure eve ip address dns gateway
How to configure cisco router as DNS server
EVE How to add cisco asa to eve-ng - Cisco Qemu ASA to EVE
Adding Cisco Routers and Switches to EVE by adding IOU/IOL images to EVE
configure ip ping ip trace telnet port  using VPC EVE and VPC GNS3
CUCM UNITY IM and Presence Memory Usage / Utilization command
configure cisco router as ntp server and as ntp client
configure cisco router as dhcp server / cisco ios dhcp configuration
https://youtu.be/a-ajvu86ZGE
Cisco ios dhcp reservation  and Cisco dhcp lease release
https://youtu.be/hTduy8_YDTY
Cisco dhcp lease time hours / Cisco dhcp lease time infinite
https://youtu.be/tpYIe6KSlcI
CUCM IP Phone Registration and Configuration and make a call between two ip phones
https://youtu.be/bXvtDY2O7JM
Make a bootable CUCM image from a non bootable iso file / Cisco bootable iso
https://youtu.be/pfH1HzVYowE
NBMA Network Broadcast Network  Point-to-Point Network
https://youtu.be/yHIkJ-jOJHM
Difference Between Network and Internetwork / Network Types LAN MAN WAN
https://youtu.be/pGIkjYf9wC8
EIGRP Metric Calculation and Configuring different Delay and Bandwidth values
https://youtu.be/zgqaVkQe5Sw
Telnet Vs SSH - Difference betweenTelnet and SSH Configuration
https://youtu.be/15pfENww-dk
Configure DHCP on Cisco router - Cisco router as a DHCP Server
https://youtu.be/YTCgVn9X4ac
SSH from cisco router and switch to another
https://youtu.be/_I-0Vgc8E9s 
Assign ip address to cisco switch / Assign ip address to vlan
https://youtu.be/Q9SUVmyJr80
OSPF Cost Calculation and Configuring Bandwidth/ip ospf cost/reference bandwidth
https://youtu.be/zgqaVkQe5Sw
EIGRP Composite Metric Calculation and Configuring different delay values and Bandwidth values
https://youtu.be/GsnV4Bc3On8
Configure / Assign IP address to Cisco Layer 3 Switch
https://youtu.be/s9DMZCq27Ys
Configuring cisco extended acl / extended named access control list tutorial using packet tracer
https://youtu.be/Tj6H8pg06f0 
Configure Cisco  Extended ACL/ Extended Numbered Access Control List ACL Using Packet Tracer
https://youtu.be/j0CDNAa2Wqg
Port Forwarding and Static Nat on Cisco Routers - Access your private network from the internet
https://youtu.be/8WVWoW86Uhw
Configure Cisco Named Standard Access Control List ACL on Cisco routers
https://youtu.be/u2zmuRZazFw
How Access List works - Numbered Standard Access List configuring
https://youtu.be/rLci6mFg2BA
Cisco router WAN Redundancy/WAN Failover and Change Routing dynamicaly Using IP SLA - Route Tracking
https://youtu.be/x3xZhbrX7Ww
Cisco Named Access Control Lists Editing (add and delete individual lines+resequence)
https://youtu.be/RToV6h2rGeM
Connecting  GNS3 to Vmware

https://youtu.be/WuLf3ESAx94
How to download and install GNS3 1.2 + adding IOS image to GNS3

https://youtu.be/CXVNpERIPo4
Mikrotik router PPPoE client configuration - Mikrotik PPPoE client Setup
https://youtu.be/aUGL20rjbVA
Add IOS images to GNS3
https://youtu.be/GjxODWeuC5Y
Connect your PC to GNS3 - connecting GNS3 to local machine
https://youtu.be/c_b_-rGWnJc
Configuration register value - changing the configuration register
https://youtu.be/YVAKerOmJpk
Backup and Restore Cisco Configuration file -  Cisco backup Config
https://youtu.be/YMX1EMPjBC0
Capture telnet password using wireshark - Sniffing telnet password using wireshark
https://youtu.be/xShwyUq-uHk
Clock Rate vs Bandwidth - Configuring clock rate and bandwidth
https://youtu.be/rntsm5bHagI
Configure Default routes on cisco routers
https://youtu.be/E5-kpZt8LU4
Configure cisco router hostname and ip address
https://youtu.be/1OYMlMzQ1dE
Cisco Router Password Recovery
https://youtu.be/6shdtrAx9l0
Static Routing Configuration Using Packet Tracer
https://youtu.be/v05Jm4h-Zms I
NSTALL and FIX CISCO VPN CLIENT ON WINDOWS 10 / FIX REASON 442 FAILED TO ENABLE VIRTUAL ADAPTER

https://youtu.be/9jp2EmiOMwQ
Remote desktop connection error due to credssp encryption oracle remediation
https://youtu.be/LkccEQPUYME
INSTALL and FIX CISCO VPN CLIENT ON WINDOWS 10 / FIX REASON 442 FAILED TO ENABLE VIRTUAL ADAPTER
https://youtu.be/9jp2EmiOMwQ
Connect GNS3 to Vmware 2019 / Adding Virtual Machines to the GNS3 Topology 2019
https://youtu.be/NVom_wG8t3o
How to Add Hibernate Shortcut to the Windows 10 Start Menu
https://youtu.be/umxSphM_VnY
GNS3 Error could not bind with WinError 10049 The requested address is not valid in its context
https://youtu.be/wMmd2_RBLYQ
Assign ip address mikrotik router interface
https://youtu.be/bFTnUCXtGmI
how to block website mikrotik / mikrotik website blocking / mikrotik website filtering
https://youtu.be/dNswOyvLT4w
Mikrotik default password / Mikrotik default ip address / mikrotik default username
https://youtu.be/xMu52KIjsn0


keywords:
how to telnet to a port using vpc in eve
telnet a port vpc
eve vpc
telnet using vpcs
configure vpc gns3
configure vpc eve-ng
telnet vpcs gns3
telnet vpcs eve-ng
telnet from a vpc to router