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
# ----------------------------------
username, password = ["brace2"] * 2
host = "http://localhost:8000"
# Setup API request
# ----------------------------------
headers = {
    "Content-Type": "Application/JSON",
}
response = requests.get(
    host + "/api/events/", headers=headers, auth=(username, password)
)
# 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
main(void)
{
  int status = EXIT_SUCCESS;

  const char
  *username = "brace2",
  *password = "brace2",
  *hostname = "localhost:8000";

  char *url = mkstr("https://%s:%s@%s/api/events/",
    username,password,hostname
  );

  CURL *curl;
  char buffer[CURL_ERROR_SIZE];

  if ((curl = curl_easy_init()) != NULL) {
          curl_easy_setopt(curl, CURLOPT_URL, url);
          curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
          curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, buffer);
          if (curl_easy_perform(curl) != CURLE_OK) {
                  fprintf(stderr, "%s\n", buffer);
                  return EXIT_FAILURE;
          }
          curl_easy_cleanup(curl);
  }
  free(url);
  return EXIT_SUCCESS;
}
#if 0
}
#endif
Go
// Claudio Perez
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

func main() {
  hostname := "localhost:8000"
  username := "brace2"
  password := "brace2"

	client := &http.Client{}
	req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/events/",hostname), nil)

	if err != nil {log.Fatal(err)}

	req.Header.Set("Content-Type", "Application/JSON")
	req.SetBasicAuth(username, password)
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	bodyText, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", bodyText)
}

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
# ----------------------------------
name = sys.argv[1]
event_file = "/home/claudio/pkgs/quakeio/dat/58658_007_20210426_10.09.54.P.zip"
# Framework parameters
# ----------------------------------
username, password = ["brace2"] * 2
hostname = "http://localhost:8000"
# Setup API request
# ----------------------------------
headers = {
    "Content-Type": "multipart/form-data",
}
files = {
    "event_file": (event_file, open(event_file, "rb")),
    "name": (None, name),
}
response = requests.post(
    hostname + "/api/events/", headers=headers, files=files, auth=(username, password)
)
print(json.loads(response))
Shell (curl)
Back to top