package org.aktin.broker.client.run;

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;

import org.aktin.broker.client.BrokerClient;
import org.aktin.broker.client.auth.HttpApiKeyAuth;
import org.aktin.broker.xml.RequestStatus;

/**
 * Post request status
 * @author R.W.Majeed
 *
 */
public class PostRequestStatus {

	/**
	 * Post request status. For allowed values, see {@link RequestStatus}
	 * @param args command line arguments. First three are required: Broker URL,  API key, request ID and status to post
	 * @throws IOException IO Error
	 */
	public static void main(String[] args) throws IOException{
		String apiKey = args[1];
		String brokerUrl = args[0];
		int requestId;
		try{
			requestId = Integer.parseInt(args[2]);
		}catch( NumberFormatException e ){
			System.err.println("Unable to parse request id: "+args[2]+": "+e.getMessage());
			System.exit(-1);
			return;
		}
		RequestStatus status;
		try{
			status = RequestStatus.valueOf(args[3]);
		}catch( IllegalArgumentException e ){
			System.err.println("Unable to parse request status: "+args[3]);
			System.err.println("Allowed values: "+Arrays.toString(RequestStatus.values()));
			System.exit(-1);
			return;
		}
		
		BrokerClient client = new BrokerClient(URI.create(brokerUrl));
		client.setClientAuthenticator(HttpApiKeyAuth.newBearer(apiKey));
		client.postRequestStatus(requestId, status);
	}
}

@@ -80,16 +80,42 @@
 et.URI;%0A
+import java.time.Instant;%0A
 import j
@@ -557,16 +557,104 @@
 to post%0A
+%09 * As optional fourth argument, a message/description can be submitted to the server. %0A
 %09 * @thr
@@ -743,16 +743,200 @@
 eption%7B%0A
+%09%09if( args.length %3C 4 %7C%7C args.length %3E 5 )%7B%0A%09%09%09System.out.println(%22Usage: PostRequestStatus %3Cbrokerurl%3E %3Capikey%3E %3Crequestid%3E %3Cstatus%3E %5B%3Cmessage%3E%5D%22);%0A%09%09%09System.exit(-1);%0A%09%09%09return;%0A%09%09%7D%0A
 %09%09String
@@ -1507,16 +1507,183 @@
 ;%0A%09%09%7D%0A%09%09
+String message = null;%0A%09%09if( args.length == 5 )%7B%0A%09%09%09message = args%5B4%5D.replace(%22%5C%5Cn%22, %22%5Cn%22).replace(%22%5C%5Ct%22, %22%5Ct%22);%0A%09%09%09System.out.println(%22Posting message:%22+message);%0A%09%09%7D
 %0A%09%09Broke
@@ -1803,24 +1803,50 @@
 r(apiKey));%0A
+%09%09if( message == null )%7B%0A%09
 %09%09client.pos
@@ -1880,13 +1880,98 @@
 tatus);%0A
+%09%09%7Delse%7B%0A%09%09%09client.postRequestStatus(requestId, status, Instant.now(), message);%0A%09%09%7D%0A
 %09%7D%0A%7D%0A

Allow posting of status message