forked from Yara724/api
Changed AI to new version
This commit is contained in:
Executable
+211
@@ -0,0 +1,211 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/yara724"
|
||||
CONFIG_FILE="$CONFIG_DIR/fanavaran-car-parts.env"
|
||||
|
||||
usage() {
|
||||
cat <<'USAGE'
|
||||
Usage: scripts/get-fanavaran-car-parts.sh [--reset]
|
||||
|
||||
Fetches Fanavaran's complete car-components lookup:
|
||||
GET /BimeApi/v2.0/car/base-info/car-components
|
||||
|
||||
On first run, prompts for the Fanavaran credentials and request header values,
|
||||
then saves them with owner-only permissions under ~/.config/yara724/.
|
||||
Use --reset to enter new values.
|
||||
USAGE
|
||||
}
|
||||
|
||||
die() {
|
||||
printf 'Error: %s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
encode() {
|
||||
printf '%s' "$1" | base64 | tr -d '\n'
|
||||
}
|
||||
|
||||
decode() {
|
||||
printf '%s' "$1" | base64 --decode
|
||||
}
|
||||
|
||||
prompt_value() {
|
||||
local label="$1" variable="$2" secret="${3:-false}" value
|
||||
if [[ "$secret" == true ]]; then
|
||||
read -r -s -p "$label: " value
|
||||
printf '\n' >&2
|
||||
else
|
||||
read -r -p "$label: " value
|
||||
fi
|
||||
[[ -n "$value" ]] || die "$label is required"
|
||||
printf -v "$variable" '%s' "$value"
|
||||
}
|
||||
|
||||
save_config() {
|
||||
mkdir -p "$CONFIG_DIR"
|
||||
chmod 700 "$CONFIG_DIR"
|
||||
local temporary_file
|
||||
temporary_file="$(mktemp "$CONFIG_DIR/.fanavaran-car-parts.XXXXXX")"
|
||||
chmod 600 "$temporary_file"
|
||||
{
|
||||
printf 'BASE_URL=%s\n' "$(encode "$base_url")"
|
||||
printf 'APP_NAME=%s\n' "$(encode "$app_name")"
|
||||
printf 'APP_SECRET=%s\n' "$(encode "$app_secret")"
|
||||
printf 'USERNAME=%s\n' "$(encode "$fanavaran_username")"
|
||||
printf 'PASSWORD=%s\n' "$(encode "$fanavaran_password")"
|
||||
printf 'CORP_ID=%s\n' "$(encode "$corp_id")"
|
||||
printf 'CONTRACT_ID=%s\n' "$(encode "$contract_id")"
|
||||
printf 'LOCATION=%s\n' "$(encode "$location")"
|
||||
} > "$temporary_file"
|
||||
mv -f "$temporary_file" "$CONFIG_FILE"
|
||||
}
|
||||
|
||||
load_config() {
|
||||
[[ -f "$CONFIG_FILE" ]] || return 1
|
||||
# Config values are base64 encoded, so this file is safe to parse as data.
|
||||
while IFS='=' read -r key value; do
|
||||
case "$key" in
|
||||
BASE_URL) base_url="$(decode "$value")" ;;
|
||||
APP_NAME) app_name="$(decode "$value")" ;;
|
||||
APP_SECRET) app_secret="$(decode "$value")" ;;
|
||||
USERNAME) fanavaran_username="$(decode "$value")" ;;
|
||||
PASSWORD) fanavaran_password="$(decode "$value")" ;;
|
||||
CORP_ID) corp_id="$(decode "$value")" ;;
|
||||
CONTRACT_ID) contract_id="$(decode "$value")" ;;
|
||||
LOCATION) location="$(decode "$value")" ;;
|
||||
esac
|
||||
done < "$CONFIG_FILE"
|
||||
[[ -n "${base_url:-}" && -n "${app_name:-}" && -n "${app_secret:-}" && \
|
||||
-n "${fanavaran_username:-}" && -n "${fanavaran_password:-}" && \
|
||||
-n "${corp_id:-}" && -n "${contract_id:-}" && -n "${location:-}" ]] ||
|
||||
die "Saved config is incomplete. Run with --reset to re-enter it."
|
||||
}
|
||||
|
||||
prompt_config() {
|
||||
local client
|
||||
printf 'Choose Fanavaran tenant (parsian, tejaratno, moallem).\n'
|
||||
prompt_value 'Tenant' client
|
||||
case "${client,,}" in
|
||||
parsian|tejaratno) base_url_default='https://apimanager.iraneit.com/BimeApiManager/api' ;;
|
||||
moallem) base_url_default='https://api.inssw.ir/BimeApiManager/api' ;;
|
||||
*) die 'Tenant must be parsian, tejaratno, or moallem' ;;
|
||||
esac
|
||||
|
||||
read -r -p "API manager base URL [$base_url_default]: " base_url
|
||||
base_url="${base_url:-$base_url_default}"
|
||||
base_url="${base_url%/}"
|
||||
[[ "$base_url" == https://* ]] || die 'Base URL must use HTTPS'
|
||||
|
||||
prompt_value 'App name' app_name
|
||||
prompt_value 'App secret' app_secret true
|
||||
prompt_value 'Fanavaran username' fanavaran_username
|
||||
prompt_value 'Fanavaran password' fanavaran_password true
|
||||
prompt_value 'CorpId' corp_id
|
||||
prompt_value 'ContractId' contract_id
|
||||
prompt_value 'Location' location
|
||||
save_config
|
||||
printf 'Credentials saved privately to %s\n' "$CONFIG_FILE" >&2
|
||||
}
|
||||
|
||||
read_response_header() {
|
||||
local wanted="$1" file="$2"
|
||||
awk -F: -v wanted="$wanted" '
|
||||
tolower($1) == tolower(wanted) {
|
||||
sub(/^[^:]*:[[:space:]]*/, "")
|
||||
sub(/\r$/, "")
|
||||
print
|
||||
exit
|
||||
}
|
||||
' "$file"
|
||||
}
|
||||
|
||||
check_http_status() {
|
||||
local status="$1" body_file="$2" label="$3"
|
||||
if [[ ! "$status" =~ ^2[0-9][0-9]$ ]]; then
|
||||
printf '%s failed (HTTP %s). Response:\n' "$label" "$status" >&2
|
||||
cat "$body_file" >&2
|
||||
printf '\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
reset_config=false
|
||||
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
|
||||
usage
|
||||
exit 0
|
||||
elif [[ "${1:-}" == "--reset" ]]; then
|
||||
reset_config=true
|
||||
elif [[ $# -gt 0 ]]; then
|
||||
usage >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if [[ "$reset_config" == true ]] || ! load_config; then
|
||||
prompt_config
|
||||
fi
|
||||
|
||||
command -v curl >/dev/null || die 'curl is required'
|
||||
command -v base64 >/dev/null || die 'base64 is required'
|
||||
command -v node >/dev/null || die 'node is required to parse the login response'
|
||||
|
||||
temporary_dir="$(mktemp -d)"
|
||||
chmod 700 "$temporary_dir"
|
||||
trap 'rm -rf "$temporary_dir"' EXIT
|
||||
|
||||
printf 'Requesting Fanavaran app token...\n' >&2
|
||||
cat > "$temporary_dir/app-token.headers" <<HEADERS
|
||||
appname: $app_name
|
||||
secret: $app_secret
|
||||
Content-Length: 0
|
||||
HEADERS
|
||||
chmod 600 "$temporary_dir/app-token.headers"
|
||||
app_status="$(curl --silent --show-error --output "$temporary_dir/app-token.body" \
|
||||
--dump-header "$temporary_dir/app-token.response-headers" --write-out '%{http_code}' \
|
||||
--request POST --header "@$temporary_dir/app-token.headers" \
|
||||
"$base_url/EITAuthentication/GetAppToken")"
|
||||
check_http_status "$app_status" "$temporary_dir/app-token.body" 'GetAppToken'
|
||||
app_token="$(read_response_header apptoken "$temporary_dir/app-token.response-headers")"
|
||||
[[ -n "$app_token" ]] || die 'GetAppToken succeeded but no apptoken response header was found'
|
||||
|
||||
printf 'Logging in to Fanavaran...\n' >&2
|
||||
cat > "$temporary_dir/login.headers" <<HEADERS
|
||||
appToken: $app_token
|
||||
userName: $fanavaran_username
|
||||
password: $fanavaran_password
|
||||
Content-Length: 0
|
||||
HEADERS
|
||||
chmod 600 "$temporary_dir/login.headers"
|
||||
login_status="$(curl --silent --show-error --output "$temporary_dir/login.body" \
|
||||
--dump-header "$temporary_dir/login.response-headers" --write-out '%{http_code}' \
|
||||
--request POST --header "@$temporary_dir/login.headers" \
|
||||
"$base_url/EITAuthentication/Login")"
|
||||
check_http_status "$login_status" "$temporary_dir/login.body" 'Login'
|
||||
authentication_token="$(read_response_header authenticationtoken "$temporary_dir/login.response-headers")"
|
||||
if [[ -z "$authentication_token" ]]; then
|
||||
authentication_token="$(node -e '
|
||||
const fs = require("node:fs");
|
||||
try {
|
||||
const body = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
|
||||
process.stdout.write(body.authenticationToken || body.authenticationtoken || body.authentication_token || "");
|
||||
} catch {}
|
||||
' "$temporary_dir/login.body")"
|
||||
fi
|
||||
[[ -n "$authentication_token" ]] || die 'Login succeeded but no authentication token was found'
|
||||
|
||||
cat > "$temporary_dir/lookup.headers" <<HEADERS
|
||||
authenticationToken: $authentication_token
|
||||
CorpId: $corp_id
|
||||
ContractId: $contract_id
|
||||
Location: $location
|
||||
Accept: application/json
|
||||
HEADERS
|
||||
chmod 600 "$temporary_dir/lookup.headers"
|
||||
|
||||
printf 'Fetching complete car-parts lookup...\n' >&2
|
||||
lookup_status="$(curl --silent --show-error --output "$temporary_dir/lookup.body" \
|
||||
--write-out '%{http_code}' --request GET --header "@$temporary_dir/lookup.headers" \
|
||||
"$base_url/BimeApi/v2.0/car/base-info/car-components")"
|
||||
check_http_status "$lookup_status" "$temporary_dir/lookup.body" 'Car-parts lookup'
|
||||
cat "$temporary_dir/lookup.body"
|
||||
printf '\n'
|
||||
Reference in New Issue
Block a user