Reorganized some codes, try to use thread for api calling