Initial version of blog Gen
This commit is contained in:
21
lib/wordpress_api_integration/V1/main.py
Normal file
21
lib/wordpress_api_integration/V1/main.py
Normal file
@@ -0,0 +1,21 @@
|
||||
## main.py
|
||||
|
||||
from wordpress_api import WordpressAPI
|
||||
|
||||
|
||||
def main():
|
||||
"""
|
||||
Main entry point of the program.
|
||||
"""
|
||||
# Create WordpressAPI instance
|
||||
wp_api = WordpressAPI(base_url="https://example.com", username="admin", password="password")
|
||||
|
||||
# Authenticate
|
||||
wp_api.authenticate()
|
||||
|
||||
# Upload content
|
||||
content = "This is a test content"
|
||||
wp_api.upload_content(content)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
30
lib/wordpress_api_integration/V1/test_wordpress_api.py
Normal file
30
lib/wordpress_api_integration/V1/test_wordpress_api.py
Normal file
@@ -0,0 +1,30 @@
|
||||
## test_wordpress_api.py
|
||||
|
||||
import pytest
|
||||
from wordpress_api import WordpressAPI
|
||||
|
||||
|
||||
class TestWordpressAPI:
|
||||
@pytest.fixture
|
||||
def wp_api(self):
|
||||
return WordpressAPI(base_url="https://example.com", username="admin", password="password")
|
||||
|
||||
def test_authenticate_success(self, wp_api):
|
||||
wp_api.authenticate()
|
||||
assert wp_api.authentication.token is not None
|
||||
|
||||
def test_authenticate_failure(self, wp_api):
|
||||
wp_api.authentication.password = "wrong_password"
|
||||
with pytest.raises(Exception):
|
||||
wp_api.authenticate()
|
||||
|
||||
def test_upload_content_success(self, wp_api):
|
||||
content = "This is a test content"
|
||||
wp_api.upload_content(content)
|
||||
# Add assertions here to verify the success of content upload
|
||||
|
||||
def test_upload_content_failure(self, wp_api):
|
||||
content = "This is a test content"
|
||||
wp_api.content_upload.base_url = "https://wrong_url.com"
|
||||
with pytest.raises(Exception):
|
||||
wp_api.upload_content(content)
|
||||
75
lib/wordpress_api_integration/V1/wordpress_api.py
Normal file
75
lib/wordpress_api_integration/V1/wordpress_api.py
Normal file
@@ -0,0 +1,75 @@
|
||||
## wordpress_api.py
|
||||
|
||||
import requests
|
||||
import json
|
||||
|
||||
class Authentication:
|
||||
def __init__(self, base_url, username, password):
|
||||
self.base_url = base_url
|
||||
self.username = username
|
||||
self.password = password
|
||||
self.token = None
|
||||
|
||||
def authenticate(self):
|
||||
"""
|
||||
Authenticates the user with the Wordpress API.
|
||||
"""
|
||||
url = f"{self.base_url}/authenticate"
|
||||
payload = {
|
||||
"username": self.username,
|
||||
"password": self.password
|
||||
}
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
response = requests.post(url, json=payload, headers=headers)
|
||||
|
||||
if response.status_code == 200:
|
||||
self.token = response.json()["token"]
|
||||
else:
|
||||
raise Exception("Authentication failed")
|
||||
|
||||
|
||||
class ContentUpload:
|
||||
def __init__(self, base_url, token):
|
||||
self.base_url = base_url
|
||||
self.token = token
|
||||
|
||||
def upload_content(self, content):
|
||||
"""
|
||||
Uploads the given content to the Wordpress API.
|
||||
"""
|
||||
url = f"{self.base_url}/upload"
|
||||
payload = {
|
||||
"content": content,
|
||||
"token": self.token
|
||||
}
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
response = requests.post(url, json=payload, headers=headers)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception("Content upload failed")
|
||||
|
||||
class WordpressAPI:
|
||||
def __init__(self, base_url, username, password):
|
||||
self.base_url = base_url
|
||||
self.username = username
|
||||
self.password = password
|
||||
self.authentication = Authentication(base_url, username, password)
|
||||
self.content_upload = ContentUpload(base_url, self.authentication.token)
|
||||
|
||||
def authenticate(self):
|
||||
"""
|
||||
Authenticates the user with the Wordpress API.
|
||||
"""
|
||||
self.authentication.authenticate()
|
||||
|
||||
def upload_content(self, content):
|
||||
"""
|
||||
Uploads the given content to the Wordpress API.
|
||||
"""
|
||||
self.content_upload.upload_content(content)
|
||||
Reference in New Issue
Block a user