Skip to content

Commit dad3fb8

Browse files
Add in C# HttpClient code generation (#453)
* Initial scaffolding * Work on unit tests * Fix eslint errors * Work on body generation * Work on body parsing * Work on passing tests * Work on unit tests * Finish form calls * Fix httpclient .net dependency * Fix DELETE test * Switch RestSharp to using 3.1 * Add converage * Added more options * Add test coverage * Fix unit tests * Add more test coverage * Added even more test coverage * Add coverage for csharpify and graphql request * Add test coverate for raw and file * Added more test coverage * Added more test coverage * Cleanup up urlencode convertion * Fix early exit from formdata request * Finished test coverage on parseRequest * Delete formdataFileCollection.json * Small fixes * Add EOF's * Update to .NET 6 * Remove duplicate --------- Co-authored-by: Akshay Deo <akshay@akshaydeo.com>
1 parent 3b85e31 commit dad3fb8

26 files changed

+2902
-10
lines changed

README.md

+19-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ List of supported code generators:
1818
| Language | Variant |
1919
|-----------|---------------|
2020
| C | libcurl |
21+
| C# | HttpClient |
2122
| C# | RestSharp |
2223
| cURL | cURL |
2324
| Dart | http |
@@ -49,16 +50,24 @@ List of supported code generators:
4950
| Swift | URLSession |
5051
## Table of contents
5152

52-
1. [Getting Started](#getting-started)
53-
2. [Prerequisite](#prerequisite)
54-
3. [Usage](#usage)
55-
1. [Using postman code generators as a Library](#using-postman-code-generators-as-a-library)
56-
4. [Development](#development)
57-
1. [Installing Dependencies](#installing-dependencies)
58-
2. [Testing](#testing)
59-
3. [Packaging](#packaging)
60-
7. [Contributing](#contributing)
61-
8. [License](#license)
53+
- [postman-code-generators ![Build Status](https://travis-ci.com/postmanlabs/postman-code-generators)](#postman-code-generators-)
54+
- [Table of contents](#table-of-contents)
55+
- [Getting Started](#getting-started)
56+
- [Prerequisite](#prerequisite)
57+
- [Usage](#usage)
58+
- [Using postman-code-generators as a Library](#using-postman-code-generators-as-a-library)
59+
- [getLanguageList](#getlanguagelist)
60+
- [Example:](#example)
61+
- [getOptions](#getoptions)
62+
- [Example:](#example-1)
63+
- [convert](#convert)
64+
- [Example:](#example-2)
65+
- [Development](#development)
66+
- [Installing dependencies](#installing-dependencies)
67+
- [Testing](#testing)
68+
- [Packaging](#packaging)
69+
- [Contributing](#contributing)
70+
- [License](#license)
6271

6372
## Getting Started
6473
To install postman-code-generators as your dependency

codegens/csharp-httpclient/.gitignore

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.DS_Store
2+
# Logs
3+
logs
4+
*.log
5+
npm-debug.log*
6+
yarn-debug.log*
7+
yarn-error.log*
8+
9+
# Coverage directory used by tools like istanbul
10+
.coverage
11+
12+
# node-waf configuration
13+
.lock-wscript
14+
15+
16+
# Dependency directories
17+
node_modules/
18+
jspm_packages/
19+
20+
# Test Project directories
21+
testProject/
22+
23+
# Typescript v1 declaration files
24+
typings/
25+
26+
# Optional npm cache directory
27+
.npm
28+
29+
# Optional eslint cache
30+
.eslintcache
31+
32+
# Optional REPL history
33+
.node_repl_history
34+
35+
# Output of 'npm pack'
36+
*.tgz
37+
38+
# Yarn Integrity file
39+
.yarn-integrity
40+
41+
# dotenv environment variables file
42+
.env
43+
44+
out/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"tags": {
3+
"allowUnknownTags": true,
4+
"dictionaries": ["jsdoc", "closure"]
5+
},
6+
"source": {
7+
"include": ["lib"],
8+
"includePattern": ".+\\.js(doc)?$",
9+
"excludePattern": "(^|\\/|\\\\)_"
10+
},
11+
12+
"plugins": [
13+
"plugins/markdown"
14+
],
15+
16+
"templates": {
17+
"cleverLinks": false,
18+
"monospaceLinks": false,
19+
"highlightTutorialCode" : true
20+
},
21+
22+
"opts": {
23+
"template": "./node_modules/postman-jsdoc-theme",
24+
"encoding": "utf8",
25+
"destination": "./out/docs",
26+
"recurse": true,
27+
"readme": "README.md"
28+
},
29+
30+
"markdown": {
31+
"parser": "gfm",
32+
"hardwrap": false
33+
}
34+
}
35+

codegens/csharp-httpclient/.npmignore

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
### NPM Specific: Disregard recursive project files
2+
### ===============================================
3+
/.editorconfig
4+
/.gitmodules
5+
/test
6+
7+
### Borrowed from .gitignore
8+
### ========================
9+
10+
# Logs
11+
logs
12+
*.log
13+
npm-debug.log*
14+
yarn-debug.log*
15+
yarn-error.log*
16+
17+
# Runtime data
18+
pids
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Prevent IDE stuff
24+
.idea
25+
.vscode
26+
*.sublime-*
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
lib-cov
30+
31+
# Coverage directory used by tools like istanbul
32+
.coverage
33+
34+
# nyc test coverage
35+
.nyc_output
36+
37+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
38+
.grunt
39+
40+
# Bower dependency directory (https://bower.io/)
41+
bower_components
42+
43+
# node-waf configuration
44+
.lock-wscript
45+
46+
# Compiled binary addons (http://nodejs.org/api/addons.html)
47+
build/Release
48+
49+
# Dependency directories
50+
node_modules/
51+
jspm_packages/
52+
53+
# Typescript v1 declaration files
54+
typings/
55+
56+
# Optional npm cache directory
57+
.npm
58+
59+
# Optional eslint cache
60+
.eslintcache
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
74+
snippet.swift
75+
76+
out/

codegens/csharp-httpclient/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
> Converts Postman-SDK Request into code snippet for .
3+
4+
#### Prerequisites
5+
To run Code-Gen, ensure that you have NodeJS >= v8. A copy of the NodeJS installable can be downloaded from https://nodejs.org/en/download/package-manager.
6+
7+
## Using the Module
8+
The module will expose an object which will have property `convert` which is the function for converting the Postman-SDK request to swift code snippet.
9+
10+
### convert function
11+
Convert function takes three parameters
12+
13+
* `request` - Postman-SDK Request Object
14+
15+
* `options` - options is an object which hsa following properties
16+
* `indentType` - String denoting type of indentation for code snippet. eg: 'Space', 'Tab'
17+
* `indentCount` - The number of indentation characters to add per code level
18+
* `trimRequestBody` - Whether or not request body fields should be trimmed
19+
20+
* `callback` - callback function with first parameter as error and second parameter as string for code snippet
21+
22+
##### Example:
23+
```js
24+
var request = new sdk.Request('www.google.com'), //using postman sdk to create request
25+
options = {
26+
indentCount: 3,
27+
indentType: 'Space',
28+
requestTimeout: 200,
29+
trimRequestBody: true
30+
};
31+
convert(request, options, function(error, snippet) {
32+
if (error) {
33+
// handle error
34+
}
35+
// handle snippet
36+
});
37+
```
38+
### Guidelines for using generated snippet
39+
40+
* Since Postman-SDK Request object doesn't provide complete path of the file, it needs to be manually inserted in case of uploading a file.
41+
42+
* This module doesn't support cookies.

codegens/csharp-httpclient/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
*
3+
*/
4+
class CodeBuilder {
5+
/**
6+
*
7+
* @param {Number} indentSize
8+
* @param {String} indentCharacter
9+
*/
10+
constructor (indentSize, indentCharacter) {
11+
this.indentSize = indentSize;
12+
this.indentCharacter = indentCharacter;
13+
this.currentIndentCount = 0;
14+
this.snippet = '';
15+
this.usings = [];
16+
this.newLineChar = '\n';
17+
}
18+
19+
/**
20+
*
21+
* @param {String} using - The namespace that is needed to be imported in order
22+
* for the code to be able to be built on it's own
23+
*/
24+
addUsing (using) {
25+
this.usings.push(using);
26+
}
27+
28+
/**
29+
*
30+
* @param {String} line - the line to be appended
31+
*/
32+
appendLine (line) {
33+
this.snippet += this.indentation + line + this.newLineChar;
34+
}
35+
36+
/**
37+
*
38+
* @param {String[]} lines
39+
*/
40+
appendLines (lines) {
41+
lines.forEach((l) => {
42+
this.appendLine(l);
43+
});
44+
}
45+
46+
/**
47+
*
48+
* @param {String} body - the value to append to the running block of code
49+
*/
50+
append (body) {
51+
this.snippet += body;
52+
}
53+
54+
/**
55+
*
56+
* @param {String} line - the line to be appened followed by the start of a new block
57+
*/
58+
appendBlock (line) {
59+
this.snippet += this.indentation + line + this.newLineChar +
60+
this.indentation + '{' + this.newLineChar;
61+
this.currentIndentCount++;
62+
}
63+
64+
/**
65+
*
66+
* @param {String} extra - value to add after closing brace
67+
*/
68+
endBlock (extra) {
69+
if (!extra) {
70+
extra = '';
71+
}
72+
this.currentIndentCount--;
73+
this.snippet += this.indentation + '}' + extra + this.newLineChar;
74+
}
75+
76+
/**
77+
*
78+
* @param {Boolean} addUsings - Whether to actually append the usings at the top
79+
*
80+
* @returns {String} the full block of code
81+
*/
82+
build (addUsings) {
83+
if (addUsings) {
84+
var builder = new CodeBuilder(this.indentSize, this.indentCharacter);
85+
this.uniqueUsings().forEach((using) => {
86+
builder.appendLine(`using ${using};`);
87+
});
88+
builder.append(this.snippet);
89+
return builder.build(false);
90+
}
91+
92+
return this.snippet;
93+
}
94+
95+
/**
96+
* @returns {Array<String>} the unique usings
97+
*/
98+
uniqueUsings () {
99+
var arr = [],
100+
i = 0;
101+
for (i = 0; i < this.usings.length; i++) {
102+
if (!arr.includes(this.usings[i])) {
103+
arr.push(this.usings[i]);
104+
}
105+
}
106+
return arr.sort();
107+
}
108+
109+
get indentation () {
110+
return this.indentCharacter.repeat(this.indentSize * this.currentIndentCount);
111+
}
112+
}
113+
114+
module.exports = CodeBuilder;

0 commit comments

Comments
 (0)