CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • should we introduce civicrm_api3
Pages: [1] 2

Author Topic: should we introduce civicrm_api3  (Read 2656 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
should we introduce civicrm_api3
June 25, 2013, 04:05:29 pm
We talked about this on a ticket. Basically we agreed that there were advantages of a function

civicrm_api3($entity, $action &$params){
  $params['version'] = 3;
  return civicrm_api($entity, $action $params);
}

The advantages being
1) having a different fn name makes it easier to grep for &
2) it saves setting the version.
3) it saves the point of having version - allowing the api wrapper to continue supporting api3 while we implement api 4

Obviously introducing this is a very quick task. Promoting it / updating the examples would be a little slower.

Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: should we introduce civicrm_api3
June 25, 2013, 09:27:16 pm
Makes sense, I pledge to update the developer guide when we do have civicrm_api3.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: should we introduce civicrm_api3
June 25, 2013, 11:10:59 pm
civicrm_api3 sounds more pleasant/usable.

I was a little concerned that the name might conflict with "civicrm_api3" from api/class.api.php, but PHP doesn't seem to mind if a function and a class share the same name.


xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: should we introduce civicrm_api3
June 26, 2013, 12:41:34 am
Are we pushing it too much if we introduce a new feature: civicrm_api3 does raise an exception on error, instead of only is_error

(as in, by default, it crashes if you don't test is_error)

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: should we introduce civicrm_api3
June 26, 2013, 02:44:26 pm
Hmm  - I'm not sold on that - but perhaps others are.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: should we introduce civicrm_api3
June 26, 2013, 11:30:49 pm
I need a little more explanation Xavier?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: should we introduce civicrm_api3
June 27, 2013, 12:36:11 am
So a common pattern is I want to fetch an id of something:

$result = civicrm_api("Whatever","get",params...);
$myid=$result["values"][0][id];
//a long while after, do something else with myid

the issue is that it doesn't test on is_error and if there is a pb somewhere, $myid is going to be empty and it happily going to make a lot of weird errors hard to catch down the line. Another common one is to create something and assume it was created.

so the proper way is
$result = civicrm_api
if ($result["is_error") {// needed, but not mandatory
  //fix or do something
}

by using exceptions on errors, you'd do the same

try {
  $result = civicrm_api
} catch {
  // fix or do something
}

So the code isn't that much longer, the big benefit is that if you don't do the try catch

$result = civicrm_api("Whatever","get",params...);
if will stop with a clear error message.

So in short: force us lazy developers to be less sloppy, one fatal at a time

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: should we introduce civicrm_api3
June 27, 2013, 12:55:10 am
Yep, I get that...I always do the
if ( $result['is_error'])

That bit is clear to me, but that means that as of the next version, we do not provide the is_error anymore?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: should we introduce civicrm_api3
June 27, 2013, 01:11:18 am
Quote from: Erik Hommel on June 27, 2013, 12:55:10 am
...I always do the
if ( $result['is_error'])

Always? Us mere mortals tend to do it, the best of our kind might reach the almost always, but, according to the legend, even Tim has not tested it at least once.

Anyway, wasn't to remove, but on the top of. Or we might introduce a options.error_type=attribute|exception|both (the later being the default)

-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: should we introduce civicrm_api3
June 27, 2013, 09:23:09 am
So what about the other bindings?
javascript: CRM.api3('foo', 'get')
smarty: {crmAPI3 entity='foo' action='get'}
rest: no change?
Try asking your question on the new CiviCRM help site.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: should we introduce civicrm_api3
June 27, 2013, 10:06:23 am
That's a really good question. would be more coherent... but even more places to change the doc...

I think version was a mandatory param only for php anyway.

Plan B: we remove version as a mandatory field and default to version 3 we keep civicrm_api, CRM.api and {crmAPI} as v3 only and if/when api 4 is there, we introduce civicrm_api4 CRM.api4 and crmAPI4 at that time?


X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: should we introduce civicrm_api3
June 27, 2013, 10:28:05 am
I like plan b, at least for the alternate bindings. For php I see no reason not to introduce civicrm_api3 in 4.4 as a thin wrapper around civicrm_api. People don't have to use it if they don't want to, but it will provide some consistency going forward with api4.
Not sure whether that's the time to introduce exceptions or wait for api4. I could be convinced either way.
Try asking your question on the new CiviCRM help site.

nicolas

  • I post occasionally
  • **
  • Posts: 92
  • Karma: 6
    • cividesk
  • CiviCRM version: 4.4 LTS
  • CMS version: Standalone (yep)
  • MySQL version: 5.1
  • PHP version: 5.3
Re: should we introduce civicrm_api3
June 27, 2013, 02:01:56 pm
I like the idea of raising an exception over returning is_error - much cleaner. I also agree there is no need to change the other bindings.

On updating the documentation, do we need to replace/obsolete the old method or just add this new one? I would rather consider this an addition only and keep both options open for developers in the future (ie. api v4 called either through civicrm_api with 'version' => 4, or through civicrm_api4).
cividesk -- CiviCRM delivered ... your way!

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: should we introduce civicrm_api3
June 28, 2013, 01:07:05 am
Xavier, the only thing I always do is forget something :-)
Happy with the exception and happy with plan b!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: should we introduce civicrm_api3
June 28, 2013, 02:13:32 am
I'm confused (permanently)

Is plan b that we leave civicrm_api as it is but make 'version' => 3 a default rather than required.

We also introduce civicrm_api3 which wraps civicrm_api but throws an exception
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • should we introduce civicrm_api3

This forum was archived on 2017-11-26.