Validate the namespace_id part of API URL paths.

This prevents a StatementError from being raised if you try to do something
like /n/bad%20id/threads, where the namespace_id part can't be decoded to an
integer.

Fixes T321.