In principle, an API is a way to define endpoints of data on a server which a client may access over Hyper Text Transfer Protocols (HTTP) that we will use. Many of the most common HTTP methods we see today are GET, POST, PUT, and DELETE.
Generally, a client will start a request and response handshake with the server via the API. A process is started which may be a request or anything that shall trigger an external event for the API.
A server on which the API is running will handle that handshake event. The event may involve authentication, validation, retreival, deletion, updating, or manipulation of a data set.
- Endpoints are stand-alone user resource link (URL) to provide specific access to a resource or dataset.
- Methods indicates the operation called by the client to the API to perform an action.
- Parameters are a variable passed to the endpoint for instruction.
- Request header is a value representing details about request process, i.e. formatting of datasets or credentials.
- Body is the center piece for request and response handshake messaging. It contains the datasets of the resource or status codes.
- Response header is much like a request header, but for information about server response
It is important to note the concept of web applications today run mainly on HTTP. Moreover, HTTP protocols began in the early 1960's with the Xanadu project by Ted Nelson and his team. HTTP was further developed and standardized by a team at the European Center for Nuclear Research (CERN) by Tim Burners-Lee to promote his World Wide Web Project (WWW) in the early 1990s with his Hyper Text Markup Language (HTML) to make information available. You may read more about HTTP standards at their website for specifications and documentation.
Regardless, we saw further development of our ability to interact with clients and servers around the mid-1990s from Daniel Stenberg with his cURL library. Written in the C-langauge, it is the foundation with which most of us interact with APIs across the WWW and you will find his library on just about any computer. Let us look at a basic example in your terminal by running the following
curl -X GET https://alpha-strike.space/api/health
As long as the server is running, you should recieve a status code of 200 which respresents that everything is 'OKAY' so to speak.
You will see many status codes, whether interacting with an API or just loading a URL. The most important codes are the following
- Code: 100 means progress.
- Code: 200 means success.
- Code: 300 means redirection, sometimes cache (temporary storage) rules.
- Code: 400 means client side error.
- Code: 500 means server side error.
We shall list each endpoint and the corresponing command to retrieve information with our basis library cURL, subsquently using the R language package httr2 for a higher-level programmed example. It should be also noted that if you want to use the R language for a web application, you may take a look at using R Shiny. Keep in mind our base url is https://alpha-strike.space/api/ for your reference.
/health return the health of server
curl -X GET https://alpha-strike.space/api/health
library(httr2)
# API call in R.
url <- "https://alpha-strike.space/api/health"
# Start the request from the given URL.
url_request <- httr2::request(url) |>
httr2::req_error(is_error = ~ FALSE)
url_response <- httr2::req_perform(url_request) # Get the response.
# Check for error in HTTP response.
if (httr2::resp_status(url_response) == 200) {
json_package <- url_response |> httr2::resp_body_json(TRUE) # Spit JSON and store.
return(json_package) # Return the JSON variable.
} else {
response_code <- httr2::resp_status(url_response)
response_string <- httr2::resp_status_desc(url_response)
http_frame <- data.frame(code = response_code, description = response_string)
return(http_frame)
}
/incident return all incidents, or by victim/killer name and system.
curl -X GET https://alpha-strike.space/api/incident
library(httr2)
# API call in R.
url <- "https://alpha-strike.space/api/incident"
# Start the request from the given URL.
url_request <- httr2::request(url) |>
httr2::req_error(is_error = ~ FALSE)
url_response <- httr2::req_perform(url_request) # Get the response.
# Check for error in HTTP response.
if (httr2::resp_status(url_response) == 200) {
json_package <- url_response |> httr2::resp_body_json(TRUE) # Spit JSON and store.
return(json_package) # Return the JSON variable.
} else {
response_code <- httr2::resp_status(url_response)
response_string <- httr2::resp_status_desc(url_response)
http_frame <- data.frame(code = response_code, description = response_string)
return(http_frame)
}
curl -X GET https://alpha-strike.space/api/incident?name=STRING_HERE
library(httr2)
# API call in R.
url <- "https://alpha-strike.space/api/incident?name=STRING_HERE"
# Start the request from the given URL.
url_request <- httr2::request(url) |>
httr2::req_error(is_error = ~ FALSE)
url_response <- httr2::req_perform(url_request) # Get the response.
# Check for error in HTTP response.
if (httr2::resp_status(url_response) == 200) {
json_package <- url_response |> httr2::resp_body_json(TRUE) # Spit JSON and store.
return(json_package) # Return the JSON variable.
} else {
response_code <- httr2::resp_status(url_response)
response_string <- httr2::resp_status_desc(url_response)
http_frame <- data.frame(code = response_code, description = response_string)
return(http_frame)
}
curl -X GET https://alpha-strike.space/api/incident?system=ID_HERE
library(httr2)
# API call in R.
url <- "https://alpha-strike.space/api/incident?system=ID_HERE"
# Start the request from the given URL.
url_request <- httr2::request(url) |>
httr2::req_error(is_error = ~ FALSE)
url_response <- httr2::req_perform(url_request) # Get the response.
# Check for error in HTTP response.
if (httr2::resp_status(url_response) == 200) {
json_package <- url_response |> httr2::resp_body_json(TRUE) # Spit JSON and store.
return(json_package) # Return the JSON variable.
} else {
response_code <- httr2::resp_status(url_response)
response_string <- httr2::resp_status_desc(url_response)
http_frame <- data.frame(code = response_code, description = response_string)
return(http_frame)
}
/location return all systems, or by id and name.
curl -X GET https://alpha-strike.space/api/location
library(httr2)
# API call in R.
url <- "https://alpha-strike.space/api/location"
# Start the request from the given URL.
url_request <- httr2::request(url) |>
httr2::req_error(is_error = ~ FALSE)
url_response <- httr2::req_perform(url_request) # Get the response.
# Check for error in HTTP response.
if (httr2::resp_status(url_response) == 200) {
json_package <- url_response |> httr2::resp_body_json(TRUE) # Spit JSON and store.
return(json_package) # Return the JSON variable.
} else {
response_code <- httr2::resp_status(url_response)
response_string <- httr2::resp_status_desc(url_response)
http_frame <- data.frame(code = response_code, description = response_string)
return(http_frame)
}
curl -X GET https://alpha-strike.space/api/location?system=STRING_OR_NUMERICAL_ID_HERE
library(httr2)
# API call in R.
url <- "https://alpha-strike.space/api/location?system=STRING_OR_NUMERICAL_ID_HERE"
# Start the request from the given URL.
url_request <- httr2::request(url) |>
httr2::req_error(is_error = ~ FALSE)
url_response <- httr2::req_perform(url_request) # Get the response.
# Check for error in HTTP response.
if (httr2::resp_status(url_response) == 200) {
json_package <- url_response |> httr2::resp_body_json(TRUE) # Spit JSON and store.
return(json_package) # Return the JSON variable.
} else {
response_code <- httr2::resp_status(url_response)
response_string <- httr2::resp_status_desc(url_response)
http_frame <- data.frame(code = response_code, description = response_string)
return(http_frame)
}
An outbound websocket for the server for latest mails. Remember, the base url is wss://alpha-strike.space/ws/ for your reference. It must be noted that we can't use the previous package and use websocket for interaction with the server. You may need the additional dependency jsonlite for parsing information.
/mails return a message, and the latest incident on the frontier.
wscat -c wss://alpha-strike.space/ws/mails
library(websocket)
library(jsonlite)
# WebSocket URL in R
url <- "wss://alpha-strike.space/ws/mails"
# Create WebSocket connection
web_socket <- WebSocket$new(url)
# Message handling
web_socket$onMessage(function(event) {
received_message <- event$data # Access the received message
print(paste("Received:", received_message)) # Process the message
# Example: Parse JSON if needed
if (grepl("{", received_message)) {
parsed_data <- jsonlite::fromJSON(received_message)
print(parsed_data)
})
# Error handling
web_socket$onError(function(err) {
print(paste("Error:", err$message))
})
# Keep alive
Sys.sleep(10) # Adjust time
# Close connection
web_socket$close()
I shamelessly promote myself, Professor Killshot, for developing this website to host incident mails from the game Eve Frontier. The development of this website is unusual in the sense that most have been written in Python, PHP, or some sort of Java framework. I took a different route by utilizing C++ for handling a PostgreSQL database and HTML for the beautiful display in conjunction with NGINX and Cloudflare for handling requests. The purpose of this endeavour is to give myself confidence handling data across my two favorite languages, support a website in its display, and learn new things while giving those who play Eve Frontier a place to show off their criminally good pew pew skills. Good luck, have fun o7.