Skip to content

Commit

Permalink
docs: update readme and add --base-uri option
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Aug 30, 2024
1 parent dbba26e commit 393e1bc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Simple utility CLI for importing data into SwaggerHub Explore.
> `import-postman-collection` Import Postman Collection (v2.1) from a file into SwaggerHub Explore
>
> `import-insomnia-collection` Import Insomnia Collection (v4) from a file into SwaggerHub Explore
>
> `import-pact-file` Import a Pact file (v2/v3/v4) into SwaggerHub Explore (HTTP interactions only)
### Prerequisites
You will need the following:
Expand Down Expand Up @@ -295,6 +297,47 @@ From SwaggerHub Explore, navigate to your browser development tools, locate the
> - Authorization - only Basic and Bearer Token variants are supported

### Running the `import-pact-file` command

**Command Options**
```
_____ _ ____ _ _
| ____| __ __ _ __ | | ___ _ __ ___ / ___| | | (_)
| _| \ \/ / | '_ \ | | / _ \ | '__| / _ \ | | | | | |
| |___ > < | |_) | | | | (_) | | | | __/ _ | |___ | | | |
|_____| /_/\_\ | .__/ |_| \___/ |_| \___| (_) \____| |_| |_|
|_|
```
**Description:**
> Import a Pact file (v2/v3/v4) into SwaggerHub Explore (HTTP interactions only)
**Usage:**
> Explore.CLI import-pact-file [options]
**Options:**
> `-ec`, `--explore-cookie` <explore-cookie> (REQUIRED) A valid and active SwaggerHub Explore session cookie
> `-fp`, `--file-path` <file-path> (REQUIRED) The path to the Insomnia collection
> `-b`, `--base-uri` <base-uri> The base url to use for all imported files
> `-v`, `--verbose` Include verbose output during processing
> `-?`, `-h`, `--help` Show help and usage information
**Note** - the format for SwaggerHub Explore cookies is as follows: `"cookie-name=cookie-value; cookie-name=cookie-value"`

>Example: `"SESSION=5a0a2e2f-97c6-4405-b72a-299fa8ce07c8; XSRF-TOKEN=3310cb20-2ec1-4655-b1e3-4ab76a2ac2c8"`
> **Notes:**
> - Compatible with Valid Pact v2 / v3 / v4 specification files
> - Users are advised to provide the required base url when importing pact files with `--base-uri` / `-b`, to the required server.
> Pact files do not contain this information
> - Currently only supports HTTP interactions.
> - V3 message based pacts are unsupported
> - V4 interactions other than synchronous/http will be ignored
>
## More Information on SwaggerHub Explore
- For SwaggerHub Explore info, see - https://swagger.io/tools/swaggerhub-explore/
Expand Down
12 changes: 6 additions & 6 deletions src/Explore.Cli/PactMappingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static string getPactVersion(string json)
}


public static Connection MapPactV2InteractionToExploreConnection(PactV2.Interaction pactInteraction)
public static Connection MapPactV2InteractionToExploreConnection(PactV2.Interaction pactInteraction, string url = "")
{
return new Connection()
{
Expand All @@ -70,7 +70,7 @@ public static Connection MapPactV2InteractionToExploreConnection(PactV2.Interact
{
new Server()
{
Url = ""
Url = url
}
},
Paths = CreatePactV2PathsDictionary(pactInteraction.Request),
Expand All @@ -84,7 +84,7 @@ public static Connection MapPactV2InteractionToExploreConnection(PactV2.Interact
},
};
}
public static Connection MapPactV3InteractionToExploreConnection(PactV3.Interaction pactInteraction)
public static Connection MapPactV3InteractionToExploreConnection(PactV3.Interaction pactInteraction, string url = "")
{
return new Connection()
{
Expand All @@ -98,7 +98,7 @@ public static Connection MapPactV3InteractionToExploreConnection(PactV3.Interact
{
new Server()
{
Url = ""
Url = url
}
},
Paths = CreatePactV3PathsDictionary(pactInteraction.Request),
Expand All @@ -113,7 +113,7 @@ public static Connection MapPactV3InteractionToExploreConnection(PactV3.Interact
};
}

public static Connection MapPactV4InteractionToExploreConnection(PactV4.Interaction pactInteraction)
public static Connection MapPactV4InteractionToExploreConnection(PactV4.Interaction pactInteraction, string url = "")
{
return new Connection()
{
Expand All @@ -127,7 +127,7 @@ public static Connection MapPactV4InteractionToExploreConnection(PactV4.Interact
{
new Server()
{
Url = ""
Url = url
}
},
Paths = CreatePactV4PathsDictionary(pactInteraction.Request),
Expand Down
20 changes: 12 additions & 8 deletions src/Explore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public static async Task<int> Main(string[] args)
var exportFileName = new Option<string>(name: "--export-name", description: "The name of the file to export") { IsRequired = false };
exportFileName.AddAlias("-en");

// should we scope this to the import-pact-file command?
var baseUri = new Option<string>(name: "--base-uri", description: "The base url to use for all imported files") { IsRequired = false };
baseUri.AddAlias("-b");

var names = new Option<string>(name: "--names", description: "The names of the spaces to export or import") { IsRequired = false };
names.AddAlias("-n");

Expand Down Expand Up @@ -64,12 +68,12 @@ public static async Task<int> Main(string[] args)
importInsomniaCollectionCommand.SetHandler(async (ec, fp, v) =>
{ await ImportInsomniaCollection(ec, fp, v); }, exploreCookie, importFilePath, verbose);

var importPactFileCommand = new Command("import-pact-file") { exploreCookie, importFilePath, verbose };
importPactFileCommand.Description = "Import a Pact file into SwaggerHub Explore";
var importPactFileCommand = new Command("import-pact-file") { exploreCookie, importFilePath, baseUri, verbose };
importPactFileCommand.Description = "Import a Pact file (v2/v3/v4) into SwaggerHub Explore (HTTP interactions only)";
rootCommand.Add(importPactFileCommand);

importPactFileCommand.SetHandler(async (ec, fp, v) =>
{ await ImportPactFile(ec, fp, v); }, exploreCookie, importFilePath, verbose);
importPactFileCommand.SetHandler(async (ec, fp, b, v) =>
{ await ImportPactFile(ec, fp, b, v); }, exploreCookie, importFilePath, baseUri, verbose);

AnsiConsole.Write(new FigletText("Explore.Cli").Color(new Color(133, 234, 45)));

Expand Down Expand Up @@ -284,7 +288,7 @@ internal static async Task ImportPostmanCollection(string exploreCookie, string
}

}
internal static async Task ImportPactFile(string exploreCookie, string filePath, bool? verboseOutput)
internal static async Task ImportPactFile(string exploreCookie, string filePath, string pactBaseUri, bool? verboseOutput)
{
//check file existence and read permissions
try
Expand Down Expand Up @@ -444,7 +448,7 @@ internal static async Task ImportPactFile(string exploreCookie, string filePath,
if (apiResponse.StatusCode == HttpStatusCode.Created)
{
var createdApiResponse = apiResponse.Content.ReadFromJsonAsync<ApiResponse>();
var connectionRequestBody = JsonSerializer.Serialize(PactMappingHelper.MapPactV2InteractionToExploreConnection(interaction));
var connectionRequestBody = JsonSerializer.Serialize(PactMappingHelper.MapPactV2InteractionToExploreConnection(interaction, pactBaseUri));
var connectionContent = new StringContent(connectionRequestBody, Encoding.UTF8, "application/json");

// //now let's do the work and import the connection
Expand Down Expand Up @@ -564,7 +568,7 @@ internal static async Task ImportPactFile(string exploreCookie, string filePath,
if (apiResponse.StatusCode == HttpStatusCode.Created)
{
var createdApiResponse = apiResponse.Content.ReadFromJsonAsync<ApiResponse>();
var connectionRequestBody = JsonSerializer.Serialize(PactMappingHelper.MapPactV3InteractionToExploreConnection(interaction));
var connectionRequestBody = JsonSerializer.Serialize(PactMappingHelper.MapPactV3InteractionToExploreConnection(interaction, pactBaseUri));
var connectionContent = new StringContent(connectionRequestBody, Encoding.UTF8, "application/json");

// //now let's do the work and import the connection
Expand Down Expand Up @@ -684,7 +688,7 @@ internal static async Task ImportPactFile(string exploreCookie, string filePath,
if (apiResponse.StatusCode == HttpStatusCode.Created)
{
var createdApiResponse = apiResponse.Content.ReadFromJsonAsync<ApiResponse>();
var connectionRequestBody = JsonSerializer.Serialize(PactMappingHelper.MapPactV4InteractionToExploreConnection(interaction));
var connectionRequestBody = JsonSerializer.Serialize(PactMappingHelper.MapPactV4InteractionToExploreConnection(interaction, pactBaseUri));
var connectionContent = new StringContent(connectionRequestBody, Encoding.UTF8, "application/json");

// //now let's do the work and import the connection
Expand Down

0 comments on commit 393e1bc

Please sign in to comment.