diff --git a/code/common.sh b/code/common.sh index 26877ce..aaf5bfc 100644 --- a/code/common.sh +++ b/code/common.sh @@ -62,7 +62,14 @@ function add_account_authtoken() { function update_account_authtoken() { a=$(cat secret/refreshTokens.dat | grep -P "^$1\:") token=$(awk -F: '{print $2}' <<< $a); - auth=$(curl -s -X POST -H "Content-type: application/json" --data '{"phoneOS": "Android", "refreshToken": "'"$token"'"}' https://api-inmobile-pl.easypack24.net/v1/authenticate | jq -r .authToken | sed -s 's/Bearer //g') + while true; do + if [[ "$auth" == '' ]]; then + auth=$(curl -s -X POST -H "Content-type: application/json" --data '{"phoneOS": "Android", "refreshToken": "'"$token"'"}' https://api-inmobile-pl.easypack24.net/v1/authenticate | jq -r .authToken | sed -s 's/Bearer //g') + sleep 1 + else + break + fi + done sed -E -i 's/^'"$1"':.*/'"$1:$auth"'/g' secret/authTokens.dat } diff --git a/webroot/api/collect.shs b/webroot/api/collect.shs index a5ccd90..5c05cbb 100644 --- a/webroot/api/collect.shs +++ b/webroot/api/collect.shs @@ -5,16 +5,22 @@ fi source "${cfg[namespace]}/code/common.sh" -data=$(curl -s -X POST -H "$(get_auth_string)" -H "Content-type: application/json" "https://api-inmobile-pl.easypack24.net/v1/collect/validate" --data '{"geoPoint":{"accuracy":"1","latitude":"'"${post_data[lat]}"'","longitude":"'"${post_data[lon]}"'"},"parcel":{"openCode":"'"${post_data[openCode]}"'","shipmentNumber":"'"${post_data[id]}"'"}}') +data='' +while [[ "$data" == '' ]]; do + data=$(curl -s -X POST -H "$(get_auth_string)" -H "Content-type: application/json" "https://api-inmobile-pl.easypack24.net/v1/collect/validate" --data '{"geoPoint":{"accuracy":"1","latitude":"'"${post_data[lat]}"'","longitude":"'"${post_data[lon]}"'"},"parcel":{"openCode":"'"${post_data[openCode]}"'","shipmentNumber":"'"${post_data[id]}"'"}}') +done if [[ $(echo $data | jq .status) == "403" ]]; then echo $data | jq "{status: .status, msg: .error}" else uuid=$(echo $data | jq -r ".sessionUuid") - data=$(curl -X POST -H "$(get_auth_string)" "https://api-inmobile-pl.easypack24.net/v1/collect/compartment/open/$uuid") + data='' + while [[ "$data" == '' ]]; do + data=$(curl -X POST -H "$(get_auth_string)" "https://api-inmobile-pl.easypack24.net/v1/collect/compartment/open/$uuid") + done if [[ $(echo $data | jq ".compartment") != "" ]]; then echo $data else - jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "emptyPage" + jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "OCW received an invalid response from the API." fi fi diff --git a/webroot/api/mailcode.shs b/webroot/api/mailcode.shs index 53c37e6..34ceaa6 100644 --- a/webroot/api/mailcode.shs +++ b/webroot/api/mailcode.shs @@ -11,7 +11,10 @@ if [[ "${post_data[code]}" != '' && "${post_data[phone]}" != '' ]]; then mail="$(get_account_mail $username)" if [[ "$(cat secret/mail.dat | grep "$username:" | grep ":${post_data[code]}:")" != '' ]]; then sed -i "s/$username:$mail:${post_data[code]}:no/$username:$mail:${post_data[code]}:yes/" secret/mail.dat - curl -s "https://api-inmobile-pl.easypack24.net/v1/sendSMSCode/${post_data[phone]}" > /dev/null + x='' + while [[ "$x" == '' ]]; do + x=$(curl -s "https://api-inmobile-pl.easypack24.net/v1/sendSMSCode/${post_data[phone]}") + done jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args 200 "success" elif [[ $status == 1 ]]; then jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "$reason" diff --git a/webroot/api/packages.shs b/webroot/api/packages.shs index 211a811..951f98b 100644 --- a/webroot/api/packages.shs +++ b/webroot/api/packages.shs @@ -3,6 +3,10 @@ if ! silent=yes source "${cfg[namespace]}/webroot/api/session.shs"; then return fi -data=$(curl -s -H "$(get_auth_string)" "https://api-inmobile-pl.easypack24.net/v1/parcel?updatedAfter=1970-01-01T00:00:00.000Z") +data='' -echo $data | jq -c "[.[] | {shipmentNumber: .shipmentNumber, status: .status, openCode: .openCode, senderName: .senderName, pickupPoint: {name: .pickupPoint.name, status: .pickupPoint.status, location: .pickupPoint.location, description: .pickupPoint.locationDescription, address: .pickupPoint.addressDetails}}]" +while [[ "$data" == '' ]]; do + data=$(curl -s -H "$(get_auth_string)" "https://api-inmobile-pl.easypack24.net/v1/parcel?updatedAfter=1970-01-01T00:00:00.000Z") +done + +echo "$data" | jq -c "[.[] | {shipmentNumber: .shipmentNumber, status: .status, openCode: .openCode, senderName: .senderName, pickupPoint: {name: .pickupPoint.name, status: .pickupPoint.status, location: .pickupPoint.location, description: .pickupPoint.locationDescription, address: .pickupPoint.addressDetails}}]" diff --git a/webroot/api/smscode.shs b/webroot/api/smscode.shs index b1fcaf5..2daf306 100644 --- a/webroot/api/smscode.shs +++ b/webroot/api/smscode.shs @@ -7,7 +7,11 @@ fi source "${cfg[namespace]}/code/common.sh" if [[ "${post_data[phone]}" != "" && "${post_data[code]}" != "" ]]; then - tokens=$(curl -s -X POST -H "Content-type: application/json" --data '{"phoneOS":"Android"}' "https://api-inmobile-pl.easypack24.net/v1/confirmSMSCode/${post_data[phone]}/${post_data[code]}") + tokens='' + while [[ "$tokens" == '' ]]; do + tokens=$(curl -s -X POST -H "Content-type: application/json" --data '{"phoneOS":"Android"}' "https://api-inmobile-pl.easypack24.net/v1/confirmSMSCode/${post_data[phone]}/${post_data[code]}") + sleep 0.5 + done if [[ $(echo $tokens | jq .status) == "404" ]]; then jq -n '{status: $ARGS.positional[0], msg: $ARGS.positional[1]}' --args -1 "wrongData"