cmd_exec already have into account if session is meterpreter or shell, maybe this could help to make the code easier, indeed the objective of the Post API is provide session independent methods :)