Skip to content

Commit

Permalink
feat: codegen swift
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Aug 14, 2024
1 parent d861145 commit 2b5a397
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 3 deletions.
2 changes: 2 additions & 0 deletions swift/AlibabacloudOpenApi.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Pod::Spec.new do |spec|
spec.dependency 'TeaUtils', '~> 1.0.6'
spec.dependency 'AlibabaCloudCredentials', '~> 1.0.1'
spec.dependency 'AlibabaCloudOpenApiUtil', '~> 1.0.1'
spec.dependency 'AlibabacloudGatewaySPI', '~> 0.0.1'
spec.dependency 'DarabonbaXML', '~> 1.0.1'

spec.swift_version='5.6'
end
2 changes: 2 additions & 0 deletions swift/Cartfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ github aliyun/tea-swift ~> 1.0.2
github alibabacloud-sdk-swift/tea-utils ~> 1.0.6
github aliyun/credentials-swift ~> 1.0.1
github alibabacloud-sdk-swift/openapi-util ~> 1.0.1
github alibabacloud-sdk-swift/alibabacloud-gateway-spi ~> 0.0.1
github alibabacloud-sdk-swift/tea-xml ~> 1.0.1
6 changes: 5 additions & 1 deletion swift/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ let package = Package(
.package(url: "https://github.com/alibabacloud-sdk-swift/tea-utils", from: "1.0.6"),
.package(url: "https://github.com/aliyun/credentials-swift", from: "1.0.1"),
.package(url: "https://github.com/alibabacloud-sdk-swift/openapi-util", from: "1.0.1"),
.package(url: "https://github.com/alibabacloud-sdk-swift/alibabacloud-gateway-spi", from: "0.0.1"),
.package(url: "https://github.com/alibabacloud-sdk-swift/tea-xml", from: "1.0.1"),
],
targets: [
.target(
Expand All @@ -27,7 +29,9 @@ let package = Package(
.product(name: "Tea", package: "tea-swift"),
.product(name: "TeaUtils", package: "tea-utils"),
.product(name: "AlibabaCloudCredentials", package: "credentials-swift"),
.product(name: "AlibabaCloudOpenApiUtil", package: "openapi-util")
.product(name: "AlibabaCloudOpenApiUtil", package: "openapi-util"),
.product(name: "AlibabacloudGatewaySPI", package: "alibabacloud-gateway-spi"),
.product(name: "DarabonbaXML", package: "tea-xml")
]),
],
swiftLanguageVersions: [.v5]
Expand Down
155 changes: 153 additions & 2 deletions swift/Sources/AlibabacloudOpenApi/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Tea
import TeaUtils
import AlibabaCloudCredentials
import AlibabaCloudOpenApiUtil
import AlibabacloudGatewaySPI
import DarabonbaXML

open class Client {
public var _endpoint: String?
Expand Down Expand Up @@ -53,6 +55,8 @@ open class Client {

public var _headers: [String: String]?

public var _spi: AlibabacloudGatewaySPI.Client?

public var _globalParameters: GlobalParameters?

public var _key: String?
Expand Down Expand Up @@ -822,8 +826,16 @@ open class Client {
_lastRequest = _request
var _response: Tea.TeaResponse = try await Tea.TeaCore.doAction(_request, _runtime)
if (TeaUtils.Client.is4xx(_response.statusCode) || TeaUtils.Client.is5xx(_response.statusCode)) {
var _res: Any = try await TeaUtils.Client.readAsJSON(_response.body)
var err: [String: Any] = try TeaUtils.Client.assertAsMap(_res)
var err: [String: Any] = [:]
if (!TeaUtils.Client.isUnset(_response.headers["content-type"]) && TeaUtils.Client.equalString(_response.headers["content-type"], "text/xml;charset=utf-8")) {
var _str: String = try await TeaUtils.Client.readAsString(_response.body)
var respMap: [String: Any] = DarabonbaXML.Client.parseXml(_str, nil)
err = try TeaUtils.Client.assertAsMap(respMap["Error"])
}
else {
var _res: Any = try await TeaUtils.Client.readAsJSON(_response.body)
err = try TeaUtils.Client.assertAsMap(_res)
}
err["statusCode"] = _response.statusCode
throw Tea.ReuqestError([
"code": Client.defaultAny(err["Code"], err["code"]),
Expand Down Expand Up @@ -894,6 +906,141 @@ open class Client {
throw Tea.UnretryableError(_lastRequest, _lastException)
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public func execute(_ params: Params, _ request: OpenApiRequest, _ runtime: TeaUtils.RuntimeOptions) async throws -> [String: Any] {
try params.validate()
try request.validate()
try runtime.validate()
var _runtime: [String: Any] = [
"timeouted": "retry",
"key": TeaUtils.Client.defaultString(runtime.key, self._key),
"cert": TeaUtils.Client.defaultString(runtime.cert, self._cert),
"ca": TeaUtils.Client.defaultString(runtime.ca, self._ca),
"readTimeout": TeaUtils.Client.defaultNumber(runtime.readTimeout, self._readTimeout),
"connectTimeout": TeaUtils.Client.defaultNumber(runtime.connectTimeout, self._connectTimeout),
"httpProxy": TeaUtils.Client.defaultString(runtime.httpProxy, self._httpProxy),
"httpsProxy": TeaUtils.Client.defaultString(runtime.httpsProxy, self._httpsProxy),
"noProxy": TeaUtils.Client.defaultString(runtime.noProxy, self._noProxy),
"socks5Proxy": TeaUtils.Client.defaultString(runtime.socks5Proxy, self._socks5Proxy),
"socks5NetWork": TeaUtils.Client.defaultString(runtime.socks5NetWork, self._socks5NetWork),
"maxIdleConns": TeaUtils.Client.defaultNumber(runtime.maxIdleConns, self._maxIdleConns),
"retry": [
"retryable": Client.defaultAny(runtime.autoretry, false),
"maxAttempts": TeaUtils.Client.defaultNumber(runtime.maxAttempts, 3)
],
"backoff": [
"policy": TeaUtils.Client.defaultString(runtime.backoffPolicy, "no"),
"period": TeaUtils.Client.defaultNumber(runtime.backoffPeriod, 1)
],
"ignoreSSL": Client.defaultAny(runtime.ignoreSSL, false),
"disableHttp2": Client.defaultAny(self._disableHttp2, false)
]
var _lastRequest: Tea.TeaRequest? = nil
var _lastException: Tea.TeaError? = nil
var _now: Int32 = Tea.TeaCore.timeNow()
var _retryTimes: Int32 = 0
while (Tea.TeaCore.allowRetry(_runtime["retry"], _retryTimes, _now)) {
if (_retryTimes > 0) {
var _backoffTime: Int32 = Tea.TeaCore.getBackoffTime(_runtime["backoff"], _retryTimes)
if (_backoffTime > 0) {
Tea.TeaCore.sleep(_backoffTime)
}
}
_retryTimes = _retryTimes + 1
do {
var _request: Tea.TeaRequest = Tea.TeaRequest()
var headers: [String: String] = try getRpcHeaders()
var globalQueries: [String: String] = [:]
var globalHeaders: [String: String] = [:]
if (!TeaUtils.Client.isUnset(self._globalParameters)) {
var globalParams: GlobalParameters = self._globalParameters!
if (!TeaUtils.Client.isUnset(globalParams.queries)) {
globalQueries = globalParams.queries ?? [:]
}
if (!TeaUtils.Client.isUnset(globalParams.headers)) {
globalHeaders = globalParams.headers ?? [:]
}
}
var extendsHeaders: [String: String] = [:]
var extendsQueries: [String: String] = [:]
if (!TeaUtils.Client.isUnset(runtime.extendsParameters)) {
var extendsParameters: TeaUtils.ExtendsParameters = runtime.extendsParameters!
if (!TeaUtils.Client.isUnset(extendsParameters.headers)) {
extendsHeaders = extendsParameters.headers ?? [:]
}
if (!TeaUtils.Client.isUnset(extendsParameters.queries)) {
extendsQueries = extendsParameters.queries ?? [:]
}
}
var requestContext: AlibabacloudGatewaySPI.InterceptorContext.Request = AlibabacloudGatewaySPI.InterceptorContext.Request([
"headers": Tea.TeaConverter.merge([:], globalHeaders, extendsHeaders, request.headers ?? [:], headers),
"query": Tea.TeaConverter.merge([:], globalQueries, extendsQueries, request.query ?? [:]),
"body": request.body!,
"stream": request.stream!,
"hostMap": request.hostMap ?? [:],
"pathname": params.pathname ?? "",
"productId": self._productId ?? "",
"action": params.action ?? "",
"version": params.version ?? "",
"protocol": TeaUtils.Client.defaultString(self._protocol, params.protocol_),
"method": TeaUtils.Client.defaultString(self._method, params.method),
"authType": params.authType ?? "",
"bodyType": params.bodyType ?? "",
"reqBodyType": params.reqBodyType ?? "",
"style": params.style ?? "",
"credential": self._credential!,
"signatureVersion": self._signatureVersion ?? "",
"signatureAlgorithm": self._signatureAlgorithm ?? "",
"userAgent": getUserAgent()
])
var configurationContext: AlibabacloudGatewaySPI.InterceptorContext.Configuration = AlibabacloudGatewaySPI.InterceptorContext.Configuration([
"regionId": self._regionId ?? "",
"endpoint": TeaUtils.Client.defaultString(request.endpointOverride, self._endpoint),
"endpointRule": self._endpointRule ?? "",
"endpointMap": self._endpointMap ?? [:],
"endpointType": self._endpointType ?? "",
"network": self._network ?? "",
"suffix": self._suffix ?? ""
])
var interceptorContext: AlibabacloudGatewaySPI.InterceptorContext = AlibabacloudGatewaySPI.InterceptorContext([
"request": requestContext as! AlibabacloudGatewaySPI.InterceptorContext.Request,
"configuration": configurationContext as! AlibabacloudGatewaySPI.InterceptorContext.Configuration
])
var attributeMap: AlibabacloudGatewaySPI.AttributeMap = AlibabacloudGatewaySPI.AttributeMap([:])
try await self._spi!.modifyConfiguration(interceptorContext as! AlibabacloudGatewaySPI.InterceptorContext, attributeMap as! AlibabacloudGatewaySPI.AttributeMap)
try await self._spi!.modifyRequest(interceptorContext as! AlibabacloudGatewaySPI.InterceptorContext, attributeMap as! AlibabacloudGatewaySPI.AttributeMap)
_request.protocol_ = interceptorContext.request!.protocol_ ?? ""
_request.method = interceptorContext.request!.method ?? ""
_request.pathname = interceptorContext.request!.pathname ?? ""
_request.query = interceptorContext.request!.query ?? [:]
_request.body = interceptorContext.request!.stream!
_request.headers = interceptorContext.request!.headers ?? [:]
_lastRequest = _request
var _response: Tea.TeaResponse = try await Tea.TeaCore.doAction(_request, _runtime)
var responseContext: AlibabacloudGatewaySPI.InterceptorContext.Response = AlibabacloudGatewaySPI.InterceptorContext.Response([
"statusCode": _response.statusCode,
"headers": _response.headers,
"body": _response.body
])
interceptorContext.response = responseContext
try await self._spi!.modifyResponse(interceptorContext as! AlibabacloudGatewaySPI.InterceptorContext, attributeMap as! AlibabacloudGatewaySPI.AttributeMap)
return [
"headers": interceptorContext.response!.headers ?? [:],
"statusCode": interceptorContext.response!.statusCode!,
"body": interceptorContext.response!.deserializedBody!
]
}
catch {
if (Tea.TeaCore.isRetryable(error)) {
_lastException = error as! Tea.RetryableError
continue
}
throw error
}
}
throw Tea.UnretryableError(_lastRequest, _lastException)
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public func callApi(_ params: Params, _ request: OpenApiRequest, _ runtime: TeaUtils.RuntimeOptions) async throws -> [String: Any] {
if (TeaUtils.Client.isUnset(params)) {
Expand Down Expand Up @@ -982,6 +1129,10 @@ open class Client {
}
}

public func setGatewayClient(_ spi: AlibabacloudGatewaySPI.Client) throws -> Void {
self._spi = spi
}

public func setRpcHeaders(_ headers: [String: String]) throws -> Void {
self._headers = headers
}
Expand Down
2 changes: 2 additions & 0 deletions swift/Sources/AlibabacloudOpenApi/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Tea
import TeaUtils
import AlibabaCloudCredentials
import AlibabaCloudOpenApiUtil
import AlibabacloudGatewaySPI
import DarabonbaXML

public class GlobalParameters : Tea.TeaModel {
public var headers: [String: String]?
Expand Down

0 comments on commit 2b5a397

Please sign in to comment.