8000 Initial WooCommerce class methods · factorlibre/wc-api-python@dd156ca · GitHub
[go: up one dir, main page]

Skip to content

Commit dd156ca

Browse files
Initial WooCommerce class methods
1 parent 9e17c6a commit dd156ca

File tree

7 files changed

+105
-25
lines changed

7 files changed

+105
-25
lines changed

requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-r requirements.txt

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
oauthlib==0.7.2
2+
requests==2.7.0

setup.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@
88

99

1010
# Get version from __init__.py file
11-
VERSION = ''
12-
with open('woocommerce-api/__init__.py', 'r') as fd:
13-
VERSION = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', fd.read(), re.MULTILINE).group(1)
11+
VERSION = ""
12+
with open("woocommerce/__init__.py", "r") as fd:
13+
VERSION = re.search(r"^__version__\s*=\s*['\"]([^\"]*)['\"]", fd.read(), re.MULTILINE).group(1)
1414

1515
if not VERSION:
16-
raise RuntimeError('Cannot find version information')
16+
raise RuntimeError("Cannot find version information")
1717

1818
# Get long description
19-
README = open(os.path.join(os.path.dirname(__file__), 'README.md')).read()
19+
README = open(os.path.join(os.path.dirname(__file__), "README.md")).read()
2020

2121
# allow setup.py to be run from any path
2222
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
2323

2424
setup(
25-
name='woocommerce-api',
25+
name="woocommerce",
2626
version=VERSION,
2727
description="A Python wrapper for the WooCommerce REST API",
2828
long_description=README,
2929
author="Claudio Sanches @ WooThemes",
30-
url='http://www.woothemes.com',
30+
url="http://www.woothemes.com",
3131
license="MIT",
32-
packages=['woocommerce-api'],
32+
packages=["woocommerce"],
3333
include_package_data=True,
3434
install_requires=[
3535
"requests",

test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
""" Testing """
5+
import woocommerce
6+
7+
8+
WC = woocommerce.WooCommerce(
9+
url="http://test.woo.com/",
10+
consumer_key="ck_38d5d5d8e55936cb7d67ad00492aa96b47fd325f",
11+
consumer_secret="cs_0c13a7221ecbceee1715879d9349fa52cec20b93"
12+
)
13+
14+
print WC.get("customers")

woocommerce-api/__init__.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

woocommerce-api/__main__.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

woocommerce/__init__.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
woocommerce-api
5+
~~~~~~~~~~~~~~~
6+
A Python wrapper for WooCommerce API.
7+
8+
:copyright: (c) 2015 by WooThemes.
9+
:license: MIT, see LICENSE for details.
10+
"""
11+
12+
__title__ = "woocommerce-api"
13+
__version__ = "1.0.0"
14+
__author__ = "Claudio Sanches @ WooThemes"
15+
__license__ = "MIT"
16+
17+
import re
18+
import requests
19+
from oauthlib.oauth1 import Client as oAuth1
20+
21+
22+
class WooCommerce(object):
23+
""" API Class """
24+
def __init__(self, url, consumer_key, consumer_secret, **kwargs):
25+
self.url = url
26+
self.consumer_key = consumer_key
27+
self.consumer_secret = consumer_secret
28+
self.version = kwargs.get("version", "v3")
29+
self.is_ssl = self.__is_ssl()
30+
self.verify_ssl = kwargs.get("verify_ssl", True)
31+
32+
def __is_ssl(self):
33+
""" Check if url use HTTPS """
34+
return re.match(r"^https", self.url) is not None
35+
36+
def __get_url(self, endpoint):
37+
""" Get URL for requests """
38+
url = self.url
39+
40+
if re.match(r".*(/)$", self.url) is None:
41+
url += "/"
42+
43+
return url + 'wc-api/' + self.version + '/' + endpoint
44+
45+
def __request(self, method, endpoint, data):
46+
""" Do requests """
47+
url = self.__get_url(endpoint)
48+
49+
if self.is_ssl is True:
50+
auth = (self.consumer_key, self.consumer_secret)
51+
else:
52+
auth = oAuth1(
53+
client_key=self.consumer_key,
54+
client_secret=self.consumer_secret
55+
).sign(url, method)
56+
print auth
57+
58+
return requests.request(
59+
method=method,
60+
url=url,
61+
verify=self.verify_ssl,
62+
auth=auth,
63+
data=data
64+
).json()
65+
66+
def get(self, endpoint):
67+
""" Get requests """
68+
return self.__request("GET", endpoint, None)
69+
70+
def post(self, endpoint, data):
71+
""" POST requests """
72+
return self.__request("POST", endpoint, data)
73+
74+
def put(self, endpoint, data):
75+
""" PUT requests """
76+
return self.__request("PUT", endpoint, data)
77+
78+
def delete(self, endpoint):
79+
""" DELETE requests """
80+
return self.__request("DELETE", endpoint, None)

0 commit comments

Comments
 (0)
0