Skip to content

Commit

Permalink
Merge pull request #80 from Clever/js-client-generation
Browse files Browse the repository at this point in the history
js client generation
  • Loading branch information
cozmo authored Oct 31, 2016
2 parents 731f34b + 3bf9acf commit d6d072e
Show file tree
Hide file tree
Showing 62 changed files with 2,281 additions and 95 deletions.
19 changes: 9 additions & 10 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ notify:
webhook_url: $$slack_webhook
publish:
report_card: {}
docker:
docker_host: $$docker_server
email: $$docker_email
image_name: clever/wag
password: $$docker_password
registry_login: true
tags:
- $(git rev-parse --short HEAD)
- latest
username: $$docker_username
github:
artifacts:
- release
repo: wag
script:
- make release
tag: v$(cat VERSION)
token: $$github_token
user: Clever
when:
branch: master
script:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ bin/

vendor/
badcode.txt
release/
.DS_Store

30 changes: 20 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
include golang.mk
.DEFAULT_GOAL := test # override default goal set in library makefile
.PHONY: test build
.PHONY: test build release
PKG := github.com/Clever/wag
PKGS := $(shell go list ./... | grep -v /vendor | grep -v /samples/gen* | grep -v /hardcoded)
VERSION := $(shell head -n 1 VERSION)
EXECUTABLE := wag

$(eval $(call golang-version-check,1.7))

MOCKGEN := $(GOPATH)/bin/mockgen
Expand All @@ -11,20 +14,19 @@ $(MOCKGEN):
go get -u github.com/golang/mock/mockgen

build: hardcoded/hardcoded.go
# disable CGO and link completely statically (this is to enable us to run in containers that don't use glibc)
CGO_ENABLED=0 go build -installsuffix cgo -o bin/wag
go build -o bin/wag

test: build generate $(PKGS)

generate: hardcoded/hardcoded.go $(MOCKGEN)
./bin/wag -file samples/swagger.yml -package $(PKG)/samples/gen-go
./bin/wag -file samples/swagger.yml -go-package $(PKG)/samples/gen-go -js-path $(GOPATH)/src/$(PKG)/samples/gen-js
go generate $(PKG)/samples/gen-go...
./bin/wag -file samples/nodefinitions.yml -package $(PKG)/samples/gen-no-definitions
go generate ${PKG}/samples/gen-no-definitions...
./bin/wag -file samples/deprecated.yml -package $(PKG)/samples/gen-deprecated
go generate ${PKG}/samples/gen-deprecated...
./bin/wag -file samples/wag-patch.yml -package $(PKG)/samples/gen-wag-patch
go generate ${PKG}/samples/gen-wag-patch...
./bin/wag -file samples/nodefinitions.yml -go-package $(PKG)/samples/gen-go-no-definitions -js-path $(GOPATH)/src/$(PKG)/samples/gen-js-no-definitions
go generate ${PKG}/samples/gen-go-no-definitions...
./bin/wag -file samples/deprecated.yml -go-package $(PKG)/samples/gen-go-deprecated -js-path $(GOPATH)/src/$(PKG)/samples/gen-js-deprecated
go generate ${PKG}/samples/gen-go-deprecated...
./bin/wag -file samples/wag-patch.yml -go-package $(PKG)/samples/gen-go-wag-patch -js-path $(GOPATH)/src/$(PKG)/samples/gen-js-wag-patch
go generate ${PKG}/samples/gen-go-wag-patch...

$(PKGS): golang-test-all-strict-deps
$(call golang-test-all-strict,$@)
Expand All @@ -43,3 +45,11 @@ $(GOPATH)/bin/glide:

install_deps: $(GOPATH)/bin/glide
$(GOPATH)/bin/glide install -v

release: hardcoded/hardcoded.go
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X main.version=$(VERSION)" -o="$@/$(EXECUTABLE)"
tar -C $@ -zcvf "$@/$(EXECUTABLE)-$(VERSION)-linux-amd64.tar.gz" $(EXECUTABLE)
@rm "$@/$(EXECUTABLE)"
GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w -X main.version=$(VERSION)" -o="$@/$(EXECUTABLE)"
tar -C $@ -zcvf "$@/$(EXECUTABLE)-$(VERSION)-darwin-amd64.tar.gz" $(EXECUTABLE)
@rm "$@/$(EXECUTABLE)"
69 changes: 49 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,44 +108,64 @@ if err != nil {

If you're using the client from another WAG-ified service you should pass in the `ctx` object you get in your server handler. Otherwise you can use `context.Background()`

### Using the Javascript Client from a Node + Express server
### Using the Javascript Client
You can initialize the client by either passing a url or by using [discovery](https://github.com/Clever/discovery-node).

The Javascript client is generated by a fork of [swagger-codegen](https://github.com/clever/swagger-codegen) that adds support for tracing and retries.
It has currently only been tested in Node + Express servers.
```javascript
import * as SampleClientLib from 'sample-client-lib-js';

The first step is initializing the object named `ApiClient`, and setting the URL of where its located:
const sampleClient = new SampleClientLib({address: "https://url_of_your_service:port"}); // Explicit url
// OR
const sampleClient = new SampleClientLib({discovery: true}); // Using discovery
```

```javascript
import * as yourservice from 'yourservice-js';
You may also configure a global timeout for requests when initalizing the client.

const apiClient = new yourservice.ApiClient();
apiClient.basePath = "https://url_of_your_service:port";
```javascript
const sampleClient = new SampleClientLib({discovery: true, timeout: 1000}); // Timeout any requests taking longer than 1 second
```

In a request handler, you can use the `apiClient` object as an argument to construct and call specific APIs.
These specific API objects will be named after the first `tag` you give the operation in your swagger spec.
You may then call methods on the client. Methods support callbacks and promises.

```javascript
// Promises
sampleClient.getBookById("bookID").then((book) => {
// ...
}).catch((err) => {
// ...
});

// Callbacks
sampleClient.getBookById("bookID", (err, book) => {
// ...
});
```

For example, if you have an operation tagged `Infra` with an `operationId` of `healthCheck`, you would call it like this:
You can also pass an optional options argument. This can have the following options
- `timeout` - overide the global timeout for this specific call
- `span` - Pass an opentracing span to instrument with the call - More on this below

```javascript
app.get('/some_route', (req, res) => {
const infraAPI = new yourservice.InfraApi(apiClient, {req}); // you must pass the request context through to the client call
infraAPI.healthCheck().then(function(data) {
res.send('service called successfully!');
}, function(error) {
console.error(error);
});
const options = {
timeout: 5000 // Timeout after 5 seconds
}

sampleClient.getBookById("bookID", options, (err, book) => {
// ...
});
```

Additionally, for the above to work you will need to set up server middleware to place tracing-related metadata on the `req` object.
We currently are testing out LightStep, a service that collects tracing data and displays it in a nice UI:
#### Tracing

To utilize the `span` option above you need to pass an opentracing span into the request. The below
example shows you how to setup an express app to track requests and any calls made via a wag client.

```bash
npm install lightstep-tracer [email protected] --save # >=0.12 contains untested breaking changes to the API
```

```javascript
import * as SampleClientLib from 'sample-client-lib-js';
import * as express from 'express';
import * as Tracer from 'opentracing';
import * as LightStep from 'lightstep-tracer';
Expand All @@ -155,6 +175,8 @@ Tracer.initGlobalTracer(LightStep.tracer({
component_name : 'repo-name',
}));

const sampleClient = new SampleClientLib({discovery: true}); // Using discovery

const app = express();

// Middleware to look for a span from inbound requests
Expand All @@ -177,6 +199,13 @@ app.use((req, res, next) => {

next();
});

app.get("/my-url", (req, res) => {
sampleClient.getBookById("bookID", {span: req.span}, (err, book) => {
// ...
});
});

```

### Custom String Validation
Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
2 changes: 1 addition & 1 deletion client/genclients.go → clients/go/gengo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package goclient

import (
"bytes"
Expand Down
Loading

0 comments on commit d6d072e

Please sign in to comment.