The New York City mayor, already a foe of the left, owes his legal reprieve to the polarizing Republican president.