API Examples
This page collects examples which demonstrate interactions with the API from various programming languages.
See https//curlconverter.com/ for more.
Get all motions
Python
import sys
import json
import requests
Payload parameters
# Framework parameters
# ----------------------------------
= ["brace2"] * 2
username, password = "http://localhost:8000" host
# Setup API request
# ----------------------------------
= {
headers "Content-Type": "Application/JSON",
}
= requests.get(
response + "/api/events/", headers=headers, auth=(username, password)
host
)# print(json.loads(response.text))
print(response.json())
Shell (curl
)
#!/bin/sh
hostname="localhost:8000"
username="brace2"
password="brace2"
event_id="$1"
curl -X GET \
-H 'Content-Type:Application/JSON' \
-u $username:$password \
$hostname/api/events/
printf "\n"
Get a single motion
C
#if 0
EXEC=${0%.*}
test -x "$EXEC" || clang "$0" -lcurl -o "$EXEC"
exec "$EXEC"
exit
{
#endif
#include <stdio.h>
#include <stdlib.h>
#include "curl/curl.h"
int
(void)
main{
int status = EXIT_SUCCESS;
const char
*username = "brace2",
*password = "brace2",
*hostname = "localhost:8000";
char *url = mkstr("https://%s:%s@%s/api/events/",
,password,hostname
username);
*curl;
CURL char buffer[CURL_ERROR_SIZE];
if ((curl = curl_easy_init()) != NULL) {
(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, buffer);
curl_easy_setoptif (curl_easy_perform(curl) != CURLE_OK) {
(stderr, "%s\n", buffer);
fprintfreturn EXIT_FAILURE;
}
(curl);
curl_easy_cleanup}
(url);
freereturn EXIT_SUCCESS;
}
#if 0
}
#endif
Go
// Claudio Perez
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
:= "localhost:8000"
hostname := "brace2"
username := "brace2"
password
:= &http.Client{}
client , err := http.NewRequest("GET", fmt.Sprintf("%s/api/events/",hostname), nil)
req
if err != nil {log.Fatal(err)}
.Header.Set("Content-Type", "Application/JSON")
req.SetBasicAuth(username, password)
req, err := client.Do(req)
respif err != nil {
.Fatal(err)
log}
defer resp.Body.Close()
, err := ioutil.ReadAll(resp.Body)
bodyTextif err != nil {
.Fatal(err)
log}
.Printf("%s\n", bodyText)
fmt}
Upload a new motion
In the following examples, a string variable named event_file
is used to store the name of the ground motion file to be uploaded.
Python
import sys
import json
import requests
# Payload parameters
# ----------------------------------
= sys.argv[1]
name = "/home/claudio/pkgs/quakeio/dat/58658_007_20210426_10.09.54.P.zip" event_file
# Framework parameters
# ----------------------------------
= ["brace2"] * 2
username, password = "http://localhost:8000" hostname
# Setup API request
# ----------------------------------
= {
headers "Content-Type": "multipart/form-data",
}
= {
files "event_file": (event_file, open(event_file, "rb")),
"name": (None, name),
}
= requests.post(
response + "/api/events/", headers=headers, files=files, auth=(username, password)
hostname
)print(json.loads(response))