Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: how to use OpenAPI connector #4488

Closed
2 tasks done
bajtos opened this issue Jan 23, 2020 · 10 comments
Closed
2 tasks done

Docs: how to use OpenAPI connector #4488

bajtos opened this issue Jan 23, 2020 · 10 comments
Assignees
Milestone

Comments

@bajtos
Copy link
Member

bajtos commented Jan 23, 2020

As a LoopBack 4 user connecting to a REST service with an OpenAPI description, I would like to leverage loopback-connector-openapi to build an easy-to-use service proxy for me. Unfortunately, there is very little information how to do that.

Acceptance Criteria

Possible structure:

  • In "Calling other APIs", provide short section covering the common path at high level and point users to the connector docs for more details.
  • In connector's README, go deeper into OpenAPI-specific configuration and behavior.
@dhmlau dhmlau added this to the April 2020 milestone Mar 17, 2020
@dhmlau dhmlau removed this from the April 2020 milestone Apr 1, 2020
@dhmlau
Copy link
Member

dhmlau commented Apr 1, 2020

@bajtos @raymondfeng , just wanna confirm with you the requirement of this task before starting to write anything.

If we want to call a third party REST API, say the PetStore, we would:

  • step 1: Run lb4 openapi with the OpenAPI json, to create the models and controller
  • step 2: Run lb4 datasource with the PetStore API url using REST connector
  • step 3: Run lb4 service to create the service proxy with the above datasource
  • step 4: Implement the methods in the controller generated in step 1 to call the service from step 3

Are they what the right steps? Thanks.

@raymondfeng
Copy link
Contributor

lb4 openapi is to implement an OpenAPI spec. To access 3rd party REST service with OpenAPI spec, use lb4 datasource and lb4 service (with loopback-connector-openapi).

In the future, we should extend lb4 openapi --client option to generate client service proxies with strongly-typed interfaces and types. Contrast to lb4 openapi generating controller code, the client option should generate client proxy/stub instead.

@dhmlau
Copy link
Member

dhmlau commented Apr 1, 2020

I actually tried with lb4 datasource (with OpenAPI connector) and lb4 service.
The datasource json is:

  "name": "ds",
  "connector": "openapi",
  "spec": "http://petstore.swagger.io:8080/api/v3/openapi.json",
  "validate": false,
  "positional": false
}

and I ran into error:

Unhandled error in POST /pet: 500 Error: only absolute urls are supported

Any ideas?

@dhmlau
Copy link
Member

dhmlau commented Apr 1, 2020

Let me try again and create a repo if the problem still exists.

@dhmlau
Copy link
Member

dhmlau commented Apr 1, 2020

@raymondfeng, looks like the "absolute urls" error still exists.

This is my repo: https://github.com/dhmlau/openapi-test2

  • there is one endpoint for addPet. Use the following requestBody:


{
  "id": 10,
  "name": "doggie",
  "category": {
    "id": 1,
    "name": "Dogs"
  },
  "photoUrls": [
    "string"
  ],
  "tags": [
    {
      "id": 0,
      "name": "string"
    }
  ],
  "status": "available"
}

Then you'll get an error something like:

Unhandled error in POST /pets: 500 Error: only absolute urls are supported
    at /Users/dhmlau/loopback4-example/openapi-test/node_modules/node-fetch/index.js:54:10
    at new Promise (<anonymous>)
    at new Fetch (/Users/dhmlau/loopback4-example/openapi-test/node_modules/node-fetch/index.js:49:9)
    at Object.Fetch (/Users/dhmlau/loopback4-example/openapi-test/node_modules/node-fetch/index.js:37:10)
    at Object.fetch (/Users/dhmlau/loopback4-example/openapi-test/node_modules/cross-fetch/fetch-node.js:11:20)
    at _callee$ (/Users/dhmlau/loopback4-example/openapi-test/node_modules/swagger-client/dist/index.js:581:86)

Thanks!

@dhmlau
Copy link
Member

dhmlau commented Apr 2, 2020

Still investigating. Got passed the above error, because it's expecting url in https://github.com/dhmlau/openapi-test2/blob/master/petstore.json#L18 to be in absolute path. But I got a 400 Bad Request error.

@dhmlau
Copy link
Member

dhmlau commented Apr 4, 2020

Got it working in https://github.com/dhmlau/openapi-test2.
Taking the petstore json, I need to modify this line:

  "servers": [{"url": "/api/v3"}],

to

  "servers": [{"url": "http://petstore.swagger.io:8080/api/v3/"}],

Otherwise, I will get the 500 Error: only absolute urls are supported error.

@dhmlau
Copy link
Member

dhmlau commented Apr 12, 2020

Closing as done.

@dhmlau dhmlau closed this as completed Apr 12, 2020
@kannant-sf
Copy link

@dhmlau am facing issue while creating post, i don't find any proper documentation on how to call post method and how many params we need to send in which param we need to send body data?

@kannan007
Copy link

@dhmlau i saw the way you had written in controllers and service file, tried the same in my code but am getting 400 status code error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants