떼닝로그

Python for Data Science, AI & Development - APIs & Data Collection (1) 본문

Coursera/IBM Data Science

Python for Data Science, AI & Development - APIs & Data Collection (1)

떼닝 2024. 1. 30. 23:40

APIs & Data Collection

Simple APIs

Simple APIs (Part 1)

What is an API?

 

Using an API Library

import pandas as pd

dict_ = {'a':[11, 21, 31], 'b':[12, 22, 32]}
df = pd.DataFrame(dict_)

df.head()
df.mean()

 

REST APIs

- REpresentational State Transfer APIs

- REST APIs are used to interact with web services, i.e., Applications that you call through the internet

- They have a set of rules regarding: Communication, Input or Request, Output or Response

 

REST API terms

 

REST API over HTTP

 

PyCoinGecko for CoinGecko API

!pip install pycoingecko
from pycoingecko import CoinGeckoAPI
cg = CoinGeckoAPI()
bitcoin_Data = cg.get_coin_market_char_by_id(id='bitcoin', vs_currency='usd', days=30)

 

data = pd.DataFrame(bitcoin_price_data, columns=['TimeStamp', 'Price'], unit='ms')

fig = go.Figure(data=[go.Candlestick(x=candlestick_data.index,
					  open=candlestick_data['Price']['first'],
                      high=candlestick_data['Price']['max'],
                      low=candlestick_data['Price']['min'],
                      close=candlestick_data['Price']['last'])
                      ])
                      
fig.update_layout(xaxis_rangeslider_visible=Flase, xaxis_title='Date',
yaxis_title='Price (USD $)', title='Bitcoin Candlestick Chart Over Past 30 Days')

plot(fig, filename='bitcoin_candlestick_graph.html')

 

Simple APIs (Part 2)

API keys and endpoints

 

Reading : API

How do APIs work?

- Endpoint : specific uniform resource locator (URL) or uniform resource identifier (URI) representing a specific API function or resource. Each endpoint corresponds to a particular operation that the API can perform

- Request methods (HTTP methods) : APIs use HTTP methods to specify the type of action the client wants to perform on a given resource.

  Common HTTP methods include :

    - GET : retrieve data from the server

    - POST : send data to the server to create a new resource

    - PUT or PATCH : update an existing resource on the server

    - DELETE : remove a resource on the server

- Request headers : headers contain additional information about the request, such as the content type, authentication tokens, or. other metadata

- Request body : request body is common in POST or PUT requests, where the client sends data to create or update a resource

<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first
paragraph.</p>
</body>
</html>

 

- Response status code : the server responds to a client request with an HTTP status code, indicating the success or failure of the operation

- Authentication : many APIs require authentication to ensure that only authorized users or applications can access certain resources - some examples of using API keys include OAuth tokens or other authentication mechanisms

- Documentation : good API design includes comprehensive documenation that explains how to use the API, the available endpoints, request and response formats, and any authentication requirements

 

Structure of API URL

- Uniform Resource Identifier (URI) : URI is a string of characters identifying a name or a resourcce on the internet

- Uniform Resource Locator (URL) : specific type of URI that provides the means to access a resource on the web

- Scheme : the protocol is specified at the beginning of the URL, indicating that the communication should be sercured using the HTTPS protocol

- Route : location on the web server

 

API vs REST API

Feature API REST API
Full form Application Programming Interface REpresentational State Transfer API
Definition A set of protocols and tools for building software applications. It defines how different software component should interact. A specific type of web API that follows the principles of representational state transfer (REST). It is an architectural style for designing networked applications.
Scope A borader term encompassing various types of interface Specifically refers to APIs following the principles of REST architecture
Communication API communication methods can vary (Remote procedure call (RPC), Simple Objects Access Protocol (SOAP), and so on) Uses standard HTTP methods (GET, POST, PUT, DELETE) for communication
Architectural style No specific architectural style Follows the REST architectural style
Data format Can use different data formats (JSON, XML, and so on) Typically uses lightweight data formats, commonly JSON, for data exchange
URI Resource identification methods may vary Resources identified by URIs, each with multiple representations
Usage Used in various contexts (web development, libraries, operating systems, and so on) Primarily used for web services and web-based applications

 

Practice Quiz

Q. What does API stand for?

A. Application Programming Interface

 

Q. Which data format is commonly found in the HTTP message for API requests?

A. JSON

 

Q. What is the primary purpose of an API?

A. To connect and enaable communication between software applications.

Comments