Skip to content

Commit

Permalink
Support for on-premise SwaggerHub Deployments (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
arumoy authored Oct 23, 2023
1 parent b0f3393 commit 48bb08d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,21 @@ swaggerhubDownload {
```

#### Parameters
Parameter | Description | Required | Default
--------- | ----------- | --------- | -------
**`api`** | API name | true | -
**`owner`** | API owner | true | -
**`version`** | API version | true | -
**`outputFile`** | API definition is written to this file | true | -
**`token`** | SwaggerHub API key, required to access private definitions | false | -
**`format`** | API definition format, `json` or `yaml` | false | `json`
**`host`** | URL of SwaggerHub API | false | `api.swaggerhub.com`
**`protocol`** | Protocol for SwaggerHub API,`http` or `https` | false | `https`
**`port`** | Port to access SwaggerHub API| false | `443`
**`oas`** | Version of the OpenApi Specification the definition adheres to | false | `2.0`
**`resolved`** | Download a resolved version of the API definition | false | `false`
Parameter | Description | Required | Default
--------- |----------------------------------------------------------------------------------------------------| --------- | -------
**`api`** | API name | true | -
**`owner`** | API owner | true | -
**`version`** | API version | true | -
**`outputFile`** | API definition is written to this file | true | -
**`token`** | SwaggerHub API key, required to access private definitions | false | -
**`format`** | API definition format, `json` or `yaml` | false | `json`
**`host`** | URL of SwaggerHub API | false | `api.swaggerhub.com`
**`protocol`** | Protocol for SwaggerHub API,`http` or `https` | false | `https`
**`port`** | Port to access SwaggerHub API | false | `443`
**`oas`** | Version of the OpenApi Specification the definition adheres to | false | `2.0`
**`resolved`** | Download a resolved version of the API definition | false | `false`
**`onPremise`** | Uses the API path suffix for on-premise SwaggerHub deployments | false | `false`
**`onPremiseAPISuffix`** | Custom API Suffix path for any future changes in SwaggerHub API pattern for on-premise deployments | false | `/v1`
***

### swaggerhubUpload
Expand Down Expand Up @@ -122,3 +124,5 @@ Parameter | Description | Required | Default
**`host`** | URL of SwaggerHub API | false | `api.swaggerhub.com`
**`protocol`** | Protocol for SwaggerHub API,`http` or `https` | false | `https`
**`port`** | Port to access SwaggerHub API| false | `443`
**`onPremise`** | Uses the API path suffix for on-premise SwaggerHub deployments | false | `false`
**`onPremiseAPISuffix`** | Custom API Suffix path for any future changes in SwaggerHub API pattern for on-premise deployments | false | `/v1`
11 changes: 10 additions & 1 deletion src/main/java/io/swagger/swaggerhub/client/SwaggerHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ public class SwaggerHubClient {
private final int port;
private final String token;
private final String protocol;
private final Boolean onPremise;
private final String onPremiseAPISuffix;
private static final String APIS = "apis";


public SwaggerHubClient(String host, int port, String protocol, String token) {
public SwaggerHubClient(String host, int port, String protocol, String token) { // TODO: Worth removing after due consideration
this(host, port, protocol, token, false, null);
}

public SwaggerHubClient(String host, int port, String protocol, String token, Boolean onPremise, String onPremiseAPISuffix) {
client = new OkHttpClient();
this.host = host;
this.port = port;
this.protocol = protocol;
this.token = token;
this.onPremise = onPremise;
this.onPremiseAPISuffix = onPremiseAPISuffix;
}

public String getDefinition(SwaggerHubRequest swaggerHubRequest) throws GradleException {
Expand Down Expand Up @@ -104,6 +112,7 @@ private HttpUrl.Builder getBaseUrl(String owner, String api) {
.scheme(protocol)
.host(host)
.port(port)
.addPathSegment(onPremise ? onPremiseAPISuffix : "")
.addPathSegment(APIS)
.addEncodedPathSegment(owner)
.addEncodedPathSegment(api);
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/io/swagger/swaggerhub/tasks/DownloadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class DownloadTask extends DefaultTask {
private Integer port = 443;
private String protocol = "https";
private Boolean resolved = false;
private Boolean onPremise = false;
private String onPremiseAPISuffix = "/v1";

@Input
public String getOwner() {
Expand Down Expand Up @@ -130,13 +132,32 @@ public void setResolved(Boolean resolved) {
this.resolved = resolved;
}

@Input
@Optional
public Boolean getOnPremise() {
return onPremise;
}

public void setOnPremise(Boolean onPremise) {
this.onPremise = onPremise;
}

@Input
@Optional
public String getOnPremiseAPISuffix() {
return onPremiseAPISuffix;
}

public void setOnPremiseAPISuffix(String onPremiseAPISuffix) {
this.onPremiseAPISuffix = onPremiseAPISuffix;
}

@TaskAction
public void downloadDefinition() throws GradleException {
SwaggerHubClient swaggerHubClient = new SwaggerHubClient(host, port, protocol, token);
SwaggerHubClient swaggerHubClient = new SwaggerHubClient(host, port, protocol, token, onPremise, onPremiseAPISuffix);

LOGGER.info("Downloading from {}: api: {}, owner: {}, version: {}, format: {}, resolved: {}, outputFile: {}",
host, api, owner, version, format, resolved, outputFile);
LOGGER.info("Downloading from {}: api: {}, owner: {}, version: {}, format: {}, resolved: {}, outputFile: {}, onPremise: {}, onPremiseAPISuffix: {}",
host, api, owner, version, format, resolved, outputFile, onPremise, onPremiseAPISuffix);

SwaggerHubRequest swaggerHubRequest = new SwaggerHubRequest.Builder(api, owner, version)
.format(format)
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/io/swagger/swaggerhub/tasks/UploadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class UploadTask extends DefaultTask {
private String protocol = "https";
private String format = "json";
private String oas = "2.0";
private Boolean onPremise = false;
private String onPremiseAPISuffix = "/v1";

private SwaggerHubClient swaggerHubClient;

Expand Down Expand Up @@ -137,13 +139,34 @@ public void setFormat(String format) {

public void setOas(String oas) { this.oas = oas; }

@Input
@Optional
public Boolean getOnPremise() {
return onPremise;
}

public void setOnPremise(Boolean onPremise) {
this.onPremise = onPremise;
}

@Input
@Optional
public String getOnPremiseAPISuffix() {
return onPremiseAPISuffix;
}

public void setOnPremiseAPISuffix(String onPremiseAPISuffix) {
this.onPremiseAPISuffix = onPremiseAPISuffix;
}

@TaskAction
public void uploadDefinition() throws GradleException {

swaggerHubClient = new SwaggerHubClient(host, port, protocol, token);
swaggerHubClient = new SwaggerHubClient(host, port, protocol, token, onPremise, onPremiseAPISuffix);

LOGGER.info("Uploading to {}: api: {}, owner: {}, version: {}, inputFile: {}, format: {}, isPrivate: {}, oas: {} ",
host, api, owner, version, inputFile, format, isPrivate, oas);
LOGGER.info("Uploading to {}: api: {}, owner: {}, version: {}, inputFile: {}, format: {}, isPrivate: {}, oas: {}," +
" onPremise: {}, onPremiseAPISuffix: {} ",
host, api, owner, version, inputFile, format, isPrivate, oas, onPremise, onPremiseAPISuffix);

try {
String content = new String(Files.readAllBytes(Paths.get(inputFile)), StandardCharsets.UTF_8);
Expand Down

0 comments on commit 48bb08d

Please sign in to comment.