#!/bin/bash

# Coordinates!
APIHOST=dev-rest.equinoxoli.org
APIUSER=admin
APIPW=demo
APILOGINTYPE=api

VERBOSE=
SLEEP=

# test default login type (api), will return an error for patrons unless they are promoted and their api-type and main-type passwords match
BASICAUTH=`echo -n $APIUSER:$APIPW | base64`
curl -H "Authorization: Basic $BASICAUTH" -H 'Accept: text/plain' -s -o - "https://$APIHOST/openapi3/v1/self/auth"
echo

# test opac login type, will return an error for API Integrators unless the api-type and main-type passwords match and they have the OPAC_LOGIN permission
BASICAUTH=`echo -n $APIUSER:$APIPW:opac | base64`
curl -H "Authorization: Basic $BASICAUTH" -H 'Accept: text/plain' -s -o - "https://$APIHOST/openapi3/v1/self/auth"
echo

# Get a session token for this run; Check your APIPW and APILOGINTYPE if this fails
BASICAUTH=`echo -n $APIUSER:$APIPW:$APILOGINTYPE | base64`
SES=`curl -H "Authorization: Basic $BASICAUTH" -H 'Accept: text/plain' -s -o - "https://$APIHOST/openapi3/v1/self/auth"`

# helper function to test with varying Accept headers
#
# can take 1, 2, or 4 arguments:
#   path
#   path accept
#   path accept method data
#
call_curl () {
  [ ! -z $VERBOSE ] && echo -----------------------------
  [ ! -z $VERBOSE ] && echo Path: $1 $2 $3 $4
  if [[ "_" == "_$2" ]]; then # no accept header
    curl -H "Authorization: Bearer $SES" -s -o - "https://$APIHOST/openapi3/v1$1";
  elif [[ "_" == "_$3" ]]; then # just use GET, no data
    curl -H "Authorization: Bearer $SES" -H "Accept: $2" -s -o - "https://$APIHOST/openapi3/v1$1"
  elif [[ "_" == "_$4" ]]; then # not GET, but no data
    curl --request $3 -H "Authorization: Bearer $SES" -H "Accept: $2" -s -o - "https://$APIHOST/openapi3/v1$1"
  else # has data!
    curl --request $3 --data $4 -H "Authorization: Bearer $SES" -H "Accept: $2" -s -o - "https://$APIHOST/openapi3/v1$1"
  fi
  [ ! -z $VERBOSE ] && echo && echo -----------------------------
  [ ! -z $SLEEP ] && sleep 1 # we sleep after each API call to avoid rate limiting during testing
}    


# if we have a command line arg, run that and exit
if [[ "_" != "_$1" ]]; then
  call_curl "$1" "$2" "$3" "$4"
  exit
fi

VERBOSE=1
SLEEP=1

# let's test some stuff

## me, me trimmed
call_curl "/self/me"
call_curl "/self/me" "application/json;filterNulls=1"
call_curl '/self/transactions/all'
call_curl '/self/standing_penalties'
call_curl '/self/checkouts'
call_curl '/self/checkouts/history'
call_curl '/self/holds'

# patron, their circs, holds, and penalties
call_curl "/patron/209"
call_curl "/patron/209/messages"
call_curl "/patron/209/checkouts"
call_curl "/patron/209/checkouts/history"
call_curl "/patron/209/checkout/343"
call_curl "/patron/209/checkout/343" 'application/json' 'PUT'
call_curl "/patron/209/checkout/343" 'application/json' 'DELETE'
call_curl "/patron/209/holds"
call_curl "/patron/209/hold/181"
call_curl "/patron/6/standing_penalties"
call_curl "/patron/6/standing_penalty/1"
call_curl '/patron/6/transactions/all'
call_curl '/patron/6/transactions/have_balance'
call_curl '/patron/6/transaction/17'

# list some more patrons and info
call_curl '/patrons?field=usrname&comparison=like&value=bm1%'
call_curl '/patron/1/activity'
call_curl '/patron/1/collections/CONS' 'application/json' 'POST'
call_curl '/patron/1/collections/CONS'
call_curl '/patron/1/collections/CONS' 'application/json' 'DELETE'
call_curl '/patron/1/triggered_events'
call_curl '/patron/1/triggered_event/1'

# course info
call_curl "/courses"
call_curl "/courses/public_role_users"
call_curl "/course/1"
call_curl "/course/1/materials"
call_curl "/course/1/public_role_users"
call_curl "/holds/pickup_locations"

# bib three ways
call_curl "/bib/2"
call_curl "/bib/2" "application/xml"
call_curl "/bib/2" "application/octet-stream"

# display fields and holdings
call_curl "/bib/2/display_fields"
call_curl "/bib/2/holdings"

# orgs and stuff
call_curl "/org_units"
call_curl "/org_tree"
call_curl "/org_unit/2"
call_curl "/org_tree/2"

# items and whatnot
call_curl "/items/fresh?limit=10"
call_curl '/items' 'application/json' 'POST' '{"precat":true,"barcode":"mike1234","circ_lib":4,"location":1}'
call_curl '/item/mike1234' 'application/json' 'PATCH' '{"barcode":"mike1235","circ_lib":5}' # cannot update barcode -- it's skipped
call_curl '/item/mike1234'
call_curl '/item/mike1234' 'application/json' 'DELETE'
call_curl '/item/mike1234' # returns nothing
call_curl '/item/mike1234' 'application/json' 'DELETE' # fails with not-found

# dangling hold api?
call_curl '/hold/181'


# Not tested above, no concerto data yet:
#                        path                        | http_method |                                           method_params
#---------------------------------------------------+-------------+---------------------------------------------------------------------------------------------------
# /bib/:id                                          | patch       | eg_auth_token param.id req.json
# /bib/:id                                          | put         | eg_auth_token param.id req.text param.sourcename
# /bib/:id                                          | delete      | eg_auth_token param.id
# /bibs                                             | post        | eg_auth_token req.text param.sourcename
# /collections/:shortname/users_of_interest         | get         | eg_auth_token param.fine_age param.fine_amount param.shortname
# /collections/:shortname/users_of_interest/warning | get         | eg_auth_token param.shortname param.min_age param.max_age
# /patron/:userid/checkouts                         | post        | eg_auth_token param.userid req.json
# /patron/:userid/hold/:hold                        | patch       | eg_auth_token param.userid param.hold req.json
# /patron/:userid/hold/:hold                        | delete      | eg_auth_token param.userid param.hold "6"
# /patron/:userid/holds                             | post        | eg_auth_token param.userid req.json
# /patron/:userid/message/:msgid                    | get         | eg_auth_token param.userid "0" param.msgid
# /patron/:userid/message/:msgid                    | patch       | eg_auth_token eg_user_id param.userid param.msgid req.json
# /patron/:userid/message/:msgid                    | delete      | eg_auth_token eg_user_id param.userid param.msgid
# /patron/:usrid/collections/:shortname             | get         | eg_auth_token param.start param.end param.shortname every_param.usrid
# /patron/:usrid/collections/:shortname             | post        | eg_auth_token param.usrid param.shortname param.fee param.note
# /patron/:usrid/collections/:shortname             | delete      | eg_auth_token param.usrid param.shortname
# /self/checkout/:id                                | get         | eg_auth_token param.id
# /self/checkout/:id                                | put         | eg_auth_token param.id eg_user_id
# /self/checkout/:id                                | delete      | eg_auth_token param.id eg_user_id
# /self/checkouts                                   | post        | eg_auth_token eg_user_id req.json
# /self/hold/:hold                                  | get         | eg_auth_token eg_user_id param.hold
# /self/hold/:hold                                  | patch       | eg_auth_token eg_user_id param.hold req.json
# /self/hold/:hold                                  | delete      | eg_auth_token eg_user_id param.hold "6"
# /self/holds                                       | post        | eg_auth_token eg_user_id req.json
# /self/me                                          | patch       | eg_auth_token req.json

