Jump to content
Sebi

New Module: AppApi

Recommended Posts

I'm here! 🥳

Hi @benbyf, I'm glad that your using AppApi! Let me clear this up a little:

1. It's a bug, I am linking to a protocol named 'app_api' in the module's settings, but the actual logs are written in a log named 'appapi-exception'. I will create an issue for that...

2. @thomasaull is exactly right. Currently there is no way to connect to the api without an api-key. It is necessary, because the key is needed to identify which application-configuration should be used. I am definitely planning to make it possible to define a "default"-application, that handles requests without an apikey, but it will need some time...an 

3. I would generally create new apikeys, even for small version-steps. If you need to block an old version, you can set the accessable-until field of the apikey or simply delete it.
What I don't fully understand is what you mean by "form_version", @benbyf. You can define an endpoint api/v1/test and api/test and let them handle requests from different versions. But the module does not automatically handle versions, add v1 to your path or something like that. Maybe you can specify what you are trying to achieve here?

Lastly, I must honestly state, that I am very busy with a very time-consuming project at work, which is why I do not find much time to work on module-updates. It will probably take until Christmas until i can put time into the project again. So if you understand the module's internal logic, I would be thankful for your pull-requests...

 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks both @Sebi and @thomasaull I appreciate your time.

Will look into the log bug as thats paramount to us using the module so would be great to clear up.

The non-apikey endpoints is more of a nice to have really as you can set up informational endpoints that act like website frontends but without the visual code obviously.

In terms of users, applications and version, they seem loosely connected. There seems little reason to fill out the apikey version  when creating a new apikey if it doesnt actually relate to anything. But maybe i just havent seen how it does or could relate. Also the users are too loosely associated with the applications (slight tangent), but it would be nice to leverage the user and roles system byt associating a user/s with either key or applications, and then auth them against them on route. e.g. [..., [route_options_role_required = "delete-pages, update-pages, create uses" ]] as the folowing api might enable these things and you wanna be pretty damn sure that user is allowed to do it 🙂

Share this post


Link to post
Share on other sites

@benbyf I think that's not possible out of the box with the module. If you really want to use one api key per user you somehow would need to programmatically create them and store a reference on the user. But honestly this doesn't look like a good solution to me.

You're free to add any checks in your API functions to restrict certain user/roles from doing things, and that is probably how I would handle this. This way you can leverage the entire ProcessWire API. For example:

// Route: /api/publish-page/{id}

public static function publishPage($data) {
  $data = AppApiHelper::checkAndSanitizeRequiredParameters($data, ['id|int']);
  $page = wire('pages')->get($data->id);

  if(!wire('user')->hasPermission('page-publish', $page)) {
    throw new Exception('Not-allowed'); // Or use AppApiException here
  }

  // Publish page   
}

 

  • Like 1

Share this post


Link to post
Share on other sites

Hi @Sebi I added the log to app-api (will create a PR somepoint soon), I wondered if you wanted a log message on each request and maybe a setting in the modules settings to turn on or off, or maybe something more granular?

My use case is going to require lots of log messages in the api files but having a generic log message on each request I though might be nice. Also wondered if it was worth making a second log for failed (exception) based log messages? so successful requests get logged to one and exceptions to the other..... any thoughts would be cool

  • Thanks 1

Share this post


Link to post
Share on other sites

Hi @Sebi I'm pretty new to ProcessWire and I've started using your module to add an API. I'm really appreciating all the work from you and @thomasaull as it's making my life much easier!

I've spotted a couple of issues around HTTP statuses, one of them I've managed to do a PR for, it was pretty straightforward. The other issue is potentially a bit more complicated, or at least requires a bit of careful thought.

Currently you can't set the response code, it's just hard-coded to 200, but I'd like to send a 201 on create. I can think of at least two ways of doing it, one is quick but messy: return the status as part of the handler response; the other is probably neater but needs choices to be made: add a function (but where? AppApiHelper?) that stores the status and then retrieve it in the router.

Any thoughts?

  • Like 1

Share this post


Link to post
Share on other sites

Hey @David Lumm, thanks for your suggestions and PR with improvements. I haven't had the need yet, but it's definitely a function which should be included in the AppApi module for sure.

Share this post


Link to post
Share on other sites

Hi everyone!

I just wanted to give a quick sign of life. The time of Christmas vacation is finally reached and I can finally invest time in open source project again. Thanks a lot for your input. In the next days there will finally be an update for the AppApi module. I would then like to provide you with a development state for testing before a new master version is released.

Let us list the feature-requests:

  • Error when importing database dump: Cannot add foreign key constraint (by @thomasaull)
  • Multi Language and ProCache do not work 
  • Weird HTTP status responses (by @David Lumm, thanks for PR 🥳)
  • [Feature] Add multiple levels to routing config (by @David Lumm, thanks for PR 🤗)
  • Optional extended logging like @benbyf said earlier. Created Issue #10 on Github so I don't forget it. 
  • I will look into the other bugs and issues discussed here as well.

Did I leave something out? Thank you for your help and stay tuned for the next updates!

giphy.gif

  • Like 5

Share this post


Link to post
Share on other sites

... not a module request but I'd love to have a snippet of code to access and expose Repeater Matrix Fields inside the docs :) 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Merry Christmas everyone! I hope you have some peaceful and calm days! 🎄

If you've been keeping an eye on the commits in the meantime, you'll have seen some progress for the upcoming version 1.1.0. The current status in the develop branch should be as good as finished in terms of programming. Only the readme has not yet been adapted. I want to move large parts of it to the repository wiki and then add more examples.

If you have some time, I would be very happy if you could test the new features (develop-branch).

What's new:

  • Improved AppApi-dashboard
  • Allow multiple levels to routing config (by @David Lumm, thanks for PR 🤗)
  • Allow requests without an api-key: You can now mark an application as "default application". If you did so, every request without an apikey will be linked with that application.
  • You can now set a custom response-code in case of success. Simply include your response-code number on key "responseCode" in your result-array. For example my test-function, which is called on a route in Routes.php:
<?php
class AppApiTest {
    public static function test($data) {
        return [
          'success' => true,
          'responseCode' => 202
        ];
    }
}
  • Optional access-logging: You can enable access-logging in the module's configuration. After that, every successful request will be logged with it's application-id, apikey-id and token-id.
  • Added hooks to all essential functions - that should enable you to customize the module's behavior even more. E.g. you could add custom logging on a hook, if you need that
  • Database-scheme does not need foreign-key constraints any more. That should fix @thomasaull 's issue with db-backups. After the update, you must remove the constraint manually because I did not find a way to remove the foreign key safely in all database-environments.
  • Multiple other bugfixes

What do you think? I look forward to your feedback!

  • Like 4
  • Thanks 1

Share this post


Link to post
Share on other sites
On 12/20/2020 at 7:06 PM, 3fingers said:

... not a module request but I'd love to have a snippet of code to access and expose Repeater Matrix Fields inside the docs 🙂

What you do in your API functions is not different from what you would do in regular ProcessWire templates. Is there anything specific you need help with?

Share this post


Link to post
Share on other sites
On 12/20/2020 at 7:06 PM, 3fingers said:

... not a module request but I'd love to have a snippet of code to access and expose Repeater Matrix Fields inside the docs 🙂

@thomasaull is right. I have added an example in the Wiki, in which I demonstrate the output of a RepeaterMatrix-field "contents" that I use in most of my projects:

https://github.com/Sebiworld/AppApi/wiki/3.1:-Output-Formatting#complex-data

I hope, that helps. Generally speaking, you can output anything you want via api. You only have to transform complex structures like ProcessWire-Pages, Repeatermatrix-fields, ... into something that is JSON-encodable.

Share this post


Link to post
Share on other sites
4 hours ago, 3fingers said:

@Sebi (I've just bought you 3 coffees)

Thank you @3fingers ! Your coffees will help me to stay motivated ☺️

  • Haha 1

Share this post


Link to post
Share on other sites

I just installed the module, but everytime i add an application i get an SQL error.

The application could not be saved: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '' for column `planner`.`appapi_applications`.`default_application` at row 1

Share this post


Link to post
Share on other sites

Hey @derixithy

I've just set up a new ProcessWire-instance (current stable 3.0.165) and installed the module. Everything seems normal and I can add applications without getting an error. Maybe it is a problem with your individual configuration, can you give me additional details (e.g. ProcessWire-version, was AppApi installed before and updated to v1.1.0, or is it a fresh install? What PHP-version do you run?)

The following is the INSERT-statement I use to create a new application in db:

$createStatement = 'INSERT INTO `' . AppApi::tableApplications . '` 
(`id`, `created_user_id`, `created`,`modified_user_id`, `modified`, `title`, `description`, `default_application`, `token_secret`, `accesstoken_secret`, `authtype`, `expires_in`) 
VALUES 
(NULL, :created_user_id, :created, :modified_user_id, :modified, :title, :description, :default_application, :token_secret, :accesstoken_secret, :authtype, :expires_in);';

So let me try to interpret the error message... "Invalid datetime format: 1366" - 1366 looks like a user-id, that should be used as variable :created_user_id. But it tries to insert that value to a datetime-column, which could be the 'created'-column that follow right after that in the insert-statement. So it looks like that something went wrong with the variable-values for the PHP-prepared statement I use. 

You find the create-statement here in the code: https://github.com/Sebiworld/AppApi/blob/65911a3f13c6420ffad0e68b34d8c51a8b1eb99c/classes/Application.php#L614

Have any of you ever seen anything like this?

Share this post


Link to post
Share on other sites

Great work @sebi!

Am I reasonably safe to upgrade to the 1.1.0 version, it won't break anything?

Share this post


Link to post
Share on other sites

Hey @David Lumm, thanks!

Short answer: Yes, I'm pretty sure that upgrading to 1.1.0 will not break anything.

Longer answer: Every previous functionality will work as before - there are only additional features, but no breaking changes. During the update the module will add a new column "default_application" to the appapi_applications table. Everything else stays the same.

To be on the safe side, I tested upgrading to 1.1.0 and new installing in multiple different configurations. Besides @derixithy 's comment above I did not hear any problems. But if against all expectations you notice something, feel free to contact me 🤗

Share this post


Link to post
Share on other sites

Hi @David Lumm, I'm workung on an emergency fix. Still cannot reproduce the problem, but I will try to change the datatype of the default_application-column from boolean to int. Hopefully that fixes it. 

Share this post


Link to post
Share on other sites
17 minutes ago, Sebi said:

Hi @David Lumm, I'm workung on an emergency fix. Still cannot reproduce the problem, but I will try to change the datatype of the default_application-column from boolean to int. Hopefully that fixes it. 

@David Lumm: v1.1.1 is out. It changes the datatype to int(1). Works for me - can you please check if that fixes the error on your configuration, too?

@derixithy: You mentioned the same error - I hope that v1.1.1 fixes it!

Share this post


Link to post
Share on other sites

Hi

When using the module with version 1.1.1 I have a strange issue with using the "StdClass".
I keep getting the error as you can see in the screenshot "Cannot use object of type stdClass as array".

In previous projects similar code was working fine, something broken with this? My calls are just regular GET requests.
If I don't use the "stdclass" I'm not receiving errors anymore.

Screenshot 2021-01-18 202346.png

Screenshot 2021-01-18 202453.png

  • Thanks 1

Share this post


Link to post
Share on other sites

Hi @thibaultvdb,

thank you for reporting this issue! 
I'll be honest: In my Apis I actually always use arrays as return values, so I didn't notice this bug. With version 1.1.2, which I just released, you can use a stdclass again instead of an array as return value. 

I hope everything is running smoothly again with your Api? I would be very happy about a short feedback!

  • Like 1

Share this post


Link to post
Share on other sites

Hey all!

I was wondering, is there any concept of "middleware" or hooks that I could attach to so I could run something on every request?

At the moment I've got a couple of things I've added into every route function, but I can anticipate that list getting longer. Rather than copy and paste the same lines into each and potentially miss something, it would be great if I could run it before the route specific code runs. Is that possible @Sebi?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jploch
      Hey folks,
      for a module (a pagebuilder based on PageTable) I need to save some settings as JSON. The values are saved for each page table item (a pw page). It's working well, but I am looking for ways to improve the structure I have. As I'm not that experienced with JSON, maybe someone more experienced can take a look and tell me if my approach is good practice. 

      My goal is to make all the items accessible by page id, without looping over them (using objects instead of arrays):
      // access from template with pw page var $jsonObject->items->{$page}->cssClass; Her is an example of my JSON structure:
      { "items": { "3252": { "id": "3252", "cssClass": "pgrid-main", "breakpoints": { "base": { "css": { "grid-column-end": "auto", "grid-row-end": "auto", "grid-column-start": "auto", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "60px", "padding-right": "60px", "padding-top": "60px", "padding-bottom": "60px", "background-color": "rgb(255, 255, 255)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } }, "3686": { "id": "3686", "cssClass": "test_global", "breakpoints": { "base": { "css": { "grid-column-end": "-1", "grid-row-end": "span 1", "grid-column-start": "1", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "0px", "padding-right": "0px", "padding-top": "0px", "padding-bottom": "0px", "background-color": "rgba(0, 0, 0, 0)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } }, "3687": { "id": "3687", "cssClass": "block_editor-3687", "breakpoints": { "base": { "css": { "grid-column-end": "span 2", "grid-row-end": "span 1", "grid-column-start": "auto", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "0px", "padding-right": "0px", "padding-top": "0px", "padding-bottom": "0px", "background-color": "rgba(0, 0, 0, 0)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } }, "3696": { "id": "3696", "cssClass": "block_editor-3696", "breakpoints": { "base": { "css": { "grid-column-end": "span 2", "grid-row-end": "span 1", "grid-column-start": "auto", "grid-row-start": "auto", "align-self": "auto", "z-index": "auto", "padding-left": "0px", "padding-right": "0px", "padding-top": "0px", "padding-bottom": "0px", "background-color": "rgba(0, 0, 0, 0)", "color": "rgb(0, 0, 0)" }, "size": "@media (min-width: 576px)", "name": "base" } } } }, "breakpointActive": "base", "breakpointActiveSize": "@media (min-width: 576px)" }  
    • By jploch
      Fieldtype Page Table Grid
      This is a sneak preview of a side project I've been working on for quite some time now. A lot of work and thought has gone into this, so I will most likely release this as a commercial module at some point in the near future. 

      As a designer (and developer) I get the appeal of a WYSIWYG editor. After playing around with some WYSIWYG page builder tools, I always felt something was wrong about them. So I decided to build my own PW version based on PageTable.

      Here is a small demo (using AdminThemeCanvas, but its working with other admin themes as well) :
      There is also a complete website that I built for a friend of mine using this module and some custom blocks.
      Concept
      This fieldtype shares a lot of features with PageTableExtended: it's also an extension of PageTable and renders the block templates in the backend and frontend (native PW templates and fields). You can also add your own css via module settings.
      The difference is, this fieldtype also gives you the ability to rearrange and resize elements in a visual way as well as enable inline editing for text, ckeditor and file fields. Similar (and promising) attempts have been made, but I wanted something based on native CSS grid instead of a CSS framework...so I built my own version. Most CSS frameworks are based on flexbox, which is great for layouting elements horizontally. With CSS grid, you can place elements horizontally and vertically, allowing for layouts that were not previously possible with CSS. Similar to webflow, this fieldtype uses javascript (in the backend) to let you manipulate CSS grid in a visual way to design fully responsive websites (or parts of them). It should still be possible to include a CSS framework if you like (just add the classes to your block markup and include the CSS via module settings).
      The CSS grid layout manipulations are saved in a single field as a JSON array and used to generate a dynamic stylesheet that you simply include in your main template (no inline styles). The styles are saved within the breakpoint you select and cascade down to smaller breakpoints. That means you can specify just the basic breakpoint and adjust other breakpoints if needed. The exception is the mobile breakpoint which will display everything in one column as a default (you can change the layout here too).
      The fieldtype also comes with an optional style panel to manipulate some additional CSS properties directly on the page. You can customize the panel or disable it completely from the module settings (and just use a CSS file that you include via module settings). The style panel is based on inputfields (nothing is saved to the database). This means that you just have to install the module and all fields are available to all blocks automatically (this can be customized). It also has the benefit that your installation is not flooded with fields; this module only installs one field.
      Don't want to give your customer all that power? Design features can be disabled for certain roles. The grid editor role can just edit the content and use the inline editing feature to edit content quickly. You can then also grant access individually to the style panel, resize or drag functionality.
      Features
      Blocks are just pages Blocks are defined by native PW templates and fields Manipulate CSS grid in a visual way to design fully responsive websites (or parts of them) Design features can be disabled for certain roles Inline editing of text, ckeditor and file fields The layout is 100% CSS grid (very small css file size) Simply drag and resize to manipulate grid items directly inside the backend Manipulate grid columns and rows directly on the page (use any number of columns you want) All style manipulations are saved as JSON and used to generate a dynamic stylesheet that you just include in your main template (no inline styles) Nested groups/grids (child pages of nested blocks are created under group parent) Global blocks work with page reference field (changes on one page, changes all blocks on all pages) Manual and auto placement of grid items Define custom icons for your blocks via native template settings (template -> advanced -> icon) Option to load lazysizes in the backend to enable lazy loading of assets with class lazyload Works with all default and ui-kit based admin themes If you have any questions or feedback, let me know.
    • By bernhard
      I built this module because I needed a versatile solution to replace tags and simple if-blocks in some E-Mails and PDF documents.
      If you only need to replace static tags (no if-conditions), then you can use default PW api and need no module:
      $str = "My favourite color is {color}."; $texttools = $sanitizer->getTextTools(); echo $texttools->populatePlaceholders($str, ['color' => 'red']); // output: My favourite color is red. Usage:
      See the two example Files in the folder /replacements

       
      Methods:
      replacementsTable()
      Renders an overview of all available replacements (see the example in the Module's config file:
       
      Create new Replacements:
      Simply copy the sample file and adopt to your needs.
       
      Download:
      https://gitlab.com/baumrock/RockReplacer
    • By bernhard
      DEPRECATED
      I'm using this module in several projects, but it will likely not see any updates in the future. I'm not happy with it and I'm looking for ways to develop better solutions. RockTabulator was my first try, but I'm also not 100% happy with that. The tabulator library is great, but my module implementation is not. I hope to get a good solution soon, but it will be a lot of work...
      ---
      Some of you might have followed the development of this module here: https://processwire.com/talk/topic/15524-previewdiscussion-rockdatatables/ . It is the successor of "RockDataTables" and requires RockFinder to get the data for the grid easily and efficiently. It uses the open source part of agGrid for grid rendering.
       
      WHY?
      ProcessWire is awesome for creating all kinds of custom backend applications, but where it is not so awesome in my opinion is when it comes to listing this data. Of course we have the built in page lister and we have ListerPro, but none of that solutions is capable of properly displaying large amounts of data, for example lists of revenues, aggregations, quick and easy sorts by the user, instant filter and those kind of features. RockGrid to the rescue 😉 
       
      Features/Highlights:
      100k+ rows Instant (client side) filter, search, sort (different sort based on data type, eg "lower/greater than" for numbers, "contains" for strings) extendable via plugins (available plugins at the moment: fullscreen, csv export, reload, batch-processing of data, column sum/statistics, row selection) all the agGrid features (cell renderers, cell styling, pagination, column grouping etc) vanilla javascript, backend and frontend support (though not all plugins are working on the frontend yet and I don't plan to support it as long as I don't need it myself)  
      Limitations:
      While there is an option to retrieve data via AJAX the actual processing of the grid (displaying, filtering, sorting) is done on the client side, meaning that you can get into troubles when handling really large datasets of several thousands of rows. agGrid should be one of the most performant grid options in the world (see the official example page with a 100k row example) and does a lot to prevent problems (such as virtual row rendering), but you should always have this limitation in mind as this is a major difference to the available lister options that do not have this limitation.
      Currently it only supports AdminThemeUikit and I don't plan to support any other admin theme.
       
      Download: https://gitlab.com/baumrock/FieldtypeRockGrid
      Installation: https://gitlab.com/baumrock/RockGrid/wikis/Installation
      Quikckstart: https://gitlab.com/baumrock/RockGrid/wikis/quickstart
      Further instructions: https://gitlab.com/baumrock/RockGrid/wikis/quickstart#further-instructions
      German Translation File: site--modules--fieldtyperockgrid--fieldtyperockgrid-module-php.json
      Changelog: https://gitlab.com/baumrock/FieldtypeRockGrid/raw/master/changelog.md
       
      Module status: alpha, License: MIT
      Note that every installation and uninstallation sends an anonymous google analytics event to my google analytics account. If you don't want that feel free to remove the appropriate lines of code before installation/uninstallation.
       
      Contribute:
      You can contribute to the development of this and other modules or just say thank you by
      testing, reporting issues and making PRs at gitlab liking this post buying me a drink: paypal.me/baumrock/5 liking my facebook page: facebook.com/baumrock hiring me for pw work: baumrock.com  
      Support: Please note that this module might not be as easy and plug&play as many other modules. It needs a good understanding of agGrid (and JavaScript in general) and it likely needs some looks into the code to get all the options. Please understand that I can not provide free support for every request here in the forum. I try to answer all questions that might also help others or that might improve the module but for individual requests I offer paid support (please contact me via PM).
       
      Use Cases / Examples:
      Colored grid cells, Icons, Links etc. The Grid also has a "batcher" feature built in that helps communicating with the server via AJAX and managing resource intensive tasks in batches:

      Filters, PW panel links and instant reload on panel close:

      You can combine the grid with a chart library like I did with the (outdated) RockDataTables module:

    • By Paul Greinke
      Hi there. I wrote a custom module for one of my projects. In fact I maybe want to use my module in other projects too. In order to be variable and customizable  I need to implement some custom hooks into my module. So I can afterwards hook into the my functions in order to modify them to match the needs of the new project.
      I tried simply defining functions with the '__' prefix. But that did not work. I'm imagining something like the following:
      <?php class MyClass { public function ___someFunction() { // Do something } } // ready.php $this->addHookBefore('MyClass::someFunction', function($event) { // some customization }); Is there a way to accomplish that? 
×
×
  • Create New...