Thursday, July 23, 2020

API and Web Services (Udemy)

[Mulesoft]

API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message or check the weather on your phone, you’re using an API. 

Example - Online travel service that interacts with several other websites to collect data...

  • API also provides a layer of security. Your phone's data is never fully exposed to the server, and likewise, the server is never fully exposed to your phone. Each communicates in small packets with only what is necessary - like ordering a takeaway. 

The Modern API

  • Modern APIs adhere to standards (typically HTTPS and REST).
  • They are treated more like products than code. 
  • They are designed for consumption by specific audiences ( for example mobile developers).
  • They are documented and versioned in a way that users can have certain expectations of its maintenance and lifecycle. 
  • They have stronger discipline for security and governance, are well monitored and managed for performance, scale and versioning.
  • Modern APIs have their own lifecycle -- Designing, Testing, Building, Managing, and Versioning. 

What makes APIs great?

  • Because users don't have to write it again. Just use it. You don't have to write a program to get the weather conditions. You simply can connect to existing weather APIs to get the details. 
  • Platform independent. 
  • Upgrade safe.

eBay API Example


How does API Work?


  • REQUEST / CALL (GET OR POST)
  • API Service / PROGRAM
  • RESPONSE

  • When you search for Tune on google.com. URL appears like Google.com/search?q=tune
  • When you search for gmail it directly goes to this page -- Google.com/gmail/about 
  • Google image search -- Tune -- google.com/search?tbm=isch&q=tune (isch is image search folder)

MASHUP

The online travel service image above shows the mashup (calling of several APIs by the search function of a travel website).

Web Service

Web = Internet
Service = API
Web Service = API that uses the Internet ( which means there are web services that don't use the Internet, reside within the same program and are "NOT OPEN" to the outside world).

So, ALL Web Services are APIs, but not all APIs are web services. 

Web services use:
  • XML or JSON to format data over the Internet. 
  • REST, SOAP or XML/RPC protocols

HTTP - HyperText Transfer Protocol

  • WWW.Google.com is a regular text; HTTP:// makes it a hypertext. 
  • HTTP enables google.com string to go to a Google computer via HTTP protocol. 

HTTP Request

  • Start Line (mandatory)
  • Headers
  • Blank line
  • Body

HTTP Response

  • Same as above

  • GET, POST, PUT, DELETE are similar to CRUD Database. 
  • GET == READ
  • POST == CREATE
  • PUT == UPDATE
  • DELETE == DELETE


START LINE


HTTP Status Codes

  • 1xx - Information
  • 2xx - Success
  • 3xx - Redirection
  • 4xx - Client errors
  • 5xx - Server errors

Idempotence


Idempotence means "safe to repeat". Can do as many times as you want and the results still stay the same. 


Blank Line

Partitions the Headers and Body.

HTTP  Body

HTTP Body is the content sent to/from the API. The headerline content-type describes the content in the HTTP body. 
  • Contains Content : 
    • Image
    • Video
    • HTML web page
    • Data
  • The Content-type header tells what type of content is sent and is received. 
  • Is in the format Type/subtype. Example Image/JPG, Image/JPEG, application/XML, text/CSS, Audio/mpeg.
  • Content types used to send / receive data
    • XML
    • JSON

XML

  • Created by W3C - 11 people, supported by 150 people.
  • Extensible markup language
  • HTTP Header line - Content-Type: application/XML
  • Holds data to be sent to the API
  • Holds data sent from the API
  • HTTP Body: XML
  • XML Tags simply describe the data within it - <Button> Dollar</Button> simply means the Button value type is dollar, unlike in HTML where the browser parses it as a Button type and creates a button. 
  • Extensible here means you can customize the tags to mean whatever you want to mean. 
  • HTML on the otherhand isn't extensible, nothing can be customized, the tags are predefined and mean what they are intended to mean. They are immutable. 
  • Example - Open notepad type this without the bullets and save as XML. 
    • <Pizza>
      • <Size>Large</Size>
      • <Toppings>Onions</Toppings>
      • <Toppings>Chicken Tikka</Toppings>
    • </Pizza>
  • XML Schema
    • Tells how XML has to be structured
    • Uses XSD --- XML Schema Definition (prev DTD, now DTD is outdated)
                    


JSON - JavaScript Object Notation

  • Part of JavaScript that holds information / data. 
  • Stands for JavaScript Object Notation. 
  • HTTP Header Line: Content-Type: application/json
  • Uses key-value pairs - example - ("Size" : "small")
  • Douglas crockford created JSON
{ "Pizza" : [

    {"Size" : "small",
    "toppings" : ["onions", "mushrooms"]
    }

                ]

}

JSON vs XML



  • XML is more powerful (transform one XML to another, query XML, provide security, etc.)
  • JSON is Simpler (good for repeated data transports).

SOAP

Both SOAP and REST Are ways to form HTTP Request and Response

  • Created by W3C
  • Simple Object Access Protocol
  • Way to access a Web Services by following some Rules.
  • HTTP Request and Response
  • Uses a WSDL (Web Services Description Language) to describe the web service
  • Uses XML content type in the WSDL
  • Has 4 HTTP Request Parts
    • Start Line
      • POST
      • WSDL 
      • HTTP Version
    • Header Line
      • Content-Type: text/XML
    • Blank Line
    • Body
      • XML Envelope formed using WSDL

SOAP Example

  • Holidaywebservice.com (no longer exists on the Internet)
  • Go there, and click the ServiceDescription to open the WSDL.



  • Open Postman
  • Endpoint is holidaywebservice.com/HolidayService_V2/HolidayService2.asmx?WSDL
  • Header - content-type is text/xml
  • Body is this

  • The Response will be the list of holidays in the US in XML format. 
  • To see the request code, click Code link on Postman to the top right, you will get the below.

Another Example

XMLSoccer.com
  • Register on the site
  • Go to my account
  • Check the free API key which you will use in the body
  • This site allows details on Scottish football league
  • Go to the service demo address - http://www.xmlsoccer.com/FootballDataDemo.asmx
  • We shall use the GetAllTeams to get all the teams' details.
  • The SOAP details are as below. We shall use them in the POSTMan
POST /FootballDataDemo.asmx HTTP/1.1
Host: www.xmlsoccer.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://xmlsoccer.com/GetAllTeams"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetAllTeams xmlns="http://xmlsoccer.com/">
      <ApiKey>string</ApiKey>
    </GetAllTeams>
  </soap:Body>
</soap:Envelope>
  • Uncheck the auto generated headers in POSTMan.
  • Update the above details. 
  • In the body mention the API key
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetAllTeams xmlns="http://xmlsoccer.com/">
      <ApiKey>JYHAKUCGGAVBCNWMNNCVBVIKZAOYIULCMSAEOHIACDSLOPHCCS</ApiKey>
    </GetAllTeams>
  </soap:Body>
</soap:Envelope>
The Response will be as below.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetAllTeamsResponse xmlns="http://xmlsoccer.com/">
            <GetAllTeamsResult>
                <XMLSOCCER.COM xmlns="">
                    <Team>
                        <Team_Id>45</Team_Id>
                        <Name>Aberdeen</Name>
                        <Country>Scotland</Country>
                        <Stadium>Pittodrie Stadium</Stadium>
                        <HomePageURL>http://www.afc.co.uk</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Aberdeen_F.C.</WIKILink>
                        <Capacity>20866</Capacity>
                        <Manager>Derek McInnes</Manager>
                    </Team>
                    <Team>
                        <Team_Id>46</Team_Id>
                        <Name>St Johnstone</Name>
                        <Country>Scotland</Country>
                        <Stadium>McDiarmid Park</Stadium>
                        <HomePageURL>http://www.perthstjohnstonefc.co.uk</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/St._Johnstone_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>47</Team_Id>
                        <Name>Motherwell</Name>
                        <Country>Scotland</Country>
                        <Stadium>Fir Park Stadium</Stadium>
                        <HomePageURL>http://www.motherwellfc.co.uk</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Motherwell_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>48</Team_Id>
                        <Name>Inverness C</Name>
                        <Country>Scotland</Country>
                        <Stadium>Caledonian Stadium</Stadium>
                        <HomePageURL>http://ictfc.com</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Inverness_Caledonian_Thistle_F.C.</WIKILink>
                        <Manager>John Robertson</Manager>
                    </Team>
                    <Team>
                        <Team_Id>49</Team_Id>
                        <Name>Rangers</Name>
                        <Country>Scotland</Country>
                        <Stadium>Ibrox Stadium</Stadium>
                        <HomePageURL>http://www.rangers.co.uk</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Rangers_F.C.</WIKILink>
                        <Capacity>50817</Capacity>
                        <Manager>Graeme Murty</Manager>
                    </Team>
                    <Team>
                        <Team_Id>50</Team_Id>
                        <Name>Hearts</Name>
                        <Country>Scotland</Country>
                        <Stadium>Tynecastle Stadium</Stadium>
                        <HomePageURL>http://www.heartsfc.co.uk/page/Home</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Heart_of_Midlothian_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>51</Team_Id>
                        <Name>Dundee United</Name>
                        <Country>Scotland</Country>
                        <Stadium>Tannadice Park</Stadium>
                        <HomePageURL>http://www.dundeeunitedfc.co.uk</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Dundee_United_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>52</Team_Id>
                        <Name>Kilmarnock</Name>
                        <Country>Scotland</Country>
                        <Stadium>Rugby Park</Stadium>
                        <HomePageURL>http://www.kilmarnockfc.co.uk/page/Home</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Kilmarnock_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>53</Team_Id>
                        <Name>Hibernian</Name>
                        <Country>Scotland</Country>
                        <Stadium>Easter Road</Stadium>
                        <HomePageURL>http://www.hibernianfc.co.uk/page/Home</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Hibernian_F.C.</WIKILink>
                        <Coach>Neil Lennon</Coach>
                        <Capacity>20421</Capacity>
                    </Team>
                    <Team>
                        <Team_Id>54</Team_Id>
                        <Name>Celtic</Name>
                        <Country>Scotland</Country>
                        <Stadium>Celtic Park</Stadium>
                        <HomePageURL>http://www.celticfc.net</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Celtic_F.C.</WIKILink>
                        <Capacity>60411</Capacity>
                        <Manager>Brendan Rodgers</Manager>
                    </Team>
                    <Team>
                        <Team_Id>55</Team_Id>
                        <Name>Dunfermline</Name>
                        <Country>Scotland</Country>
                        <Stadium>East End Park</Stadium>
                        <HomePageURL>http://www.dafc.co.uk/page/Home</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Dunfermline_Athletic_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>56</Team_Id>
                        <Name>St Mirren</Name>
                        <Country>Scotland</Country>
                        <Stadium>St. Mirren Park</Stadium>
                        <HomePageURL>http://www.saintmirren.net/pages/</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/St._Mirren_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>228</Team_Id>
                        <Name>Hamilton</Name>
                        <Country>Scotland</Country>
                        <Stadium>New Douglas Park</Stadium>
                        <HomePageURL>http://www.acciesfc.co.uk/</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Hamilton_Academical_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>254</Team_Id>
                        <Name>Falkirk</Name>
                        <Country>Scotland</Country>
                        <Stadium>Falkirk Stadium</Stadium>
                        <HomePageURL>http://www.falkirkfc.co.uk/</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Falkirk_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>291</Team_Id>
                        <Name>Gretna</Name>
                        <Country>Scotland</Country>
                        <Stadium />
                        <HomePageURL />
                        <WIKILink>http://en.wikipedia.org/wiki/Gretna_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>360</Team_Id>
                        <Name>Ross County</Name>
                        <Country>Scotland</Country>
                        <Stadium>Victoria Park</Stadium>
                        <HomePageURL>http://www.rosscountyfootballclub.co.uk/</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Ross_County_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>522</Team_Id>
                        <Name>Dundee FC</Name>
                        <Country>Scotland</Country>
                        <Stadium>Dens Park</Stadium>
                        <HomePageURL>http://www.dundeefc.co.uk/</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Dundee_F.C.</WIKILink>
                    </Team>
                    <Team>
                        <Team_Id>556</Team_Id>
                        <Name>Airdrie Utd</Name>
                        <Country>Scotland</Country>
                        <Stadium>New Broomfield</Stadium>
                        <HomePageURL>http://www.airdriefc.com/</HomePageURL>
                        <WIKILink>http://en.wikipedia.org/wiki/Airdrie_United_F.C.</WIKILink>
                    </Team>
                       <Team_Id>4959</Team_Id>
                        <Name>Rangers U20</Name>
                        <Country>Scotland</Country>
                        <Stadium />
                        <HomePageURL />
                        <WIKILink />
                    </Team>
                    <AccountInformation>.</AccountInformation>
                </XMLSOCCER.COM>
            </GetAllTeamsResult>
        </GetAllTeamsResponse>
    </soap:Body>
</soap:Envelope>

REST (Most Modern)


No comments:

Post a Comment

Full capabilities of ChatGPT 4 O (O for Omni) - From Openai.com

Omni, O, has multimodal capabitlies, which means it can take text, voice or video as an input and serve audio/text/image output (there's...