|
8 | 8 | import io.github.atkawa7.httpsnippet.generators.java.Jsoup; |
9 | 9 | import io.github.atkawa7.httpsnippet.generators.java.OkHttp; |
10 | 10 | import io.github.atkawa7.httpsnippet.generators.java.Unirest; |
| 11 | +import io.github.atkawa7.httpsnippet.generators.javascript.Fetch; |
11 | 12 | import io.github.atkawa7.httpsnippet.generators.javascript.JQuery; |
12 | 13 | import io.github.atkawa7.httpsnippet.generators.javascript.XMLHttpRequest; |
13 | 14 | import io.github.atkawa7.httpsnippet.generators.node.NodeNative; |
|
36 | 37 | * Ruby, C#, Go, OCaml and more! |
37 | 38 | */ |
38 | 39 | public class HttpSnippetCodeGenerator { |
39 | | - private final List<CodeGenerator> codeGenerators; |
40 | | - private final List<Language> languages; |
41 | | - |
42 | | - public HttpSnippetCodeGenerator() { |
43 | | - this(defaultGenerators()); |
44 | | - } |
45 | | - |
46 | | - public HttpSnippetCodeGenerator(@NonNull CodeGenerator... codeGenerators) { |
47 | | - this(Arrays.asList(codeGenerators)); |
48 | | - } |
49 | | - |
50 | | - public HttpSnippetCodeGenerator(@NonNull final List<CodeGenerator> codeGenerators) { |
51 | | - Collections.sort(codeGenerators, Comparator.comparing(o -> o.getDisplayName())); |
52 | | - this.codeGenerators = Collections.unmodifiableList(codeGenerators); |
53 | | - this.languages = Collections.unmodifiableList(Arrays.asList(Language.values())); |
54 | | - } |
55 | | - |
56 | | - private static List<CodeGenerator> defaultGenerators() { |
57 | | - List<CodeGenerator> codeGenerators = new ArrayList<>(); |
58 | | - codeGenerators.add(new OkHttp()); |
59 | | - codeGenerators.add(new Unirest()); |
60 | | - codeGenerators.add(new LibCurl()); |
61 | | - codeGenerators.add(new RestSharp()); |
62 | | - codeGenerators.add(new GoNative()); |
63 | | - codeGenerators.add(new JQuery()); |
64 | | - codeGenerators.add(new XMLHttpRequest()); |
65 | | - codeGenerators.add(new NodeNative()); |
66 | | - codeGenerators.add(new NodeRequest()); |
67 | | - codeGenerators.add(new NodeUnirest()); |
68 | | - codeGenerators.add(new ObjNSURLSession()); |
69 | | - codeGenerators.add(new Python3Native()); |
70 | | - codeGenerators.add(new PythonRequests()); |
71 | | - codeGenerators.add(new RubyNative()); |
72 | | - codeGenerators.add(new Curl()); |
73 | | - codeGenerators.add(new CljHttp()); |
74 | | - codeGenerators.add(new PowerShell()); |
75 | | - codeGenerators.add(new Jsoup()); |
76 | | - codeGenerators.add(new Swift()); |
77 | | - return codeGenerators; |
78 | | - } |
79 | | - |
80 | | - /** |
81 | | - * @param harRequest The object contains detailed info about performed request. |
82 | | - * @param language The target programming language |
83 | | - * @param client The target client for the target programming language |
84 | | - * @return The http snippet for a target. The target is searched from a list of available targets |
85 | | - * {@link #findGenerator(String, String) findGenerator} using language and client |
86 | | - * @throws Exception throws exception |
87 | | - */ |
88 | | - public HttpSnippet snippet( |
89 | | - @NonNull final HarRequest harRequest, |
90 | | - @NonNull final String language, |
91 | | - @NonNull final String client) |
92 | | - throws Exception { |
93 | | - CodeGenerator codeGenerator = this.findGenerator(language, client); |
94 | | - return this.convert(harRequest, codeGenerator); |
95 | | - } |
96 | | - |
97 | | - /** |
98 | | - * @param harRequest The object contains detailed info about performed request. |
99 | | - * @param language The target programming language |
100 | | - * @return The http snippet for a code generator. Finds {@link #findLanguage(String) language} and |
101 | | - * uses the {@link Language#getDefaultClient() default client} for a language to search |
102 | | - * through the list of available code generators. |
103 | | - * @throws Exception throws Exception |
104 | | - */ |
105 | | - public HttpSnippet snippet(@NonNull final HarRequest harRequest, @NonNull final String language) |
106 | | - throws Exception { |
107 | | - Language lang = this.findLanguage(language); |
108 | | - return this.snippet(harRequest, lang); |
109 | | - } |
110 | | - |
111 | | - /** |
112 | | - * @param harRequest The object contains detailed info about performed request. |
113 | | - * @param language The target programming language |
114 | | - * @param client The target client for the target programming language |
115 | | - * @return The http snippet for a target. The target is searched from a list of available targets |
116 | | - * {@link #findGenerator(String, String) findGenerator} using language and client |
117 | | - * @throws Exception throws Exception |
118 | | - */ |
119 | | - public HttpSnippet snippet( |
120 | | - @NonNull final HarRequest harRequest, |
121 | | - @NonNull final Language language, |
122 | | - @NonNull final Client client) |
123 | | - throws Exception { |
124 | | - CodeGenerator codeGenerator = this.findGenerator(language, client); |
125 | | - return this.convert(harRequest, codeGenerator); |
126 | | - } |
127 | | - |
128 | | - /** |
129 | | - * @param harRequest The object contains detailed info about performed request. |
130 | | - * @param language The target programming language |
131 | | - * @return The http snippet for a code generator. Uses the {@link Language#getDefaultClient() |
132 | | - * default client} for a language to search through the list of available code generators. |
133 | | - * @throws Exception throws Exception |
134 | | - */ |
135 | | - public HttpSnippet snippet(@NonNull final HarRequest harRequest, @NonNull final Language language) |
136 | | - throws Exception { |
137 | | - Client client = language.getDefaultClient(); |
138 | | - CodeGenerator codeGenerator = this.findGenerator(language, client); |
139 | | - return this.convert(harRequest, codeGenerator); |
140 | | - } |
141 | | - |
142 | | - /** |
143 | | - * @param harRequest The object contains detailed info about performed request. |
144 | | - * @return The list of http snippets using generators |
145 | | - * @throws Exception when fails to convert post data to json |
146 | | - */ |
147 | | - public List<HttpSnippet> snippets(@NonNull final HarRequest harRequest) throws Exception { |
148 | | - CodeRequest codeRequest = CodeRequest.newCodeRequest(harRequest); |
149 | | - |
150 | | - List<HttpSnippet> httpSnippets = new ArrayList<>(codeGenerators.size()); |
151 | | - for (CodeGenerator codeGenerator : codeGenerators) { |
152 | | - |
153 | | - httpSnippets.add(this.convert(codeRequest, codeGenerator)); |
154 | | - } |
155 | | - return httpSnippets; |
156 | | - } |
157 | | - |
158 | | - private HttpSnippet convert( |
159 | | - @NonNull final HarRequest harRequest, @NonNull final CodeGenerator codeGenerator) |
160 | | - throws Exception { |
161 | | - return this.convert(CodeRequest.newCodeRequest(harRequest), codeGenerator); |
162 | | - } |
163 | | - |
164 | | - private HttpSnippet convert( |
165 | | - @NonNull final CodeRequest codeRequest, @NonNull final CodeGenerator codeGenerator) |
166 | | - throws Exception { |
167 | | - return HttpSnippet.builder() |
168 | | - .client(codeGenerator.getClient()) |
169 | | - .displayName(codeGenerator.getDisplayName()) |
170 | | - .language(codeGenerator.getLanguage()) |
171 | | - .code(codeGenerator.generateCode(codeRequest)) |
172 | | - .build(); |
173 | | - } |
174 | | - |
175 | | - private CodeGenerator findGenerator( |
176 | | - @NonNull final Language language, @NonNull final Client client) throws Exception { |
177 | | - return this.findGenerator(language.getTitle(), client.getTitle()); |
178 | | - } |
179 | | - |
180 | | - private CodeGenerator findGenerator(@NonNull final String language, @NonNull final String client) |
181 | | - throws Exception { |
182 | | - return this.codeGenerators.stream() |
183 | | - .filter( |
184 | | - t -> |
185 | | - t.getClient().getTitle().equalsIgnoreCase(client) |
186 | | - && t.getLanguage().getTitle().equalsIgnoreCase(language)) |
187 | | - .findFirst() |
188 | | - .orElseThrow( |
189 | | - () -> |
190 | | - new Exception( |
191 | | - String.format("CodeGenerator (%s, %s) not supported", client, language))); |
192 | | - } |
193 | | - |
194 | | - private Language findLanguage(@NonNull String name) throws Exception { |
195 | | - return this.languages.stream() |
196 | | - .filter(l -> l.getTitle().equalsIgnoreCase(name)) |
197 | | - .findFirst() |
198 | | - .orElseThrow(() -> new Exception(String.format("Language (%s) not supported", name))); |
199 | | - } |
| 40 | + private final List<CodeGenerator> codeGenerators; |
| 41 | + private final List<Language> languages; |
| 42 | + |
| 43 | + public HttpSnippetCodeGenerator() { |
| 44 | + this(defaultGenerators()); |
| 45 | + } |
| 46 | + |
| 47 | + public HttpSnippetCodeGenerator(@NonNull CodeGenerator... codeGenerators) { |
| 48 | + this(Arrays.asList(codeGenerators)); |
| 49 | + } |
| 50 | + |
| 51 | + public HttpSnippetCodeGenerator(@NonNull final List<CodeGenerator> codeGenerators) { |
| 52 | + Collections.sort(codeGenerators, Comparator.comparing(o -> o.getDisplayName())); |
| 53 | + this.codeGenerators = Collections.unmodifiableList(codeGenerators); |
| 54 | + this.languages = Collections.unmodifiableList(Arrays.asList(Language.values())); |
| 55 | + } |
| 56 | + |
| 57 | + private static List<CodeGenerator> defaultGenerators() { |
| 58 | + List<CodeGenerator> codeGenerators = new ArrayList<>(); |
| 59 | + codeGenerators.add(new OkHttp()); |
| 60 | + codeGenerators.add(new Unirest()); |
| 61 | + codeGenerators.add(new LibCurl()); |
| 62 | + codeGenerators.add(new RestSharp()); |
| 63 | + codeGenerators.add(new GoNative()); |
| 64 | + codeGenerators.add(new JQuery()); |
| 65 | + codeGenerators.add(new XMLHttpRequest()); |
| 66 | + codeGenerators.add(new NodeNative()); |
| 67 | + codeGenerators.add(new NodeRequest()); |
| 68 | + codeGenerators.add(new NodeUnirest()); |
| 69 | + codeGenerators.add(new Python3Native()); |
| 70 | + codeGenerators.add(new PythonRequests()); |
| 71 | + codeGenerators.add(new RubyNative()); |
| 72 | + codeGenerators.add(new Curl()); |
| 73 | + codeGenerators.add(new CljHttp()); |
| 74 | + codeGenerators.add(new PowerShell()); |
| 75 | + codeGenerators.add(new Jsoup()); |
| 76 | + codeGenerators.add(new Fetch()); |
| 77 | + codeGenerators.add(new Swift()); |
| 78 | + codeGenerators.add(new ObjNSURLSession()); |
| 79 | + return codeGenerators; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param harRequest The object contains detailed info about performed request. |
| 84 | + * @param language The target programming language |
| 85 | + * @param client The target client for the target programming language |
| 86 | + * @return The http snippet for a target. The target is searched from a list of available targets |
| 87 | + * {@link #findGenerator(String, String) findGenerator} using language and client |
| 88 | + * @throws Exception throws exception |
| 89 | + */ |
| 90 | + public HttpSnippet snippet( |
| 91 | + @NonNull final HarRequest harRequest, |
| 92 | + @NonNull final String language, |
| 93 | + @NonNull final String client) |
| 94 | + throws Exception { |
| 95 | + CodeGenerator codeGenerator = this.findGenerator(language, client); |
| 96 | + return this.convert(harRequest, codeGenerator); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param harRequest The object contains detailed info about performed request. |
| 101 | + * @param language The target programming language |
| 102 | + * @return The http snippet for a code generator. Finds {@link #findLanguage(String) language} and |
| 103 | + * uses the {@link Language#getDefaultClient() default client} for a language to search |
| 104 | + * through the list of available code generators. |
| 105 | + * @throws Exception throws Exception |
| 106 | + */ |
| 107 | + public HttpSnippet snippet(@NonNull final HarRequest harRequest, @NonNull final String language) |
| 108 | + throws Exception { |
| 109 | + Language lang = this.findLanguage(language); |
| 110 | + return this.snippet(harRequest, lang); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @param harRequest The object contains detailed info about performed request. |
| 115 | + * @param language The target programming language |
| 116 | + * @param client The target client for the target programming language |
| 117 | + * @return The http snippet for a target. The target is searched from a list of available targets |
| 118 | + * {@link #findGenerator(String, String) findGenerator} using language and client |
| 119 | + * @throws Exception throws Exception |
| 120 | + */ |
| 121 | + public HttpSnippet snippet( |
| 122 | + @NonNull final HarRequest harRequest, |
| 123 | + @NonNull final Language language, |
| 124 | + @NonNull final Client client) |
| 125 | + throws Exception { |
| 126 | + CodeGenerator codeGenerator = this.findGenerator(language, client); |
| 127 | + return this.convert(harRequest, codeGenerator); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param harRequest The object contains detailed info about performed request. |
| 132 | + * @param language The target programming language |
| 133 | + * @return The http snippet for a code generator. Uses the {@link Language#getDefaultClient() |
| 134 | + * default client} for a language to search through the list of available code generators. |
| 135 | + * @throws Exception throws Exception |
| 136 | + */ |
| 137 | + public HttpSnippet snippet(@NonNull final HarRequest harRequest, @NonNull final Language language) |
| 138 | + throws Exception { |
| 139 | + Client client = language.getDefaultClient(); |
| 140 | + CodeGenerator codeGenerator = this.findGenerator(language, client); |
| 141 | + return this.convert(harRequest, codeGenerator); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @param harRequest The object contains detailed info about performed request. |
| 146 | + * @return The list of http snippets using generators |
| 147 | + * @throws Exception when fails to convert post data to json |
| 148 | + */ |
| 149 | + public List<HttpSnippet> snippets(@NonNull final HarRequest harRequest) throws Exception { |
| 150 | + CodeRequest codeRequest = CodeRequest.newCodeRequest(harRequest); |
| 151 | + |
| 152 | + List<HttpSnippet> httpSnippets = new ArrayList<>(codeGenerators.size()); |
| 153 | + for (CodeGenerator codeGenerator : codeGenerators) { |
| 154 | + |
| 155 | + httpSnippets.add(this.convert(codeRequest, codeGenerator)); |
| 156 | + } |
| 157 | + return httpSnippets; |
| 158 | + } |
| 159 | + |
| 160 | + private HttpSnippet convert( |
| 161 | + @NonNull final HarRequest harRequest, @NonNull final CodeGenerator codeGenerator) |
| 162 | + throws Exception { |
| 163 | + return this.convert(CodeRequest.newCodeRequest(harRequest), codeGenerator); |
| 164 | + } |
| 165 | + |
| 166 | + private HttpSnippet convert( |
| 167 | + @NonNull final CodeRequest codeRequest, @NonNull final CodeGenerator codeGenerator) |
| 168 | + throws Exception { |
| 169 | + return HttpSnippet.builder() |
| 170 | + .client(codeGenerator.getClient()) |
| 171 | + .displayName(codeGenerator.getDisplayName()) |
| 172 | + .language(codeGenerator.getLanguage()) |
| 173 | + .code(codeGenerator.generateCode(codeRequest)) |
| 174 | + .build(); |
| 175 | + } |
| 176 | + |
| 177 | + private CodeGenerator findGenerator( |
| 178 | + @NonNull final Language language, @NonNull final Client client) throws Exception { |
| 179 | + return this.findGenerator(language.getTitle(), client.getTitle()); |
| 180 | + } |
| 181 | + |
| 182 | + private CodeGenerator findGenerator(@NonNull final String language, @NonNull final String client) |
| 183 | + throws Exception { |
| 184 | + return this.codeGenerators.stream() |
| 185 | + .filter( |
| 186 | + t -> |
| 187 | + t.getClient().getTitle().equalsIgnoreCase(client) |
| 188 | + && t.getLanguage().getTitle().equalsIgnoreCase(language)) |
| 189 | + .findFirst() |
| 190 | + .orElseThrow( |
| 191 | + () -> |
| 192 | + new Exception( |
| 193 | + String.format("CodeGenerator (%s, %s) not supported", client, language))); |
| 194 | + } |
| 195 | + |
| 196 | + private Language findLanguage(@NonNull String name) throws Exception { |
| 197 | + return this.languages.stream() |
| 198 | + .filter(l -> l.getTitle().equalsIgnoreCase(name)) |
| 199 | + .findFirst() |
| 200 | + .orElseThrow(() -> new Exception(String.format("Language (%s) not supported", name))); |
| 201 | + } |
200 | 202 | } |
0 commit comments