I hope all you guys be fine.
I was trying to modify multiple products in Woocommerce with python(Restful Api WooCommerce)
here is the code I used:
import sys
import json
from woocommerce import API
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
wcapi = API(
url="https://...",
consumer_key="ck_...",
consumer_secret="cs_...",
wp_api = True,
version="wc/v3",
query_string_auth=True # Force Basic Authentication as query string true and using under HTTPS
)
data = {"id":"1995","regular_price":"1775000","sale_price":"1685000","stock_quantity":"6"},
{"id":"4673","regular_price":"2300000","sale_price":"2045000","stock_quantity":"15"}
print(wcapi.put("products/bulk",data).json())
but when I run the code, I got below error:
{'code': 'rest_no_route', 'message': 'No route was found matching the URL and request method', 'data': {'status': 404}}
product IDs are available.
before trying to make some changes on bulk, I was able to modify a single product with the below code:
print(wcapi.put("products/1995",data).json())
my current Woocommerce version 3.9 - & - current python version 3.8.1
i was not able to find the solution or why is that happening. how to fix it?
also how can i modify multiple variable Woocommerce products?
Thanks
finally, the problem got solved.
I just wanted to post it here for the future use of others who may face such this problem.
here is the change which I made in the code:
data = {
"update":
[
{
"id":"1995",
"regular_price":"1775000",
"sale_price":"1685000",
"stock_quantity":"6"},
{
"id":"4673",
"regular_price":"2300000",
"sale_price":"2045000",
"stock_quantity":"15"}
]
}
# Bulk edit of Woocommerce products - here we have 2 sample products
print(wcapi.put("products/batch",data).json())
if you have a better way or think should notice any other thing for working with woocommerce in python, happy to have your post below in this post:-)
Related
I want to see the 'user(specifically, user's display_name') data using Stackoverflow's API.
I'm using and reading the docs about StackExchange API, and still didn't get the idea about 'fetch' and anything about get the data.
Using 'beautifulsoup' or any crawling code, is it the only way to get the data?
from stackapi import StackAPI
SITE = StackAPI('stackoverflow')
users = SITE.fetch('users')
print(users['items'])
Following the documentation for the StackAPI python library, the method skeleton is fetch(endpoint=None, page=1, key=None, filter='default', **kwargs).
So your api call will look something like:
from stackapi import StackAPI
SITE = StackAPI('stackoverflow')
SITE.page_size = 5 # limits to 5 returned results for demo purpose
users = SITE.fetch('users', filter='!)mYVom7)TA9')
print(users['items'])
where the filter code was obtained from the get users docs for the api.
Returns:
[{'display_name': 'Jon Skeet'}, {'display_name': 'Gordon Linoff'}, {'display_name': 'VonC'}, {'display_name': 'BalusC'}, {'display_name': 'Darin Dimitrov'}]
I want to get a one product information by Id in wcapi(Module page).I use this code But dont work :/ and get all product information
from woocommerce import API
wcapi = API()
response = wcapi.get('products',params={'id':776}).json()
As other mentioned the API works as below:
/wp-json/wc/v3/products/<id>
So use the API as mentioned in the documentation and your code should be like below:
from woocommerce import API
wcapi = API()
response = wcapi.get("products/794").json()
Read more about the output here.
It's always a good idea to stick with documentation.
I am not able to understand how to fetch the status of the pull request via Python jira API.
I have gone through https://jira.readthedocs.io/en/latest/examples.html,
and searched the internet for it. But I was not able to link the jira issue with the pull request, I saw that the pull request is linked to jira issue id, but was not able to understand how to implement it.
I am using python 3.7
from jira import JIRA
issue = auth_jira.issue('XYZ-000')
pull_request = issue.id.pullrequest
I am getting this error:
AttributeError: 'str' object has no attribute 'pullrequest'
I am not sure how to access pullrequest data in jira.
Any leads would help.
I did something similar with another python wrapper for the jira-API: atlassian-python-api.
Look if it works in your case:
from atlassian import Jira
from pprint import pprint
import json
jira = Jira(
url='https://your.jira.url',
username=user,
password=pwd)
issue = jira.get_issue(issue_key)
# get the custom field ref of the "Development" field (I don't know if it's always the same):
dev_field_string = issue["fields"]["customfield_13900"]
# the value of this field is a huge string containing a json, that we must parse ourselves:
json_str = dev_field_string.split("devSummaryJson=")[1][:-1]
# we load it with the json module (this ensures json is converted as dict, i.e. 'true' is interpreted as 'True'...)
devSummaryJson = json.loads(json_str)
# the value of interest are under cachedValue/summary:
dev_field_dic = devSummaryJson["cachedValue"]["summary"]
pprint(dev_field_dic)
# you can now access the status of your pull requests (actually only the last one):
print(dev_field_dic['pullrequest']['overall']['state'])
I'm working on a Rails 4 / mongoid application which needs to expose APIs for other applications and scripts. I need to be able to update documents in one of the models through an API with Python 3 script. I'm a bit fresh with Python hence asking for help here.
I already found out how to query Rails APIs with Python 3 and urllib but struggling with updates. I was trying to go through Python 3.5 docs for urllib2 but struggling to apply this to my script.
What goes to data and how to add authentication token to headers, which in curl would look something like this
-H 'Authorization:Token token="xxxxxxxxxxxxxxxx"'
-X POST -d "name=A60E001&status=changed"
I would greatly appreciate if somebody explained how to, for example, update status based on name (name is not unique yet but will be). My biggest challenge is the Python side. Once I have the data in params on Rails side I think I can handle it. I think.
I included my model and update action from the controller below.
app/models/experiment.rb
class Experiment
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :status, type:String
end
app/controllers/api/v1/experiments_controller.rb
module Api
module V1
class ExperimentsController < ActionController::Base
before_filter :restrict_access
...
def update
respond_to do |format|
if #expt_proc.update(expt_proc_params)
format.json { render :show, status: :ok, location: #expt_proc }
else
format.json { render json: #expt_proc.errors, status: :unprocessable_entity }
end
end
end
...
private
def restrict_access
authenticate_or_request_with_http_token do |token, options|
ApiKey.where(access_token: token).exists?
end
end
...
I figured out who to send a PATCH request with Python 3 and update the the record's status by name.
Thanks to this post found out about requests module. I used requests.patch to update the record and it works great.
python code
import requests
import json
url = 'http://0.0.0.0:3000/api/v1/update_expt_queue.json'
payload = {'expt_name' : 'myExperiment1', 'status' : 'finished' }
r = requests.patch(url, payload)
There are two problems remaining:
How to add headers to this request which would allow me to go through token based authentication. At the moment it's disabled. request.patch only takes 2 parameters, it doesn't take headers parameter.
How to access the JSON which is sent in response by Rails API.
I ran for awhile a python script to post articles on my blogspot blog.
everything ran smoothly until I started to get this auth error
RequestError: {'status': 401, 'body': 'User does not have permission to create new post', 'reason': 'Unauthorized'}
I really can't understand how to fix it reading gdata documentation.
Could you please suggest me how to do?
Thank you
Here the part of my code that doesn't work anymore:
from gdata import service
import gdata
import atom
blogger_service = service.GDataService('xxxxxx','xxxxxx')
blogger_service.service = 'blogger'
blogger_service.account_type = 'GOOGLE'
blogger_service.server = 'www.blogger.com'
blogger_service.ProgrammaticLogin()
def CreatePublicPost(blogger_service, blog_id, title, content,tags):
entry = gdata.GDataEntry()
entry.title = atom.Title('xhtml', title)
entry.content = atom.Content(content_type='html', text=content)
for tag in tags :
category = atom.Category(term=tag, scheme="http://www.blogger.com/atom/ns#")
entry.category.append(category)
return blogger_service.Post(entry, '/feeds/%s/posts/default' % blog_id)
Now there is a API version 3.0...
This old version is obsolete and no longer works, apparently...
You can find more about it here:
https://developers.google.com/blogger/
https://developers.google.com/blogger/docs/3.0/using/
And if you have questions about authentication, maybe those links help:
Having trouble trying to use gdata and oauth2 in python
Authentication with the Google Docs List API, Python and OAuth 2