-
Notifications
You must be signed in to change notification settings - Fork 1
/
myacclaro.sh
executable file
·1054 lines (945 loc) · 29.8 KB
/
myacclaro.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
### "Global" variables to work with
SCRIPT=$( basename "$0" )
VERSION="0.5-beta"
consoleActivated=false #sets the console mode off by default
##################
# Core Functions #
##################
### Basic Log
logFile=/tmp/myAcclaro.log
if [[ ! -e /tmp/myAcclaro.log ]]; then
touch /tmp/myAcclaro.log
fi
### Allow History
if [[ ! -e /tmp/myAcclaroConsole.history ]]; then
touch /tmp/myAcclaroConsole.history
fi
history -r /tmp/myAcclaroConsole.history
history -w /tmp/myAcclaroConsole.history
# Checks that dependencies are installed
function checkToolsInstalled()
{
tools=("curl" "jq")
for tool in ${tools[@]}
do
output=$(${tool} --version 2>&1)
if [ $? -ne 0 ]; then
execFailed "[${tool}] is necessary to run this application, exiting."
handleExit 1
fi
done
} #checkCurlInstalled
# Function to handle success messages
function execSuccess()
{
echo -e "$(date +%F\ %T) :: [\e[32mSUCCESS\e[39m] - $1" | tee -a ${logFile}
} #execSuccess
# Function to handle failed messages
function execFailed()
{
echo -e "$(date +%F\ %T) :: [\e[31mFAIL\e[39m] - $1" | tee -a ${logFile}
} #execFailed
# Checks that mandatory arguments are sent to function before launching
function checkNotEmpty()
{
if [[ -z $1 ]]; then
execFailed "The following mandatory argument [$2] is empty."
handleExit 1
fi
} #checkNotEmpty
# Checks if CSV output is requested
function outputCsv()
{
if [[ "$1" == "-csv" ]]; then
csvOutput=true
fi
} #outputCsv
# Handles the exit, if on script it should exit 1 but on console it should just return error
function handleExit()
{
if [[ "${consoleActivated}" != true ]]; then
exit $1
consoleDie=false #this is not really needed
else
echo $1 >/dev/null #just do something xD
consoleDie=true #set variable to true so that it is read in the function and it exits the function
fi
} #handleExit
# Message to display when wrong usage of scirpt.
function wrongUsage()
{
local message="$1"
local txt=(
"For an overview of the command, execute:"
"$SCRIPT --help"
)
[[ ${message} ]] && printf "${message}\n"
printf "%s\n" "${txt[@]}"
} #wrongUsage
function wrongUsageConsole()
{
echo "command '${line[0]}' not found"
} #wrongUsage
# Message to display for version (script).
function version
{
local txt=(
"$SCRIPT version $VERSION"
)
printf "%s\n" "${txt[@]}"
} #version
# Message to display version (console)
function versionConsole()
{
echo "MyAcclaro Console ${VERSION}"
} #versionConsole
# Message to display for usage and help (script).
function usage()
{
local txt=(
"Utility ${SCRIPT} for Querying My Acclaro's REST API."
"Usage: $SCRIPT <base URL> <API key> [options] <arguments>"
""
"Command:"
"--------"
" <base URL> e.g. \"apisandbox.acclaro.com\""
" <api Key> e.g. \"pYXQiOjE2MjYyODYxOTIsInN1YiI6...\""
""
"Options:"
"--------"
" --help, -h Print help."
" --version, -v Print version."
" --console, -c Starts the interactive Console Mode"
""
"Parameters"
"----------"
"* Orders:"
" --create-order, -co <name> [string] Creates an Order, if \"string\" added as parameter, then the Order takes strings rather than files."
" --add-target-lang, -atl <orderID> <targetLang> Adds a target language to the specified Order."
" --add-lang-pair, -alp <orderID> <sourceLang> <targetLang> Adds a language pair to the specified Order."
" --get-order-details, -god <orderID> Gets the specified Order details."
" --get-all-order-details, -gaod <orderID> Gets all Order details."
" --set-order-comment, -soc <orderID> <comment> Sets a comment for the Order"
" --get-order-comments, -goc <orderID> Gets the Order comments"
" --submit-order, -so <orderID> Submits the Order for preparation and then translation."
"* Files:"
" --send-file, -sf <orderID> <sourceLang> <targertLang> <path_to_file> Sends a source file for translation."
" --send-reference-file, -srf <orderID> <sourceLang> <targertLang> <path_to_reference_file> Sends a reference file (e.g. a styleguide) for a particular language."
" --get-file, -gf <orderID> <fileID> Gets a file based on its ID."
" --get-file-info, -gfi <orderID> <fileID> Gets the information of a file based on its ID."
"* Strings:"
" --post-string, -ps <orderID> <sourceString> <sourceLang> <targertLang> Posts a string for translation."
" --get-string-info, -gsi <orderID> <stringID> Gets the string information and the translated string once completed."
"* Quotes:"
" --request-quote, -rq <orderID> Requests a quote for the Order."
" --get-quote-details, -gqd <orderID> Gets the Quote status and details for the Order."
" --quote-decision, -qd <orderID> [--approve,-a/--decline,-d] Approves/declines the quoted price for the Order."
""
"Example:"
" $SCRIPT \"apisandbox.acclaro.com\" \"pYXQiOjE2MjYyODYxOTIsInN1YiI6...\" --send-file \"15554\" \"en-us\" \"de-de\" \"./mySourceFile.docx\""
)
printf "%s\n" "${txt[@]}"
} #usage
# Message to display for usage and help (console).
function usageConsole()
{
local txt=(
""
"MyAcclaro Console for Querying MyAcclaro's REST API."
""
"System Commands:"
" login Interactive login to MyAcclaro API."
" logout Clears the login information."
" exit Exits the MyAcclaro console"
" help Print help."
" version Print version."
""
"Shell commands:"
" The following shell commands are available: ls, cd, pwd, grep, find, cat, less"
" [CTRL+C] Is captured by this script"
""
"MyAcclaro Commands"
"* Orders:"
" create-order <name> [string] Creates an Order, if \"string\" added as parameter, then the Order takes strings rather than files."
" add-target-lang <orderID> <targetLang> Adds a target language to the specified Order."
" add-lang-pair <orderID> <sourceLang> <targetLang> Adds a language pair to the specified Order."
" get-order-details <orderID> Gets the specified Order details."
" get-all-order-details <orderID> Gets all Order details."
" set-order-comment <orderID> <comment> Sets a comment for the Order"
" get-order-comments <orderID> Gets the Order comments"
" submit-order <orderID> Submits the Order for preparation and then translation."
"* Files:"
" send-file <orderID> <sourceLang> <targertLang> <path_to_file> Sends a source file for translation."
" send-reference-file <orderID> <sourceLang> <targertLang> <path_to_reference_file> Sends a reference file (e.g. a styleguide) for a particular language."
" get-file <orderID> <fileID> Gets a file based on its ID."
" get-file-info <orderID> <fileID> Gets the information of a file based on its ID."
"* Strings:"
" post-string <orderID> <sourceString> <sourceLang> <targertLang> Posts a string for translation."
" get-string-info <orderID> <stringID> Gets the string information and the translated string once completed."
"* Quotes:"
" request-quote <orderID> Requests a quote for the Order."
" get-quote-details <orderID> Gets the Quote status and details for the Order."
" quote-decision <orderID> [--approve,-a/--decline,-d] Approves/declines the quoted price for the Order."
""
""
"Example:"
"MyAcclaro@test> send-file 15554 en-us de-de ./mySourceFile.docx"
""
)
printf "%s\n" "${txt[@]}"
} #usageConsole
############
# Console #
############
# Main console handler, builds the "console echo"
function console()
{
trap ctrl_c INT #trap control+c and send it to function to handle
unset line #clean before using, for sanity purposes! :)
if [[ -z ${baseUrl} || -z ${apiKey} ]]; then
read -e -p $(echo -e -n "\e[33mMyAcclaro@DISCONNECTED\e[39m>") input
history -s "${input}"
eval line=(${input})
else
read -e -p $(echo -e -n "\e[92mMyAcclaro@${baseUrl}\e[39m>") input
history -s "${input}"
eval line=(${input})
fi
} #console
function ctrl_c()
{
printf "\n[CTRL+C] detected - if you need to exit, please type 'exit' (press 'enter' to continue)"
}
# Allows to set the domain and the API key for MyAcclaro API
function logIn()
{
if [[ -z ${baseUrl} || -z ${apiKey} ]]; then
echo -e -n "Please enter the MyAcclaro domain: "
read -e -a domain
echo -e -n "Please paste your API key here: "
read -e -a key
response=$(
curl --location --silent --request GET "https://${domain}/api/v2/info/account" \
--header "Authorization: Bearer ${key}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
firstname=$(echo ${response} | jq -r '(.data.firstname)')
lastname=$(echo ${response} | jq -r '(.data.lastname)')
execSuccess "Welcome back ${firstname} ${lastname}!"
baseUrl=${domain}
apiKey=${key}
else
execFailed "the domain or the API key are wrong, please check output"
echo ${response} | jq
fi
else
echo "you are already logged in into [${baseUrl}] using the following API key:"
echo "[${apiKey}]"
fi
} #logIn
# Unsets the domain and the API key for MyAcclaro API
function logOut()
{
if [[ -z ${baseUrl} || -z ${apiKey} ]]; then
execFailed "You cannot be logged out because you are not logged in!"
else
baseUrl=""
apiKey=""
execSuccess "You have been successfully logged out."
fi
} #logOut
# Creates a greeting screen so it looks cooler
function headerGreeting()
{
versionConsole
local txt=(
""
" _ "
" /\ | | "
" _ __ ___ _ _ / \ ___ ___| | __ _ _ __ ___ "
" | _ _ \| | | | / /\ \ / __/ __| |/ _ | \__/ _ \ "
" | | | | | | |_| |/ ____ \ (_| (__| | (_| | | | (_) | "
" |_| |_| |_|\__ /_/ \_\___\___|_|\__ _|_| \___/ "
" __/ | "
" |___/ "
""
""
"Type 'help' to see the available commands."
""
)
printf "%s\n" "${txt[@]}"
} #headerGreeting
# Provides the command control for console
function consoleMode()
{
consoleActivated=true
headerGreeting
console
while [ "${line[0]}" != "exit" ]
do
case "${line[0]}" in
ls)
"${line[@]}"
;;
cd)
"${line[0]}" "${line[1]}"
;;
pwd)
"${line[@]}"
;;
grep)
"${line[@]}"
;;
find)
"${line[@]}"
;;
cat)
"${line[@]}"
;;
less)
"${line[@]}"
;;
help)
usageConsole
;;
version)
versionConsole
;;
create-order)
createAnOrder "${line[1]}" "${line[2]}"
;;
post-string)
postString "${line[1]}" "${line[2]}" "${line[3]}" "${line[4]}"
;;
send-file)
sendFile "${line[1]}" "${line[2]}" "${line[3]}" "${line[4]}"
;;
send-reference-file)
sendReferenceFile "${line[1]}" "${line[2]}" "${line[3]}" "${line[4]}"
;;
get-order-details)
getOrderDetails "${line[1]}"
;;
get-all-order-details)
getAllOrderDetails
;;
submit-order)
submitOrder "${line[1]}"
;;
get-string-info)
getStringInfo "${line[1]}" "${line[2]}"
;;
get-file)
getFile "${line[1]}" "${line[2]}"
;;
get-file-info)
getFileInfo "${line[1]}" "${line[2]}"
;;
get-order-comments)
getComments "${line[1]}" "${line[2]}"
;;
set-order-comment)
setComment "${line[1]}" "${line[2]}"
;;
request-quote)
requestQuote "${line[1]}"
;;
get-quote-details)
getQuoteDetails "${line[1]}"
;;
quote-decision)
quoteWorkflow "${line[1]}" "${line[2]}"
;;
add-target-lang)
addTargetToOrder "${line[1]}" "${line[2]}"
;;
add-lang-pair)
addSourceAndTargetToOrder "${line[1]}" "${line[2]}" "${line[3]}"
;;
login)
logIn
;;
logout)
logOut
;;
"")
#do nothing
;;
*)
wrongUsageConsole
;;
esac
console
done
exit 0
} #consoleMode
####################
# API WRAPPING #
####################
function createAnOrder()
{
orderName=$1
string=$2
if [ "${string}" = "string" ]; then
processType="--form process_type=\"string\""
else
processType=""
fi
checkNotEmpty "${orderName}" "<name>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --silent --location --request POST "https://${baseUrl}/api/v2/orders" \
--header 'Content-Type: multipart/form-data' \
--header "Authorization: Bearer ${apiKey}" \
--form "name=\"${orderName}\"" \
${processType} \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
orderId=$(jq -r '.data.orderid' <<< ${response})
if [ "${string}" = "string" ]; then
execSuccess "Your Order for Strings has been created and has id [${orderId}]"
else
execSuccess "Your Order for Files has been created and has id [${orderId}]"
fi
else
execFailed "There was a problem while creating your Order, please see the response below:"
echo ${response} | jq
fi
} #createAnOrder
function postString ()
{
orderId=$1
checkNotEmpty "${orderId}" "<orderID>"
sourceString=$2
checkNotEmpty "${sourceString}" "<sourceString>"
sourceLang=$3
checkNotEmpty "${sourceLang}" "<sourceLang>"
targertLang=$4
checkNotEmpty "${targertLang}" "<targertLang>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --silent --location --request POST "https://${baseUrl}/api/v2/orders/${orderId}/strings" \
--header "Authorization: Bearer ${apiKey}" \
--header 'Content-Type: application/json' \
--data-raw "{\"strings\":[{\"value\":\"${sourceString}\",\"target_lang\":[\"${targertLang}\"],\"source_lang\": \"${sourceLang}\"}]}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
stringId=$(jq -r '.data.strings[].string_id' <<< ${response})
execSuccess "Your string has been posted to Order [${orderId}] and has String ID: [${stringId}]"
else
execFailed "There was a problem while posting your string, please see bellow the response:"
echo ${response} | jq
handleExit 1
fi
resultStringId=${stringId}
} #postString
function sendFile ()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
sourceLang=$2
checkNotEmpty "${sourceLang}" "<sourceLang>"
targetLang=$3
checkNotEmpty "${targetLang}" "<targertLang>"
pathToSourceFile=$4
checkNotEmpty "${pathToSourceFile}" "<path_to_file>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --silent --location --request POST "https://${baseUrl}/api/v2/orders/${orderId}/files" \
--header 'Content-Type: multipart/form-data' \
--header "Authorization: Bearer ${apiKey}" \
--form "sourcelang=\"${sourceLang}\"" \
--form "targetlang=\"${targetLang}\"" \
--form "file=@\"${pathToSourceFile}\"" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
fileId=$(jq -r '.data.fileid' <<< ${response})
execSuccess "Your source file has been posted to Order [${orderId}] and has File ID: [${fileId}]"
else
execFailed "There was a problem while sending your source file, please see bellow the response:"
echo ${response} | jq
handleExit 1
fi
} #sendFile
function getOrderDetails ()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --silent --location --request GET "https://${baseUrl}/api/v2/orders/${orderId}" \
--header "Authorization: Bearer ${apiKey}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
#getting the attributes using jq - yes, an array would have made sense
orderName=$(jq -r '.data.name' <<< "${response}")
orderStatus=$(jq -r '.data.status' <<< "${response}")
processType=$(jq -r '.data.process_type' <<< "${response}")
dueDate=$(jq -r '.data.duedate' <<< "${response}")
createdDate=$(jq -r '.data.created' <<< "${response}")
createdBy=$(jq -r '.data.emailaddress' <<< "${response}")
sourceLang=$(jq -r '.data.sourcelang' <<< "${response}")
targetLangs=$(jq -r '.data.targetlang[]' <<< "${response}")
execSuccess "Your Order [${orderId}] has the following attributes:"
local txt=(
""
" * Order ID: ${orderId}"
" * Order Name: ${orderName}"
" * Status: ${orderStatus}"
" * Process Type: ${processType}"
" * Source Language: ${sourceLang}"
" * Target Language(s): ${targetLangs[@]}"
" * Creation Date: ${createdDate}"
" * Created By: ${createdBy}"
" * Due date: ${dueDate}"
""
)
printf "%s\n" "${txt[@]}"
else
execFailed "There was a problem while getting your Order, please see the response below:"
echo ${response} | jq
handleExit 1
fi
} #getOrderDetails
function getAllOrderDetails ()
{
outputCsv $1
response=$(
curl --silent --location --request GET "https://${baseUrl}/api/v2/orders" \
--header "Authorization: Bearer ${apiKey}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
if [ "${csvOutput}" = true ]; then
echo ${response} | jq -r '.data[] | [.orderid, .name, .status, .process_type, .user.email, .duedate] | @csv' | awk -v FS="\t" 'BEGIN{print "\"Order ID\",\"Order Name\",\"Status\",\"Type\",\"Creator\",\"Due Date\""}{printf "%s\t%s\t%s%s\t%s\t%s\t%s",$1,$2,$3,$4,$5,$6,ORS}'
else
execSuccess "Please see a list of Orders bellow"
echo ${response} | jq -r '["Order ID","Order Name","Status","Process Type","Creator","Due Date"], ["--------","----------","------","------------","-------","--------"], (.data[] | [.orderid, .name, .status, .process_type, .user.email, .duedate]) | @tsv' | column -ts $'\t'
fi
else
execFailed "There was a problem while getting your Order, please see the response below:"
echo ${response} | jq
handleExit 1
fi
} #getAllOrderDetails
function submitOrder ()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --silent --location --request POST "https://${baseUrl}/api/v2/orders/${orderId}/submit" \
--header "Authorization: Bearer ${apiKey}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
execSuccess "Your Order [${orderId}] has beeen submitted"
else
execFailed "There was a problem while submitting your Order, please see the response below:"
echo ${response} | jq
handleExit 1
fi
} #submitOrder
function getStringInfo ()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
stringId=$2
checkNotEmpty "${stringId}" "<stringID>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --silent --location --request GET "https://${baseUrl}/api/v2/orders/${orderId}/strings/${stringId}" \
--header "Authorization: Bearer ${apiKey}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
translatedString=$(jq -r '.data.translated_value' <<< ${response})
stringStatus=$(jq -r '.data.status' <<< "${response}")
if [ "$stringStatus" != "complete" ]; then
execSuccess "Your Order [${orderId}] has a string with ID [${stringId}] which has not yet been translated, and its status is [${stringStatus}]."
else
execSuccess "Your Order [${orderId}] has a string with ID [${stringId}] which has been translated."
printf "\n\t* Translated String: ${translatedString}\n"
fi
else
execFailed "There was a problem while getting your string, please see the response below:"
echo ${response} | jq
handleExit 1
fi
} #getStringInfo
function getFile ()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
fileId=$2
checkNotEmpty "${fileId}" "<FileID>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
#set a filename if needed, you can also use the "fileName" variable form the function "getFileInfo"
fileName="$(pwd)/output.myacclaro"
response=$(
curl --silent --location --request GET -w "%{http_code}" "https://${baseUrl}/api/v2/orders/${orderId}/files/${fileId}" -o ${fileName} \
--header "Authorization: Bearer ${apiKey}" \
)
if [ ${response} -eq 200 ]; then
writeFileTest=$(echo ${fileName} >${fileName}.test && rm ${fileName}.test) #test to check if we can write in the destination folder, cURL will not return an error for that (ugly hack)
if [ $? -eq 0 ]; then
execSuccess "Your file with ID [${fileId}] has been downloaded here: ${fileName}"
else
execFailed "The system failed while trying to save the file, please check the error message"
echo ${writeFile}
handleExit 1
fi
else
execFailed "There was a problem while getting your file, the status code is [${response}]"
handleExit 1
fi
} #getFile
function getFileInfo ()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
fileId=$2
checkNotEmpty "${fileId}" "<FileID>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --silent --location --request GET "https://${baseUrl}/api/v2/orders/${orderId}/files/${fileId}/status" \
--header "Authorization: Bearer ${apiKey}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
fileName=$(jq -r '.data.filename' <<< ${response})
fileStatus=$(jq -r '.data.status' <<< ${response})
targetFileId=$(jq -r '.data.targetfile' <<< ${response})
if [[ "$fileStatus" == "complete" ]]; then
execSuccess "Your file [${fileName}] has been translated, status is [${fileStatus}]."
echo "Please proceed to download the translated file using this file ID: [${targetFileId}]"
else
execSuccess "Your file [${fileName}] has yet not been translated and has status: [${fileStatus}]"
fi
else
execFailed "There was a problem while getting your file info, please see the response below:"
echo ${response} | jq
handleExit 1
fi
} #getFileInfo
function getComments ()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
outputCsv $2
response=$(
curl --location --silent --request GET "https://${baseUrl}/api/v2/orders/${orderId}/comments" \
--header "Authorization: Bearer ${apiKey}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
if [ "${csvOutput}" = true ]; then
echo ${response} | jq -r '.data[] | [.author, .timestamp, .comment] | @csv' | awk -v FS="\t" 'BEGIN{print "\"Author\",\"Creation Date\",\"Comment\""}{printf "%s\t%s\t%s%s",$1,$2,$3,ORS}'
else
execSuccess "Your Order [${orderId}] has comments, please see comments bellow:"
echo ${response} | jq -r '["Author","Timestamp","Comment"], ["------","---------","-------"], (.data[] | [.author, .timestamp, .comment] ) | @tsv' | column -ts $'\t'
fi
else
execFailed "There was a problem while getting your comments"
echo ${response} | jq
handleExit 1
fi
} #getComments
function setComment ()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
commentLine=$2
checkNotEmpty "${commentLine}" "<Comment>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --location --silent --request POST "https://${baseUrl}/api/v2/orders/${orderId}/comment" \
--header "Authorization: Bearer ${apiKey}" \
--data-urlencode "comment=${commentLine}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
execSuccess "Your Order [${orderId}] has been added the following comment:"
echo " * ${commentLine}"
else
execFailed "There was a problem while posting your comment"
echo ${response} | jq
handleExit 1
fi
} #setComment
function requestQuote()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --location --silent --request GET "https://${baseUrl}/api/v2/orders/${orderId}/quote" \
--header "Authorization: Bearer ${apiKey}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
execSuccess "Quote succesfully requested for Order [${orderId}]"
else
execFailed "There was a problem while requestiong your Quote for Order [${orderId}]"
echo ${response} | jq
handleExit 1
fi
} #requestQuote
function getQuoteDetails()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --location --silent --request GET "https://${baseUrl}/api/v2/orders/${orderId}/quote-details" \
--header "Authorization: Bearer ${apiKey}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
totalQuote=$(jq -r '.data.total' <<< ${response})
execSuccess "Quote details for Order [${orderId}] are bellow:"
echo ${response} | jq -r '["Description","Quantity","Unit Price","Subtotal"], ["-----------","--------","----------","--------"], (.data.lines[] | [.description, .quantity, "$"+.unitprice, "$"+.price]) | @tsv' | column -ts $'\t'
echo "** TOTAL: \$${totalQuote}"
else
execFailed "There was a problem while getting your Quote"
echo ${response} | jq
handleExit 1
fi
} #getQuoteDetails
function quoteWorkflow()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
checkNotEmpty "$2" "<--approve/--decline>"
case "$2" in
--approve | -a)
quoteDecision="quote-approve"
verbPresCont="approving"
verbPast="approved"
;;
--decline | -d)
quoteDecision="quote-decline"
verbPresCont="declining"
verbPast="declined"
;;
*)
execFailed "You should either decline or approve the quote [--approve/--decline]"
handleExit 1
return 1 #stop executing quoteWorkflow
;;
esac
response=$(
curl --location --silent --request POST "https://${baseUrl}/api/v2/orders/${orderId}/${quoteDecision}" \
--header "Authorization: Bearer ${apiKey}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
execSuccess "The Quote for Order [${orderId}] has been succesfully ${verbPast}"
else
execFailed "There was a problem while ${verbPresCont} your Quote for Order [${orderId}]"
echo ${response} | jq
handleExit 1
fi
} #quoteWorkflow
function addTargetToOrder()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
targetLang=$2
checkNotEmpty "${targetLang}" "<targetLang>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --location --silent --request POST "https://${baseUrl}/api/v2/orders/${orderId}/language" \
--header "Authorization: Bearer ${apiKey}" \
--data-urlencode "targetlang=${targetLang}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
execSuccess "The following target language: [${targetLang}] has been succesfully added to the Order [${orderId}]"
else
execFailed "There was a problem while adding the target lang [${targetLang}] to Order [${orderId}]"
echo ${response} | jq
handleExit 1
fi
} #addTargetToOrder
function addSourceAndTargetToOrder()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
sourceLang=$2
checkNotEmpty "${sourceLang}" "<sourceLang>"
targetLang=$3
checkNotEmpty "${targetLang}" "<targetLang>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --location --silent --request POST "https://${baseUrl}/api/v2/orders/${orderId}/language-pair" \
--header "Authorization: Bearer ${apiKey}" \
--data-urlencode "sourcelang=${sourceLang}" \
--data-urlencode "targetlang=${targetLang}" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
execSuccess "The following language pair: [${sourceLang} > ${targetLang}] has been succesfully added to the Order [${orderId}]"
else
execFailed "There was a problem while adding the language pair: [${sourceLang} > ${targetLang}] to Order [${orderId}]"
echo ${response} | jq
handleExit 1
fi
} #addSourceAndTargetToOrder
function sendReferenceFile()
{
orderId=$1
checkNotEmpty "${orderId}" "<OrderID>"
sourceLang=$2
checkNotEmpty "${sourceLang}" "<sourceLang>"
targetLang=$3
checkNotEmpty "${targetLang}" "<targertLang>"
pathToReferenceFile=$4
checkNotEmpty "${pathToReferenceFile}" "<path_to_reference_file>"
if [[ "${consoleDie}" == true ]]; then
return 1
fi
response=$(
curl --silent --location --request POST "https://${baseUrl}/api/v2/orders/${orderId}/reference-file" \
--header 'Content-Type: multipart/form-data' \
--header "Authorization: Bearer ${apiKey}" \
--form "sourcelang=\"${sourceLang}\"" \
--form "targetlang=\"${targetLang}\"" \
--form "file=@\"${pathToReferenceFile}\"" \
)
if [ $? -eq 0 ] && [[ $(jq -r '.success' <<< ${response}) == true ]]; then
fileId=$(jq -r '.data.fileid' <<< ${response})
execSuccess "Your reference file has been posted to Order [${orderId}] for the following target lang(s): [${targetLang}]"
else
execFailed "There was a problem while sending your reference file, please see bellow the response:"
echo ${response} | jq
handleExit 1
fi
} #sendReferenceFile
################
### BODY ###
################
# check that all dependencies are met before starting
checkToolsInstalled
# if no arguments defined, then print script usage
if [[ $# -eq 0 ]] ; then
usage
exit 1
fi
# read arguments and execute accordingly
while (( $# ))
do
case "$1" in
--help | -h)
usage
exit 0
;;
--version | -v)
version
exit 0
;;
--console | -c)
consoleMode
;;
esac
baseUrl="$1"
checkNotEmpty "${baseUrl}" "base URL"
apiKey="$2"
checkNotEmpty "${apiKey}" "API key"
case "$3" in
--create-order | -co)
createAnOrder "$4" "$5"
exit 0
;;
--post-string | -ps)
postString "$4" "$5" "$6" "$7"
exit 0
;;
--send-file | -sf)
sendFile "$4" "$5" "$6" "$7"
exit 0
;;
--send-reference-file | -srf)
sendReferenceFile "$4" "$5" "$6" "$7"
exit 0
;;
--get-order-details | -god)
getOrderDetails "$4"
exit 0
;;
--get-all-order-details | -gaod)
getAllOrderDetails "$4"
exit 0
;;
--submit-order | -so)
submitOrder "$4"
exit 0
;;
--get-string-info | -gsi)
getStringInfo "$4" "$5"
exit 0