Initial version of blog Gen

This commit is contained in:
AjaySi
2023-10-07 10:51:22 +05:30
commit 12010c0478
27 changed files with 1295 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
## main.py
import os
import requests
import json
class WordPressAPIIntegration:
def __init__(self, credentials: dict):
self.credentials = credentials
def upload_file(self, file_path: str) -> bool:
if not self._check_file(file_path):
return False
if not self._authenticate():
return False
if not self._upload_file_to_api(file_path):
return False
return True
def _check_file(self, file_path: str) -> bool:
max_file_size = 10 * 1024 * 1024 # 10MB
file_size = os.path.getsize(file_path)
if file_size > max_file_size:
return False
valid_file_types = ['.jpg', '.jpeg', '.png', '.gif']
file_extension = os.path.splitext(file_path)[1]
if file_extension not in valid_file_types:
return False
return True
def _authenticate(self) -> bool:
url = "https://wordpress-api.com/authenticate"
headers = {'Content-Type': 'application/json'}
data = json.dumps(self.credentials)
response = requests.post(url, headers=headers, data=data)
if response.status_code == 200:
return True
return False
def _upload_file_to_api(self, file_path: str) -> bool:
url = "https://wordpress-api.com/upload"
files = {'file': open(file_path, 'rb')}
response = requests.post(url, files=files)
if response.status_code == 200:
return True
return False

View File

@@ -0,0 +1,86 @@
## test_wordpress_api_integration.py
import os
import pytest
from wordpress_api_integration import WordPressAPIIntegration
class TestWordPressAPIIntegration:
@pytest.fixture
def credentials(self):
return {
"username": "test_user",
"password": "test_password"
}
@pytest.fixture
def valid_file_path(self):
return "path/to/valid/file.jpg"
@pytest.fixture
def invalid_file_path(self):
return "path/to/invalid/file.txt"
def test_upload_file_valid_file(self, credentials, valid_file_path, monkeypatch):
def mock_check_file(file_path):
return True
def mock_authenticate():
return True
def mock_upload_file_to_api(file_path):
return True
monkeypatch.setattr(WordPressAPIIntegration, "_check_file", mock_check_file)
monkeypatch.setattr(WordPressAPIIntegration, "_authenticate", mock_authenticate)
monkeypatch.setattr(WordPressAPIIntegration, "_upload_file_to_api", mock_upload_file_to_api)
api_integration = WordPressAPIIntegration(credentials)
result = api_integration.upload_file(valid_file_path)
assert result is True
def test_upload_file_invalid_file(self, credentials, invalid_file_path, monkeypatch):
def mock_check_file(file_path):
return False
monkeypatch.setattr(WordPressAPIIntegration, "_check_file", mock_check_file)
api_integration = WordPressAPIIntegration(credentials)
result = api_integration.upload_file(invalid_file_path)
assert result is False
def test_upload_file_authentication_failed(self, credentials, valid_file_path, monkeypatch):
def mock_check_file(file_path):
return True
def mock_authenticate():
return False
monkeypatch.setattr(WordPressAPIIntegration, "_check_file", mock_check_file)
monkeypatch.setattr(WordPressAPIIntegration, "_authenticate", mock_authenticate)
api_integration = WordPressAPIIntegration(credentials)
result = api_integration.upload_file(valid_file_path)
assert result is False
def test_upload_file_upload_failed(self, credentials, valid_file_path, monkeypatch):
def mock_check_file(file_path):
return True
def mock_authenticate():
return True
def mock_upload_file_to_api(file_path):
return False
monkeypatch.setattr(WordPressAPIIntegration, "_check_file", mock_check_file)
monkeypatch.setattr(WordPressAPIIntegration, "_authenticate", mock_authenticate)
monkeypatch.setattr(WordPressAPIIntegration, "_upload_file_to_api", mock_upload_file_to_api)
api_integration = WordPressAPIIntegration(credentials)
result = api_integration.upload_file(valid_file_path)
assert result is False

View File

@@ -0,0 +1,54 @@
## wordpress_api_integration.py
import os
import requests
import json
class WordPressAPIIntegration:
def __init__(self, credentials: dict):
self.credentials = credentials
def upload_file(self, file_path: str) -> bool:
if not self._check_file(file_path):
return False
if not self._authenticate():
return False
if not self._upload_file_to_api(file_path):
return False
return True
def _check_file(self, file_path: str) -> bool:
max_file_size = 10 * 1024 * 1024 # 10MB
file_size = os.path.getsize(file_path)
if file_size > max_file_size:
return False
valid_file_types = ['.jpg', '.jpeg', '.png', '.gif']
file_extension = os.path.splitext(file_path)[1]
if file_extension not in valid_file_types:
return False
return True
def _authenticate(self) -> bool:
url = "https://wordpress-api.com/authenticate"
headers = {'Content-Type': 'application/json'}
data = json.dumps(self.credentials)
response = requests.post(url, headers=headers, data=data)
if response.status_code == 200:
return True
return False
def _upload_file_to_api(self, file_path: str) -> bool:
url = "https://wordpress-api.com/upload"
files = {'file': open(file_path, 'rb')}
response = requests.post(url, files=files)
if response.status_code == 200:
return True
return False