• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperwallet / hyperwallet-ios-sdk / 15826401626

21 May 2025 02:36PM UTC coverage: 96.705%. Remained the same
15826401626

push

github

web-flow
Initial commit for CodeQL (#152)

* Initial commit for CodeQL

* Updating language

1937 of 2003 relevant lines covered (96.7%)

18.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.31
/Sources/TransactionType.swift
1
//
2
// Copyright 2018 - Present Hyperwallet
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5
// and associated documentation files (the "Software"), to deal in the Software without restriction,
6
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
7
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in all copies or
11
// substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18

19
import Foundation
20

21
/// Transaction type definitions.
22
///
23
/// - rest: Represents a REST transaction.
24
/// - graphQl: Represents a GraphQL transaction.
25
internal enum TransactionType {
26
    case rest
27
    case graphQl
28

29
    /// Creates URLRequest depending on transaction type
30
    ///
31
    /// - Parameters:
32
    ///   - configuration: Configuration
33
    ///   - method: HTTPMethod
34
    ///   - path: urlPath
35
    ///   - query: urlQuery
36
    ///   - httpBody: <Body>
37
    /// - Returns: URLRequest
38
    func createRequest<Body>(_ configuration: Configuration,
39
                             method: HTTPMethod,
40
                             urlPath path: String,
41
                             urlQuery query: [String: String]? = nil,
42
                             httpBody: Body?) throws -> URLRequest where Body: Encodable {
101✔
43
        switch self {
101✔
44
        case .graphQl:
101✔
45
            return try createGraphQlRequest(configuration,
16✔
46
                                            method: method,
16✔
47
                                            urlPath: path,
16✔
48
                                            httpBody: httpBody)
16✔
49

101✔
50
        case .rest:
101✔
51
            return try createRestRequest(configuration,
85✔
52
                                         method: method,
85✔
53
                                         urlPath: path,
85✔
54
                                         urlQuery: query,
85✔
55
                                         httpBody: httpBody)
85✔
56
        }
101✔
57
    }
101✔
58

59
    private func createRestRequest<Body>(_ configuration: Configuration,
60
                                         method: HTTPMethod,
61
                                         urlPath path: String,
62
                                         urlQuery query: [String: String]? = nil,
63
                                         httpBody: Body?) throws -> URLRequest where Body: Encodable {
85✔
64
        let formattedPath = String(format: path, configuration.userToken)
85✔
65
        let baseURL = URL(string: configuration.restUrl + formattedPath)
85✔
66
        // let baseURL = url(configuration, path)
85✔
67
        guard let url = addQueryIfRequired(baseURL, query) else {
85✔
68
            throw ErrorTypeHelper.invalidUrl()
2✔
69
        }
83✔
70
        var request = URLRequest(url: url)
83✔
71
        request.addValue("Bearer " + configuration.authorization, forHTTPHeaderField: "Authorization")
83✔
72
        request.httpMethod = method.rawValue
83✔
73
        if httpBody != nil, method == .post || method == .put {
83✔
74
            let encoder = JSONEncoder()
44✔
75
            let data = try? encoder.encode(httpBody)
44✔
76
            request.httpBody = data
44✔
77
        }
44✔
78
        return request
83✔
79
    }
85✔
80

81
    private func createGraphQlRequest<Body>(_ configuration: Configuration,
82
                                            method: HTTPMethod,
83
                                            urlPath path: String,
84
                                            httpBody: Body?) throws -> URLRequest where Body: Encodable {
16✔
85
        guard let baseURL = URL(string: configuration.graphQlUrl) else {
16✔
86
            throw ErrorTypeHelper.invalidUrl()
×
87
        }
16✔
88
        var request = URLRequest(url: baseURL)
16✔
89
        request.addValue("Bearer " + configuration.authorization, forHTTPHeaderField: "Authorization")
16✔
90
        request.httpMethod = method.rawValue
16✔
91
        if let httpBody = httpBody, let graphQl = httpBody as? GraphQlQuery {
16✔
92
            let graphQlQuery = graphQl.toGraphQl(userToken: configuration.userToken)
15✔
93
            request.httpBody = graphQlQuery.data(using: .utf8)
15✔
94
        }
15✔
95

16✔
96
        return request
16✔
97
    }
16✔
98

99
    private func addQueryIfRequired(_ baseUrl: URL?, _ query: [String: String]?) -> URL? {
85✔
100
        guard let baseUrl = baseUrl else {
85✔
101
            return nil
2✔
102
        }
83✔
103

83✔
104
        if let urlQuery = query {
83✔
105
            var urlComponent = URLComponents(url: baseUrl, resolvingAgainstBaseURL: false)
25✔
106
            urlComponent?.queryItems = urlQuery.map { URLQueryItem(name: $0, value: $1) }
108✔
107
            return urlComponent?.url
25✔
108
        }
58✔
109
        return baseUrl
58✔
110
    }
85✔
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc