apiv2: refactor common code into api module