Skip to content

Add support to SafeTransport class #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions wordpress_xmlrpc/base.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import collections
import sys
import urllib.parse

from wordpress_xmlrpc.compat import xmlrpc_client, dict_type
from wordpress_xmlrpc.exceptions import ServerConnectionError, UnsupportedXmlrpcMethodError, InvalidCredentialsError, XmlrpcDisabledError


class Client(object):

"""
Connection to a WordPress XML-RPC API endpoint.

To execute XML-RPC methods, pass an instance of an
`XmlrpcMethod`-derived class to `Client`'s `call` method.
"""

def __init__(self, url, username, password, blog_id=0, transport=None, verbose=False):
def __init__(self, url, username, password, blog_id=0, transport=None, verbose=False, safe_transport=None):
self.url = url
self.username = username
self.password = password
self.blog_id = blog_id

self.setup(transport, verbose)

self.setup(transport, verbose, safe_transport)

def setup(self, transport, verbose):
def setup(self, transport, verbose, safe_transport):
try:
self.server = xmlrpc_client.ServerProxy(self.url, allow_none=True, transport=transport,
verbose=verbose)
verbose=verbose)
self.supported_methods = self.server.mt.supportedMethods()
except xmlrpc_client.ProtocolError:
e = sys.exc_info()[1]
Expand All @@ -34,7 +35,12 @@ def setup(self, transport, verbose):
self.url = e.headers['location']
except KeyError:
self.url = e.headers['Location']
self.setup(transport, verbose)

protocol, _ = urllib.parse.splittype(self.url)
if protocol == 'https':
transport = safe_transport

self.setup(transport, verbose, None)
else:
raise ServerConnectionError(repr(e))

Expand All @@ -59,6 +65,7 @@ def call(self, method):


class XmlrpcMethod(object):

"""
Base class for XML-RPC methods.

Expand All @@ -80,10 +87,12 @@ def __init__(self, *args, **kwargs):
if self.optional_args:
max_num_args = len(self.method_args) + len(self.optional_args)
if not (len(self.method_args) <= len(args) <= max_num_args):
raise ValueError("Invalid number of parameters to %s" % self.method_name)
raise ValueError(
"Invalid number of parameters to %s" % self.method_name)
else:
if len(args) != len(self.method_args):
raise ValueError("Invalid number of parameters to %s" % self.method_name)
raise ValueError(
"Invalid number of parameters to %s" % self.method_name)

for i, arg_name in enumerate(self.method_args):
setattr(self, arg_name, args[i])
Expand Down Expand Up @@ -144,13 +153,15 @@ def process_result(self, raw_result):


class AnonymousMethod(XmlrpcMethod):

"""
An XML-RPC method for which no authentication is required.
"""
pass


class AuthenticatedMethod(XmlrpcMethod):

"""
An XML-RPC method for which user authentication is required.

Expand Down