Jump to content

New Module: AppApi


Sebi

Recommended Posts

Hi @Sebi

thanks for your reply. I've thought about it again. The website where I'm working on will be fully headless. The URLs mappings are then handled by a vuejs frontend. So in this case it does not matter much if it's controlled by a var or URL segement (Forgot about this when asking :roll_eyes:).

The requirements of the site are still a bit unclear but it looks I do not need the c-u-d of C-R-U-D. This is why I most likely will just build a small API with the new URL Hooks. This is how I would handle the language there (Quick and dirty example):

        $this->wire()->addHook('/{lang}/api/books', function($event) {
            $books = $this->pages->find("template=books");
            $response_arr = [];          
            foreach ($books as $book){
                $current_page = [];
                $current_page['title'] = $book->getLanguageValue($event->lang, 'title');
                $current_page['author'] = $book->getLanguageValue($event->lang, 'author');
				// ...
                array_push($res, $current_page);
            }
        	header('Content-Type: application/json; charset=utf-8');
        	echo json_encode( $response_arr );
        });

But it's great to have your Extension available in case there is some "heavier" API to build ;))

@Jukka Nice to hear!!

Link to comment
Share on other sites

Posted (edited)

@Sebi

Thanks for this great module, I'm testing it for a project right now. 

However I'm running into a slight problem, not sure if this is a bug or it's something on my end.
I'm testing it on two PW 3.0.165 instances and in one of them everything works perfect.

In the other I can't seem to edit an application to set an apiKey in the admin. When clicking it (/processwire/setup/appapi/application/edit/1) it renders the 404 of the website instead of showing the edit page of the application. I have two languages setup for this project and also formBuilder module. Other than that, I don't see any differences with the other working project.

The applications overview (/processwire/setup/appapi/applications) does work the way it should.

Do you have any idea where this could be going wrong?

 

Update: I removed all modules from the failing project that I had installed (FormBuilder, Language support), but to no avail.
I can navigate to /processwire/setup/appapi/applications/ which lists all applications, and /processwire/setup/appapi/application/edit (without an ID and it will give me a missing ID message within the admin). As soon as I add an ID (/processwire/setup/appapi/application/edit/1) it will fail, throws a 404 outside of the admin.

I also cleared out my ready.php to prevent any hooks causing issues.

 

@ryan You obviously can't provide support for a custom module, but is there anything here that sounds familiar? Could there be a routing or permission issue here? Is there some caching going on somewhere that might be causing this?

Edited by PascalKonings
Link to comment
Share on other sites

On 5/10/2022 at 12:46 PM, PascalKonings said:

@Sebi

Thanks for this great module, I'm testing it for a project right now. 

However I'm running into a slight problem, not sure if this is a bug or it's something on my end.
I'm testing it on two PW 3.0.165 instances and in one of them everything works perfect.

In the other I can't seem to edit an application to set an apiKey in the admin. When clicking it (/processwire/setup/appapi/application/edit/1) it renders the 404 of the website instead of showing the edit page of the application. I have two languages setup for this project and also formBuilder module. Other than that, I don't see any differences with the other working project.

The applications overview (/processwire/setup/appapi/applications) does work the way it should.

Do you have any idea where this could be going wrong?

 

Update: I removed all modules from the failing project that I had installed (FormBuilder, Language support), but to no avail.
I can navigate to /processwire/setup/appapi/applications/ which lists all applications, and /processwire/setup/appapi/application/edit (without an ID and it will give me a missing ID message within the admin). As soon as I add an ID (/processwire/setup/appapi/application/edit/1) it will fail, throws a 404 outside of the admin.

I also cleared out my ready.php to prevent any hooks causing issues.

 

@ryan You obviously can't provide support for a custom module, but is there anything here that sounds familiar? Could there be a routing or permission issue here? Is there some caching going on somewhere that might be causing this?

@ryan @Sebi Found it! I had maxUrlSegments set to 2 in my config file #doh

  • Like 1
Link to comment
Share on other sites

@Sebi

I'm running into a problem deleting a repeater item through the API, or any page for that matter.

Using my code below I'm getting an error

public static function deleteEvent($data) {
  $data = AppApiHelper::checkAndSanitizeRequiredParameters($data, ['id|int']);

  // $data->id returns an integer
  wire('pages')->get($data->id)->delete();

  // Using a static ID also fails
  // wire('pages')->get(1066)->delete();

  return [
  'message' => 'deletion successful',
  'status' => 200
  ];
}

 

The error I'm getting is:

devmessage: {message: "Trying to get property 'id' of non-object", location: '/myDev/wire/core/PagesEditor.php', line: 296}
error: "Internal Server Error"

 

This line is within the isDeleteable function, where it tries to get the ID of the page that needs to be deleted, however it seems the $page:

else if($page->id === $this->wire('page')->id && $this->wire('config')->installedAfter('2019-04-04')) {
	$error = "it is the current page being viewed, try \$pages->trash() instead";
}

 

I've set up the route without any auth or roles, so it should work from that perspective. Other routes getting and posting are working fine! And running the API from the template directly works perfectly: 

wire('pages')->get(1066)->delete();

 

Am I missing something here?

Link to comment
Share on other sites

  • 2 weeks later...
On 5/16/2022 at 9:44 AM, PascalKonings said:

@Sebi

I'm running into a problem deleting a repeater item through the API, or any page for that matter.

Am I missing something here?

@Sebi Any chance you have time to take a look at my issue above?

Link to comment
Share on other sites

This error is related to Processwire, not to the module. Check the documentation for the Repeater field in the section "Using the API to add or remove repeater items":  https://processwire.com/docs/fields/repeaters/ You can also check this post that talks about the same problem: https://processwire.com/talk/topic/16276-remove-repeater-item-by-page-id-rather-than-item-id/

 

  • Like 2
Link to comment
Share on other sites

Because I'm using the LoginRegisterPro module, users use their email to log in. So I'm wondering, for authentication, should i make an endpoint that returns the username for a given email address and use that to authenticate or is there a better way to solve this that doesn't use an extra request.

Link to comment
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
×
×
  • Create New...