Given Nigeria's size, wealth and geopolitical importance, there is a tendency to assume that its president can speak for his continent. When Muhammadu Buhari landed in Washington this week, international media were fixated on a foul-mouthed January tirade by Donald Trump, wherein he likened African countries to a toilet.

But those expecting fireworks were left disappointed. More than 15 months after Mr Trump took office, Mr Buhari is the first sub-Saharan leader to visit the White House. His cordial four-day stopover is best viewed not through the lens of US-Africa relations, but rather as an important spectacle for two leaders each facing the looming prospect of electoral disaster.

Concerns that relations had soured were not entirely unfounded. Nigeria was one of several African countries to summon the US ambassador over Mr Trump’s comments in January. With its majority-Muslim population, Nigeria also condemned the planned US embassy move from Tel Aviv to Jerusalem. And Abuja was perturbed when former Secretary of State Rex Tillerson was fired while visiting Nigeria.

But in the end, both leaders had too much to gain.

After riding a wave of optimism into office in 2015, illness saw Mr Buhari hospitalised for months in London, while his domestic agenda stalled. Amid rampant corruption, his popularity has dwindled. Last month he sparked controversy when he said the Nigerian youth was “lazy”.

His biggest failure is the war against Boko Haram, that has killed at least 20,000 people in nine years and looks no closer to a resolution. While Mr Buhari was in Washington, a double suicide attack hit a mosque in Mubi, northeast Nigeria, killing 29 according to authorities. Local gravediggers have said they buried 86 people.

Nigeria’s weak security apparatus has bred a booming kidnapping industry and has exacerbated bloody clashes between nomadic herdsmen and farming communities over central Nigeria’s limited resources. In late April, 19 parishioners, including two priests, died in the violence.

In spite of his frailty, the 75-year-old leader has defied expectations to seek re-election in 2019.

Mr Trump is himself facing the November midterm elections, which analysts suggest could be ruinous for his Republican party. With mounting scandal at home and paralysis in his key initiatives, Mr Trump is keen to lift his approval ratings and enliven his base.

Against that backdrop, Mr Buhari’s visit, and its outcomes, make sense.

As the first sub-Saharan leader to visit the Trump White House, Mr Buhari could demonstrate his fitness for office, particularly among Nigerian Christians, who revere Mr Trump for his tough stance on Islam. It is a group whose support Mr Buhari – himself a Muslim – will need to court ahead of next year’s election.

Evangelical Christians are the cornerstone of Mr Trump’s base. He won plaudits when he said of the clashes in central Nigeria: “We’ve had very serious problems with Christians who have been murdered, killed in Nigeria... we can’t allow that to happen.”

For the fight against Boko Haram, Mr Trump confirmed the sale of a dozen US light attack aircraft, worth a reported $496 million, that his predecessor Barack Obama had frozen after the Nigerian military bombed a refugee camp.

Mr Trump urged Mr Buhari to remove trade barriers and invite US investors into Nigeria, dangling America’s $1 billion in annual aid over his counterpart, in accordance with his vow to secure better trade deals for the US.

Meanwhile Mr Buhari was able to court investors for Nigeria’s agriculture, aviation and transport sectors. A team of officials reportedly travelled to the US beforehand to sign a $2 billion railway revamp agreement with a consortium led by General Electric.

After 15 months in office, Mr Trump may have made few friends in Africa. But Nigeria’s president, under enormous strain at home, is as close as they come.