@@ -13,27 +13,19 @@ import Foundation
1313// Construct a URI with options encoded
1414//
1515public  struct  URLBuilder  { 
16-      
16+ 
1717    let  encodedParams ,  url :  String 
1818    static  let  host   =  " https://box.magic.link " 
19- //  static let host = "http://192.168.0.106:3016"
20-     
19+ 
2120    public  let  apiKey :  String 
2221
23-     init ( apiKey:  String ,  customNode:  CustomNodeConfiguration ,  locale:  String )  { 
24-         let  options  =  CustomNodeOptions ( apiKey:  apiKey,  customNode:  customNode,  locale:  locale) 
25-     
26-         let  data  =  try !   JSONEncoder ( ) . encode ( options) 
27-         self . init ( data:  data,  host:  URLBuilder . host,  apiKey:  apiKey) 
28-     } 
29-     
30-     init ( apiKey:  String ,  network:  EthNetwork ,  locale:  String )  { 
31-         let  options  =  EthNetworkOptions ( apiKey:  apiKey,  network:  network,  locale:  locale) 
32-         let  data  =  try !   JSONEncoder ( ) . encode ( options) 
33-         self . init ( data:  data,  host:  URLBuilder . host,  apiKey:  apiKey) 
22+     init ( apiKey:  String ,  customNode:  CustomNodeConfiguration ?   =  nil ,  ethNetwork:  EthNetwork ?   =  nil ,  locale:  String ,  productType:  ProductType )  { 
23+ 
24+         let  data  =  try !   JSONEncoder ( ) . encode ( paramsEncodable ( apiKey:  apiKey,  ethNetwork:  ethNetwork,  customNode:  customNode,  locale:  locale,  productType:  productType) ) 
25+         self . init ( data:  data,  host:  URLBuilder . host,  apiKey:  apiKey,  productType:  productType) 
3426    } 
35-      
36-     private  init ( data:  Data ,  host:  String ,  apiKey:  String )  { 
27+ 
28+     private  init ( data:  Data ,  host:  String ,  apiKey:  String ,  productType :   ProductType )  { 
3729        let  jsonString  =  String ( data:  data,  encoding:  . utf8) !
3830        let  string  =  jsonString. replacingOccurrences ( of:  " \\ " ,  with:  " " ) 
3931        // Encode instantiate option to params
@@ -43,44 +35,83 @@ public struct URLBuilder {
4335    } 
4436
4537    // MARK: - Options structs
46-     // Here, Struct is more preferable here. Even though, it can't inherit from classes
47-     // Using class will create more duplicate code as all the sub-class
48-     // needs to be override encode function to encode additional property
49-     struct  EthNetworkOptions :  Encodable  { 
38+     struct  paramsEncodable :  Encodable  { 
5039        let  API_KEY :  String 
51-         let  host   =  URLBuilder . host
52-         let  sdk   =  " magic-sdk-ios " 
53-         let  ETH_NETWORK :  String 
5440        let  locale :  String 
55-         let  bundleId   =  Bundle . main. bundleIdentifier
56-         init ( apiKey:  String ,  network:  EthNetwork ,  locale:  String )  { 
57-             self . ETH_NETWORK =  network. rawValue
41+         let  customNode :  CustomNodeConfiguration ? 
42+         let  ethNetwork :  EthNetwork ? 
43+         let  productType :  ProductType 
44+         init ( apiKey:  String ,  ethNetwork:  EthNetwork ? ,  customNode:  CustomNodeConfiguration ? ,  locale:  String ,  productType:  ProductType )  { 
45+             self . productType =  productType
46+             self . customNode =  customNode
47+             self . ethNetwork =  ethNetwork
5848            self . API_KEY =  apiKey
5949            self . locale =  locale
6050        } 
61-     } 
6251
63-     struct  CustomNodeOptions :  Encodable  { 
64-         let  API_KEY :  String 
65-         let  host   =  URLBuilder . host
66-         let  sdk   =  " magic-sdk-ios " 
67-         let  ETH_NETWORK :  CustomNodeConfiguration 
68-         let  locale :  String 
69-         let  bundleId   =  Bundle . main. bundleIdentifier
70-         init ( apiKey:  String ,  customNode:  CustomNodeConfiguration ,  locale:  String )  { 
71-             self . ETH_NETWORK =  customNode
72-             self . API_KEY =  apiKey
73-             self . locale =  locale
52+         enum  CodingKeys :  String ,  CodingKey  { 
53+             case  sdk,  bundleId,  API_KEY,  host,  ETH_NETWORK,  ext
54+         } 
55+ 
56+         func  encode( to encoder:  Encoder )  throws  { 
57+             var  container  =  encoder. container ( keyedBy:  CodingKeys . self) 
58+             try   container. encode ( " magic-sdk-ios " ,  forKey:  . sdk) 
59+             try   container. encode ( Bundle . main. bundleIdentifier,  forKey:  . bundleId) 
60+             try   container. encode ( self . API_KEY,  forKey:  . API_KEY) 
61+             try   container. encode ( URLBuilder . host,  forKey:  . host) 
62+ 
63+             /// Network
64+             if  ( customNode !=  nil )  { 
65+                 try   container. encode ( customNode,  forKey:  . ETH_NETWORK) 
66+             } 
67+             if  ( ethNetwork !=  nil )  { 
68+                 try   container. encode ( ethNetwork? . rawValue,  forKey:  . ETH_NETWORK) 
69+             } 
70+ 
71+             try   container. encode ( ExtensionObject ( productType:  productType) ,  forKey:  . ext) 
7472        } 
7573    } 
7674} 
7775
76+ // MARK: -- Network
7877public  struct  CustomNodeConfiguration :  Encodable  { 
7978    let  rpcUrl :  String 
8079    let  chainId :  Int ? 
81-      
80+ 
8281    public  init  ( rpcUrl:  String ,  chainId:  Int ?   =  nil )  { 
8382        self . rpcUrl =  rpcUrl
8483        self . chainId =  chainId
8584    } 
8685} 
86+ 
87+ 
88+ // MARK: -- Extension
89+ struct  ExtensionObject :  Encodable  { 
90+ 
91+     let  productType :  ProductType 
92+ 
93+     init ( productType:  ProductType )  { 
94+         self . productType =  productType
95+     } 
96+ 
97+     enum  CodingKeys :  String ,  CodingKey  { 
98+         case  connect
99+     } 
100+ 
101+     func  encode( to encoder:  Encoder )  throws  { 
102+         var  container  =  encoder. container ( keyedBy:  CodingKeys . self) 
103+ 
104+         switch  productType { 
105+         case  . MC: 
106+             try   container. encode ( MCConfig ( ) ,  forKey:  . connect) 
107+             break 
108+         default : 
109+             break 
110+         } 
111+ 
112+     } 
113+ } 
114+ 
115+ internal  struct  MCConfig :  Encodable  { 
116+     let  mc   =  true 
117+ } 
0 commit comments