Integrating $\frac1{a+b\cos(x)}$ using $e^{ix}$ In calculus class our teacher demonstrated us the evaluation of the definite integral $\int_0^\pi \dfrac{1}{a+b\cos(x)}dx=\dfrac{\pi}{\sqrt{a^2-b^2}}$, for $a\ne 0, b \ne 0, |\dfrac ba \lt 1|$, but there's a part I could not grasp.
Our teacher started out by setting up an equality: $bz^2+2az+b=b(z-\alpha)(z-\beta)$, where $\alpha, \beta$ are the roots of the expression on the LHS in $\Bbb C$, and $\alpha$ takes on the value with a negative sign before the radical, while $\beta$ is the other one. Letting $z=e^{ix}$, the integrand is transformed into the following form:
$\dfrac{1}{\sqrt{a^2-b^2}}\left(\dfrac{\alpha}{\alpha-e^{ix}}-\dfrac{\beta}{e^{ix}-\beta}\right)=\dfrac{1}{\sqrt{a^2-b^2}}\left(\dfrac{1}{\alpha e^{-ix}-1}+\dfrac{1}{1-\beta e^{-ix}}\right)$
$=\dfrac{1}{\sqrt{a^2-b^2}}\sum_{n=0}^{\infty}(\beta^n-\alpha^n)e^{-inx}=\dfrac{1}{\sqrt{a^2-b^2}}\sum_{n=1}^{\infty}(\beta^n-\alpha^n)(\cos(nx)-i\sin(nx))$
But then $\int_0^\pi \cos(nx)-i\sin(nx) dx=\dfrac 1n(\sin(nx)+i\cos(nx))|_0^\pi=\dfrac in(\cos(n\pi)-1)$ and I was completely lost on how the next part was carried out. For if we substitute that back into the infinite sum, we get that integrated version of that expression would be $\dfrac{1}{\sqrt{a^2-b^2}}\sum_{n=1}^{\infty}(\beta^n-\alpha^n)\dfrac in(\cos(n\pi)-1)$
$=\dfrac{1}{\sqrt{a^2-b^2}}\sum_{k=1}^{\infty}(\beta^{2k-1}-\alpha^{2k-1})\dfrac {i}{2k-1}(\cos((2k-1)\pi)-1)$
$=\dfrac{1}{\sqrt{a^2-b^2}}\sum_{k=1}^{\infty}(\beta^{2k-1}-\alpha^{2k-1})\dfrac {-2i}{2k-1}$
Yet I fail to see how this could lead to the supposed final result, especially with that $i$ there.

A simpler method:
Let us recall the fact that when $A, B, C\in{\mathbb R}$ and $\Delta=B^2-4 A C < 0$ then
\begin{equation}
\int \frac{d t}{A t^2 + B t + C} = \frac{2}{\sqrt{-\Delta}} \arctan\left(\frac{2 A t + B}{\sqrt{- \Delta}}\right)
\end{equation}
The substitution $t = \tan(x/2)$ in the original integral gives
\begin{equation}
I = \int_0^\pi\frac{1}{a + b \cos(x)} d x= \int_0^{+\infty}\frac{1}{a + b\frac{1-t^2}{1+t^2}}\times \frac{2}{1 + t^2} dt
=\int_0^{+\infty}\frac{2}{(a-b)t^2+(a+b)} d t
\end{equation}
Here $\Delta = -4 (a-b)(a+b) = -4(a^2-b^2)<0$, hence
\begin{equation}
I = \left[ \frac{2}{\sqrt{a^2-b^2}}\arctan\left(\frac{(a-b)t}{\sqrt{a^2-b^2}}\right)\right]_0^{+\infty} = \frac{\pi}{\sqrt{a^2-b^2}}
\end{equation}
