#1 2015-12-29 06:15:36

EvaF
Member
Registered: 2014-07-19
Posts: 40

A mORMot Wordpress REST API (and WooCommerce API) consumer

Hi,
for my project that uses webservice (based on mORMot) I will have to create users in Wordpress with the same account ( username,pwd,email, etc)  and maybe later to reading/writing WooCommerce data. Therefore  I created a small library to accomplish it.
I tried to use ORM classes for the work with Wordpress records.

The reading looks like this

type 
TWPRESTAPI1_0_3LEGS = class(TWPRESTAPI)       // my class for dealing with Wordpress REST API include providing authentication
...
TSQLWPClient = class(TSQLHttpClientWinHTTP)
...
TSQLRESTAPIUser = class(TSQLRecord)
  protected
    fusername: RawUTF8;
    fname: RawUTF8;
    ffirst_name: RawUTF8;
    fnickname: RawUTF8;
    fslug: RawUTF8;
...

var
     fWP: TWPRESTAPI1_0_3LEGS ;  
     fWPClient : TSQLWPClient;
     fWPUser : TSQLRESTAPIUser;

procedure ReadWPUsers;
begin    
    fWP := TWPRESTAPI1_0_3LEGS.create('WPRestAPI.json'); // WPRestAPI.json - the file containing TSQLRecord that stores Wordpress server, consumer key, secret consumer key etc. 
    fWP.Connect;

    fWPClient := TSQLWPClient(fWP.getWAPIClient('users', '{"search":"*eva*","orderby":"username}'));   //'users' is route of WP REST API and '{"search":...'  is filter
 
    fWPUser := TSQLRESTAPIUser.CreateAndFillPrepare(fWPClient,'',[]);
end;

the updating like this:

procedure UpdateWPUser;
begin
  if assigned( fWPUser) then
  begin
    fWPUser.FillRewind;
    while fWPUser.FillOne do
    begin
      if fWPUser.username=EditWPUsername.text then
      begin
        fWPUser.description := EditWPdescription.text;
        fWPClient.Update(fWPUser);
      end;
    end ;
  end;
end;

I had to only  take care of the authentication of the requests, everything else was pretty simple thanks mormot.

Offline

#2 2015-12-30 21:35:55

EvaF
Member
Registered: 2014-07-19
Posts: 40

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

hereby is source https://github.com/Eva-F/mormot-WP-REST-API
maybe it helps somebody

a little remark:
I am not good in the writing of commentaries neither in my native language let alone in English.
I know only a small fraction of mormot, so some part of my code could use routines of mormot better and more effectively

Offline

#3 2015-12-31 04:20:07

edwinsn
Member
Registered: 2010-07-02
Posts: 1,176

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

Thanks for sharing!


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#4 2015-12-31 18:05:59

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,170
Website

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

Thanks for sharing.

BTW, isn't CopyVarRec() leaking memory, when processing vtPWideChar, vtUnicodeString?

Offline

#5 2015-12-31 18:32:29

EvaF
Member
Registered: 2014-07-19
Posts: 40

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

Thanks for the warning, I will take look into - in this case I think that in SynCommons is a suitable subroutine for this purpose, but I could not find it

Offline

#6 2016-01-01 07:30:28

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,170
Website

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

I did not understand the purpose of this function, and its use case...

Offline

#7 2016-01-01 15:39:34

EvaF
Member
Registered: 2014-07-19
Posts: 40

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

ab wrote:

I did not understand the purpose of this function, and its use case...

I will try to explain my idea:
I have intended  to call the CallbackGet function that uses open array of TVarRec as a parameter.

function TSQLRestClientURI.CallBackGet(const aMethodName: RawUTF8;
  const aNameValueParameters: array of const; ...): integer;
 

Rather then explicitly enumerate  all  name, value pairs I would prefer to add them gradually one by one based on the actual authentication stage. Therefore  each NameValue pair is copied into a FParameters array

    fParameters: array of TVarRec;

and afterwards passed as an aNameValueParameters.

fOauthClient.CallBackGet('request', fParameters, lResponse)

I have fixed the unsatisfactory releasing of fParameters and there are no memory leaks now.

Further I have discovered a bug in OAuth1 plugin and when I have checked it on Github, I have found out that there is a new version of Oauth1 plugin available and that this new version changed the way the signature is created at the third leg of authetication. I have also added  the support for OAuth1 plugin version 0.2.1 into source code.

Offline

#8 2016-01-01 22:49:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,170
Website

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

You could use a TDocVariantData to store the array and convert it to an array of const.

Offline

#9 2016-01-02 12:37:25

EvaF
Member
Registered: 2014-07-19
Posts: 40

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

ab wrote:

You could use a TDocVariantData to store the array and convert it to an array of const.

ok, thanks, I did it by your recommended way.

Offline

#10 2016-12-22 10:26:01

lele9
Member
Registered: 2011-10-28
Posts: 170

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

Hi EvaF,
i'm testing your mormot-WP-REST-API.
I try to follow your instruction on readme, installed the plugin (but some version of plugin is updated), generate the key, edit json configuration file.
I have no response when i try to "get WP users".
I get error 404: rest_no_route if i try to create new user.
I read well woocommerce customers with "get Woo Customers".
I get error 404: woocommerce_api_no_route if i try to create new customer.
I think that i mistake something in the configuration file or in wordpress configuration.
Can you help me to understand?
thanks

Offline

#11 2016-12-22 14:18:11

EvaF
Member
Registered: 2014-07-19
Posts: 40

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

I will check all new versions and after that let you know...
E.

Offline

#12 2016-12-22 15:39:13

lele9
Member
Registered: 2011-10-28
Posts: 170

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

thanks a lot!

Offline

#13 2016-12-23 10:29:12

EvaF
Member
Registered: 2014-07-19
Posts: 40

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

I checked only WordpressREST ( sorry - a few time). I have installed new version (Oauth 0.3.0, Wordpress 4.7 +  Mormot Nigth Build -mORMot_and_Open_Source_friends_2016-12-21_193435_df194baa76) and made a couple of the small changes in WordpressREST sources.
The adding, updating and listing Wordpress users works now.

About the Error 404: rest_no_route
Please check your Wordpress endpoint address
=[WP-REST-API-Url] - http[https]://[your-WP-server]/wp-json/ in WPRESTAPI.json
and
=[WOO-REST-API-Url] - http[https]://[your-WP-server]/wc-api/[Woocommerce API version]/ in WooRESTAPI.JSON     

f.e.

http://somewordpress.blabla.com/wc-api/v2/

Last edited by EvaF (2016-12-23 10:31:49)

Offline

#14 2017-01-03 05:46:15

lele9
Member
Registered: 2011-10-28
Posts: 170

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

Hi,
I download mormorò nigth build and your fresh code from guthub bit now i have anche access violation when i try to get user or customer.
Thanks

Offline

#15 2017-01-04 20:41:49

EvaF
Member
Registered: 2014-07-19
Posts: 40

Re: A mORMot Wordpress REST API (and WooCommerce API) consumer

Hi,
I forgot to comment some debug statements - sorry. The fixed version of WPRESTAPI.pas file is on git.

Offline

Board footer

Powered by FluxBB