mirror of
https://github.com/foss42/apidash.git
synced 2025-06-06 19:38:58 +08:00
Feat/codegen url session (#1)
* ✨ (wip): codegen `URLSession` configs * 📝 (chore): docs update and deps install * ✨ (feat): `URLSession` codegen * ✅ (test): `test/codegen/swift_urlsession_test.dart` --wip
This commit is contained in:

committed by
Deepraj Baidya

parent
4c1b75bf98
commit
f5c49d4ac9
@ -133,6 +133,7 @@ API Dash currently supports API integration code generation for the following la
|
||||
| Rust | `reqwest` | |
|
||||
| Rust | `ureq` | |
|
||||
| Rust | `Actix Client` | |
|
||||
| Swift | `URLSession` | |
|
||||
| Java | `asynchttpclient` | |
|
||||
| Java | `HttpClient` | |
|
||||
| Java | `okhttp3` | |
|
||||
|
@ -1,13 +1,23 @@
|
||||
import 'package:apidash/models/models.dart';
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'package:apidash/models/models.dart';
|
||||
import 'package:apidash/utils/utils.dart' show getNewUuid;
|
||||
|
||||
import 'c/curl.dart';
|
||||
import 'csharp/http_client.dart';
|
||||
import 'csharp/rest_sharp.dart';
|
||||
import 'dart/http.dart';
|
||||
import 'dart/dio.dart';
|
||||
import 'dart/http.dart';
|
||||
import 'go/http.dart';
|
||||
import 'java/async_http_client.dart';
|
||||
import 'java/httpclient.dart';
|
||||
import 'java/okhttp.dart';
|
||||
import 'java/unirest.dart';
|
||||
import 'js/axios.dart';
|
||||
import 'js/fetch.dart';
|
||||
import 'julia/http.dart';
|
||||
import 'kotlin/okhttp.dart';
|
||||
import 'others/curl.dart';
|
||||
import 'others/har.dart';
|
||||
import 'php/curl.dart';
|
||||
import 'php/guzzle.dart';
|
||||
import 'php/http_plug.dart';
|
||||
@ -19,15 +29,7 @@ import 'rust/actix.dart';
|
||||
import 'rust/curl_rust.dart';
|
||||
import 'rust/reqwest.dart';
|
||||
import 'rust/ureq.dart';
|
||||
import 'js/axios.dart';
|
||||
import 'js/fetch.dart';
|
||||
import 'others/har.dart';
|
||||
import 'others/curl.dart';
|
||||
import 'julia/http.dart';
|
||||
import 'java/unirest.dart';
|
||||
import 'java/okhttp.dart';
|
||||
import 'java/async_http_client.dart';
|
||||
import 'java/httpclient.dart';
|
||||
import 'swift/urlsession.dart';
|
||||
|
||||
class Codegen {
|
||||
String? getCode(
|
||||
@ -98,6 +100,8 @@ class Codegen {
|
||||
return RustReqwestCodeGen().getCode(rM);
|
||||
case CodegenLanguage.rustUreq:
|
||||
return RustUreqCodeGen().getCode(rM, boundary: boundary);
|
||||
case CodegenLanguage.swiftUrlsession:
|
||||
return SwiftURLSessionCodeGen().getCode(rM);
|
||||
case CodegenLanguage.phpGuzzle:
|
||||
return PhpGuzzleCodeGen().getCode(rM);
|
||||
case CodegenLanguage.phpCurl:
|
||||
|
94
lib/codegen/swift/urlsession.dart
Normal file
94
lib/codegen/swift/urlsession.dart
Normal file
@ -0,0 +1,94 @@
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'package:apidash/models/models.dart';
|
||||
import 'package:apidash/utils/utils.dart' show getValidRequestUri;
|
||||
import 'package:jinja/jinja.dart' as jj;
|
||||
|
||||
class SwiftURLSessionCodeGen {
|
||||
final String kTemplateStart = """
|
||||
import Foundation
|
||||
|
||||
""";
|
||||
|
||||
final String kTemplateParameters = '''
|
||||
let parameters = "{{parameters}}"
|
||||
let postData = parameters.data(using: .utf8)
|
||||
|
||||
''';
|
||||
|
||||
final String kTemplateRequest = """
|
||||
var request = URLRequest(url: URL(string: "{{url}}")!,timeoutInterval: Double.infinity)
|
||||
request.httpMethod = "{{method}}"
|
||||
|
||||
""";
|
||||
|
||||
final String kTemplateHeaders = """
|
||||
{% for header, value in headers %}
|
||||
request.addValue("{{value}}", forHTTPHeaderField: "{{header}}")
|
||||
{% endfor %}
|
||||
|
||||
""";
|
||||
|
||||
final String kTemplateBody = """
|
||||
request.httpBody = postData
|
||||
|
||||
""";
|
||||
|
||||
final String kTemplateEnd = """
|
||||
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
||||
guard let data = data else {
|
||||
print(String(describing: error))
|
||||
return
|
||||
}
|
||||
print(String(data: data, encoding: .utf8)!)
|
||||
}
|
||||
task.resume()
|
||||
""";
|
||||
|
||||
String? getCode(HttpRequestModel requestModel) {
|
||||
try {
|
||||
String result = kTemplateStart;
|
||||
|
||||
var rec =
|
||||
getValidRequestUri(requestModel.url, requestModel.enabledParams);
|
||||
Uri? uri = rec.$1;
|
||||
|
||||
// if (uri == null) {
|
||||
// throw Exception("Invalid URL");
|
||||
// }
|
||||
|
||||
if (requestModel.hasJsonData || requestModel.hasTextData) {
|
||||
var templateParameters = jj.Template(kTemplateParameters);
|
||||
result += templateParameters.render({
|
||||
"parameters":
|
||||
requestModel.body!.replaceAll('"', '\\"').replaceAll('\n', '\\n')
|
||||
});
|
||||
}
|
||||
|
||||
var templateRequest = jj.Template(kTemplateRequest);
|
||||
result += templateRequest.render({
|
||||
"url": uri.toString(),
|
||||
"method": requestModel.method.name.toUpperCase()
|
||||
});
|
||||
|
||||
var headers = requestModel.enabledHeadersMap;
|
||||
if (requestModel.hasJsonData || requestModel.hasTextData) {
|
||||
headers.putIfAbsent(
|
||||
kHeaderContentType, () => requestModel.bodyContentType.header);
|
||||
}
|
||||
if (headers.isNotEmpty) {
|
||||
var templateHeader = jj.Template(kTemplateHeaders);
|
||||
result += templateHeader.render({"headers": headers});
|
||||
}
|
||||
|
||||
if (requestModel.hasTextData || requestModel.hasJsonData) {
|
||||
result += kTemplateBody;
|
||||
}
|
||||
|
||||
result += kTemplateEnd;
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_fonts/google_fonts.dart';
|
||||
@ -391,6 +392,7 @@ enum CodegenLanguage {
|
||||
rustReqwest("Rust (reqwest)", "rust", "rs"),
|
||||
rustCurl("Rust (curl-rust)", "rust", "rs"),
|
||||
rustUreq("Rust (ureq)", "rust", "rs"),
|
||||
swiftUrlsession("Swift (urlsession)", "swift", "swift"),
|
||||
javaOkHttp("Java (okhttp3)", "java", 'java'),
|
||||
javaAsyncHttpClient("Java (asynchttpclient)", "java", "java"),
|
||||
javaHttpClient("Java (HttpClient)", "java", "java"),
|
||||
|
@ -1576,10 +1576,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: vm_service
|
||||
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
|
||||
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "14.2.5"
|
||||
version: "14.2.4"
|
||||
watcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
0
test/codegen/swift_urlsession_test.dart
Normal file
0
test/codegen/swift_urlsession_test.dart
Normal file
Reference in New Issue
Block a user