Thomas D.
Thomas D.

Reputation: 1061

Single backend with multiple APIs

I have a single backend defined, for this example named myserver with a runtime url https://myserver.com. Also, I have "Authorization credentials" set for this backend: The header x-custom-header is added with a secret value. Every request needs to provide this header.

My backend provides the following endpoints:

GET https://myserver.com/Devices
GET https://myserver.com/Devices/device-1
POST https://myserver.com/Devices
Get https://myserver.com/Weather?location=Vienna
...

Here comes the problem: I want to provide multiple APIs specific for the data:

For some reason, I don't understand how I can achieve that with only a single backend without too much configuration hassle. I have configured <set-backend-service backend-id="myserver" /> but that obviously does not correctly resolve https://mycompany-apim.azure-api.net/Devices to GET https://myserver.com/Devices but only to GET https://myserver.com/.

Some solutions that might work:

Are there alternatives?

Upvotes: 1

Views: 872

Answers (1)

Markus Meyer
Markus Meyer

Reputation: 3937

This can be achieved by creating one API e.g. 73219639 and the required operations:

enter image description here

It's important that each APIM operation has the same path e.g. /Devices:
https://rfqapiservicey27itmeb4cf7q.azure-api.net/Devices -> https://myserver.com/Devices

This is the simple working yaml:

openapi: 3.0.1
info:
  title: '73219639'
  description: ''
  version: '1.0'
servers:
  - url: 'https://rfqapiservicey27itmeb4cf7q.azure-api.net'
paths:
  /Devices:
    post:
      summary: Devices
      description: Devices
      operationId: post-devices
      responses:
        '200':
          description: OK
    get:
      summary: Devices
      description: Devices
      operationId: get-devices
      responses:
        '200':
          description: OK
  /Weather:
    get:
      summary: Weather
      description: Weather
      operationId: get-weather
      responses:
        '200':
          description: OK
components:
  securitySchemes:
    apiKeyHeader:
      type: apiKey
      name: Ocp-Apim-Subscription-Key
      in: header
    apiKeyQuery:
      type: apiKey
      name: subscription-key
      in: query
security:
  - apiKeyHeader: []
  - apiKeyQuery: []

Configure the Backend in the All operations policy with an existing backend:

Backend

<policies>
    <inbound>
        <base />
        <set-backend-service backend-id="myserver-com" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

Do not change/modify any other policy. It's working with the default ones.

GET Devices - Test:

HTTP request

GET https://rfqapiservicey27itmeb4cf7q.azure-api.net/Devices HTTP/1.1
Host: rfqapiservicey27itmeb4cf7q.azure-api.net
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
Ocp-Apim-Trace: true

Request

Trace: set-backend-service (18.701 ms)

{
    "message": "Backend service URL was changed.",
    "oldBackendServiceUrl": "https://rfqapiservicey27itmeb4cf7q.azure-api.net/",
    "newBackendServiceUrl": "https://myserver.com/",
    "request": {
        "url": "https://myserver.com/Devices"
    }
}

Trace: forward-request (0.127 ms)

{
    "message": "Request is being forwarded to the backend service. Timeout set to 300 seconds",
    "request": {
        "method": "GET",
        "url": "https://myserver.com/Devices",
        "headers": [
            {
                "name": "Host",
                "value": "myserver.com"
            },
            {

Forward

Upvotes: 1

Related Questions