def create_posts(token: str): number_posts_per_user = random.randint(1, config.MAX_POSTS_PER_USER) try: for i in range(0, number_posts_per_user): title = fake.sentence() text = fake.text() payload = json.dumps({'title': title, 'author': None, 'text': text}) request(resource='posts/', method="POST", data=payload, token=token) logger.info('New post was created: %s' % title) except Exception: logger.error('There is some problem with post sending')
def like_post(token: str): number_likes_per_user = random.randint(1, config.MAX_LIKES_PER_USER) try: posts = request(resource='posts', method="GET") post_list = [post["id"] for post in json.loads(posts.text)["results"]] like = 1 for i in range(0, number_likes_per_user): post_id = random.choice(post_list) payload = json.dumps({'post': post_id, 'like': like}) request(resource='likes/', method="POST", data=payload, token=token) logger.info('User like post %s' % post_id) except Exception: logger.warning('There is some problem with post sending')
def get_token(username: str, password: str): payload = json.dumps({'username': username, 'password': password}) result = request(resource='api/token/', method="POST", data=payload) logger.debug('Token was generated for user: %s' % username) if result.status_code == 200: return json.loads(result.text)['access'] else: logger.error('There is some problem with token creating') raise Exception
def test_custom_request(): headers = {"Content-Type": "application/json"} test_issue = "https://api.github.com/repos/rucio/rucio/issues/5" response = utils.request(test_issue, headers) assert response["number"] == 5 assert response["title"] == "This is a test issue" assert response["body"] == "test!" assert response["created_at"] == "2017-11-07T13:03:03Z" assert response["assignees"][0]["login"] == "bari12" assert type(response) == dict
def register_user(): username = fake.first_name() password = fake.password() email = fake.email() payload = json.dumps({'username': username, 'email': email, 'password': password}) result = request(resource='users/account/register', method="POST", data=payload) logger.info('New user was registered. Username: %s' % username) if result.status_code == 201: return [username, password] else: logger.error('There is some problem with user signup!') raise Exception
def _check_token(self): """Check the GitHub token's validity.""" try: ##TODO improve hacky approach below # if the request is correct then no message is returned and we have a TypeError if (utils.request(self.docs_url, self.headers)["message"] == "Bad credentials"): raise InvalidTokenError( f"\nError: Bad credentials. The OAUTH token {self.api_token} is not correct." ) except InvalidTokenError as _e: sys.exit(_e) except: # we don't care about the TypeError pass
def fetch(self, api_token): """ Returns a pandas DataFrames that holds information for Rucio's documentation, utilizing GitHub's api. attributes: doc_id : doc's id name : doc's name/title url : doc's url body : doc's creator doc_type : 'general', 'daemon' or 'release_notes' :param api_token : GitHub api token used for fetching the data :return docs_df : DataFrame containing all the information for Rucio's docs """ self.api_token = api_token self.headers = { "Content-Type": "application/json", "Authorization": f"token {self.api_token}", } self._check_token() docs_df = pd.DataFrame( columns=["doc_id", "name", "url", "body", "doc_type"]) doc_id = 0 print("Fetching Rucio documentation...") for doc in tqdm(utils.request(self.docs_url, self.headers)): if type(doc) == str: print( f"Error: Problem fetching the doc {doc} moving on to the next..." ) continue elif doc["type"] == "file": if doc["name"].split(".")[-1] not in ["rst", "md"]: continue doc_name = doc["name"] doc_url = doc["html_url"] doc_download_url = doc["download_url"] doc_body = requests.get(doc_download_url).content.decode( "utf-8") docs_df = docs_df.append( { "doc_id": doc_id, "name": doc_name, "url": doc_url, "body": doc_body, "doc_type": "general", }, ignore_index=True, ) doc_id += 1 elif doc["type"] == "dir": if doc["name"] == "images": pass # daemon documentation exists under the man directory elif doc["name"] == "man": print("\nFetching the daemon documentation...") man_url = doc["url"] try: daemons_url = (self.root_download_url + "/doc/source/man/daemons.rst") daemon_body = requests.get(daemons_url).content.decode( "utf-8") # regex used to extract daemon names from body daemons = re.findall("rucio-.*$", daemon_body, re.MULTILINE) except: raise AssertionError( "There is a problem with the daemons_url. Double check if it has changed" ) for man_doc in utils.request(man_url, self.headers): if type(man_doc) == str: print( f"Error : There was a problem fetching the file : {man_doc}" ) continue elif man_doc["name"].split(".")[-1] not in [ "rst", "md" ]: continue else: # make sure that we are looking at daemon documentation if man_doc["name"].split(".")[0] in daemons: doc_name = man_doc["name"] doc_url = man_doc["html_url"] doc_download_url = man_doc["download_url"] # In Rucio daemons the doc_body usually points to the docsting documentation doc_body = requests.get( doc_download_url).content.decode("utf-8") # We need additional handling to get it, done with _extract_daemon_body method final_doc_body = self._extract_daemon_body( doc_body) docs_df = docs_df.append( { "doc_id": doc_id, "name": doc_name, "url": doc_url, "body": final_doc_body, "doc_type": "daemon", }, ignore_index=True, ) doc_id += 1 elif doc["name"] == "releasenotes": print("\nFetching the release notes...") release_notes_url = doc["url"] for release_note in utils.request(release_notes_url, self.headers): if type(release_note) == str: print( f"Error: Problem fetching the release note {release_note}" ) continue elif release_note["name"].split(".")[-1] not in [ "rst", "md" ]: continue else: doc_name = release_note["name"] doc_url = release_note["html_url"] doc_download_url = release_note["download_url"] doc_body = requests.get( doc_download_url).content.decode("utf-8") docs_df = docs_df.append( { "doc_id": doc_id, "name": doc_name, "url": doc_url, "body": doc_body, "doc_type": "release_notes", }, ignore_index=True, ) doc_id += 1 ##TODO handle restapi, api # Below are complicated for now, if we want to integrate we can # download and compile with Sphinx the Makefile etc # restapi documentation elif doc["name"] == "restapi": pass # api documentation elif doc["name"] == "api": pass self.docs = docs_df return docs_df