Problem/Motivation

My API I want to proxy is something like `https://api.example.com/search?clientId=4583&contentTypeId=CODES&fragmen...` but all I want to expose is the `&searchText=pool` and really just the value of the URL parameter "pool".

Setting the service URL to a URL with query parameters doesn't seem to work.

 *   description = @Translation("Proxies requests to api.example.com"),
 *   serviceUrl = "https://api.example.com/search?clientId=4583&contentTypeId=CODES&fragmentSize=200&isAdvanced=false&searchText=",

I would like the exposed URL that proxies to just be `/api-proxy/api-slug?_api_proxy_uri=pool`

How is that possible?

Side note the docs are a bit lacking for begginers and this reddit thread helped to understand what I need to do https://www.reddit.com/r/drupal/comments/hpb85g/how_to_implement_a_modul...

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#3 2021-03-08_12-39-49.png47.84 KBnicholass

Comments

NicholasS created an issue. See original summary.

e0ipso’s picture

Category: Feature request » Support request
Status: Active » Fixed

I would like the exposed URL that proxies to just be `/api-proxy/api-slug?_api_proxy_uri=pool`

If you search the module for the serviceUrl you will see that you can override the method.

  /**
   * {@inheritdoc}
   */
  public function getBaseUrl(): string {
    return $this->getPluginDefinition()['serviceUrl'];
  }

To provide whatever you need.

Side note the docs are a bit lacking for begginers and this reddit thread helped to understand what I need to do https://www.reddit.com/r/drupal/comments/hpb85g/how_to_implement_a_modul...

I would love to improve the docs. Could you write the documentation you would have liked to have as a beginner, now that you understand it better?

nicholass’s picture

StatusFileSize
new47.84 KB

@e0ipso Thanks I will give that a shot, its much better than what I was trying to do by overriding the preprocessIncoming I think.

The below was working but required me to request /api-proxy/api-municode?_api_proxy_uri=search%2F%3FsearchText%3DPool in order to get results for "Pool" from the API
screen shot

And yes I will help with updating the docs, mainly my confusion was the same as that reddit thread. Us front end guys I guess need it spelled out that "Create a proxy plugin" means a custom module with a file located at `/src/Plugin/api_proxy/Example.php' An example on Github also helped clarify what I needed to do https://github.com/weaver299/asc_cors_proxy.

nicholass’s picture

So I tried to override the `getBaseUrl()`

/**
* {@inheritdoc}
 */
public function getBaseUrl(): string {
  return 'https://api.municode.com/search?clientId=4583&contentTypeId=CODES&pageNum=1&pageSize=5&stateId=9&titlesOnly=true&searchText=';
}

Drupal URL request is now /api-proxy/api-municode?_api_proxy_uri=pool (awesome nice and clean)

But it seems the module always prepends a "/" so what is actually being requested is "https://api.municode.com/search?clientId=4583&contentTypeId=CODES&pageNum=1&pageSize=5&stateId=9&titlesOnly=true&searchText=/pool" (the end `&searchText=/pool` is the problem needs to just be `&searchText=pool`) I think this line is what is adding that forward slash https://git.drupalcode.org/project/api_proxy/-/blob/8.x-1.x/src/Plugin/H...

So I guess a path is always expected, and I can't just open up a query parameter to only be modified?

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.