@@ -50,6 +50,22 @@ type RelationApiBody struct {
50
50
OdataID string `json:"@odata.id"`
51
51
}
52
52
53
+ func getEntityDefinition (ctx context.Context , client * DataRecordClient , environmentUrl string , entityLogicalName string ) * EntityDefinitionsDto {
54
+ entityDefinitionApiUrl := & url.URL {
55
+ Scheme : "https" ,
56
+ Host : strings .TrimPrefix (environmentUrl , "https://" ),
57
+ Path : fmt .Sprintf ("/api/data/v9.2/EntityDefinitions(LogicalName='%s')" , entityLogicalName ),
58
+ Fragment : "$select=PrimaryIdAttribute,LogicalCollectionName" ,
59
+ }
60
+ entityDefinition := EntityDefinitionsDto {}
61
+ _ , err := client .Api .Execute (ctx , "GET" , entityDefinitionApiUrl .String (), nil , nil , []int {http .StatusOK }, & entityDefinition )
62
+ if err != nil {
63
+ return nil
64
+ }
65
+
66
+ return & entityDefinition
67
+ }
68
+
53
69
func (client * DataRecordClient ) GetEnvironmentUrlById (ctx context.Context , environmentId string ) (string , error ) {
54
70
env , err := client .getEnvironment (ctx , environmentId )
55
71
if err != nil {
@@ -92,19 +108,9 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
92
108
if nestedMap , ok := value .(map [string ]interface {}); ok {
93
109
delete (columns , key )
94
110
if len (nestedMap ) > 0 {
95
- entityLogicalName := nestedMap ["entity_logical_name" ]
111
+ entityLogicalName := nestedMap ["entity_logical_name" ].( string )
96
112
97
- entityDefinitionApiUrl := & url.URL {
98
- Scheme : "https" ,
99
- Host : strings .TrimPrefix (environmentUrl , "https://" ),
100
- Path : fmt .Sprintf ("/api/data/v9.2/EntityDefinitions(LogicalName='%s')" , entityLogicalName ),
101
- Fragment : "$select=PrimaryIdAttribute,LogicalCollectionName" ,
102
- }
103
- entityDefinition := EntityDefinitionsDto {}
104
- _ , err = client .Api .Execute (ctx , "GET" , entityDefinitionApiUrl .String (), nil , nil , []int {http .StatusOK }, & entityDefinition )
105
- if err != nil {
106
- return & result , err
107
- }
113
+ entityDefinition := getEntityDefinition (ctx , client , environmentUrl , entityLogicalName )
108
114
109
115
columns [
fmt .
Sprintf (
"%[email protected] " ,
key )]
= fmt .
Sprintf (
"%s/api/data/v9.2/%s(%s)" ,
environmentUrl ,
entityDefinition .
LogicalCollectionName ,
nestedMap [
"data_record_id" ])
110
116
}
@@ -115,18 +121,7 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
115
121
}
116
122
}
117
123
118
- apiUrl := & url.URL {
119
- Scheme : "https" ,
120
- Host : strings .TrimPrefix (environmentUrl , "https://" ),
121
- Path : fmt .Sprintf ("/api/data/v9.2/EntityDefinitions(LogicalName='%s')" , tableName ),
122
- Fragment : "$select=PrimaryIdAttribute,LogicalCollectionName" ,
123
- }
124
-
125
- entityDefinition := EntityDefinitionsDto {}
126
- _ , err = client .Api .Execute (ctx , "GET" , apiUrl .String (), nil , nil , []int {http .StatusOK }, & entityDefinition )
127
- if err != nil {
128
- return & result , err
129
- }
124
+ entityDefinition := getEntityDefinition (ctx , client , environmentUrl , tableName )
130
125
131
126
method := "POST"
132
127
path := fmt .Sprintf ("/api/data/v9.2/%s" , entityDefinition .LogicalCollectionName )
@@ -139,7 +134,7 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
139
134
path = fmt .Sprintf ("%s(%s)" , path , recordId )
140
135
}
141
136
142
- apiUrl = & url.URL {
137
+ apiUrl : = & url.URL {
143
138
Scheme : "https" ,
144
139
Host : strings .TrimPrefix (environmentUrl , "https://" ),
145
140
Path : path ,
@@ -181,19 +176,9 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
181
176
for _ , nestedItem := range nestedMapList {
182
177
nestedMap := nestedItem .(map [string ]interface {})
183
178
184
- entityLogicalName := nestedMap ["entity_logical_name" ]
179
+ entityLogicalName := nestedMap ["entity_logical_name" ].( string )
185
180
186
- entityDefinitionApiUrl := & url.URL {
187
- Scheme : "https" ,
188
- Host : strings .TrimPrefix (environmentUrl , "https://" ),
189
- Path : fmt .Sprintf ("/api/data/v9.2/EntityDefinitions(LogicalName='%s')" , entityLogicalName ),
190
- Fragment : "$select=PrimaryIdAttribute,LogicalCollectionName" ,
191
- }
192
- entityDefinition := EntityDefinitionsDto {}
193
- _ , err = client .Api .Execute (ctx , "GET" , entityDefinitionApiUrl .String (), nil , nil , []int {http .StatusOK }, & entityDefinition )
194
- if err != nil {
195
- return & result , err
196
- }
181
+ entityDefinition := getEntityDefinition (ctx , client , environmentUrl , entityLogicalName )
197
182
198
183
relation := RelationApiBody {
199
184
OdataID : fmt .Sprintf ("%s/api/data/v9.2/%s(%s)" , environmentUrl , entityDefinition .LogicalCollectionName , nestedMap ["data_record_id" ]),
@@ -227,20 +212,9 @@ func (client *DataRecordClient) DeleteDataRecord(ctx context.Context, recordId s
227
212
}
228
213
}
229
214
230
- apiUrl := & url.URL {
231
- Scheme : "https" ,
232
- Host : strings .TrimPrefix (environmentUrl , "https://" ),
233
- Path : fmt .Sprintf ("/api/data/v9.2/EntityDefinitions(LogicalName='%s')" , tableName ),
234
- Fragment : "$select=PrimaryIdAttribute,LogicalCollectionName" ,
235
- }
236
-
237
- tableEntityDefinition := EntityDefinitionsDto {}
238
- _ , err = client .Api .Execute (ctx , "GET" , apiUrl .String (), nil , nil , []int {http .StatusOK }, & tableEntityDefinition )
239
- if err != nil {
240
- return err
241
- }
215
+ tableEntityDefinition := getEntityDefinition (ctx , client , environmentUrl , tableName )
242
216
243
- apiUrl = & url.URL {
217
+ apiUrl : = & url.URL {
244
218
Scheme : "https" ,
245
219
Host : strings .TrimPrefix (environmentUrl , "https://" ),
246
220
Path : fmt .Sprintf ("/api/data/v9.2/%s(%s)" , tableEntityDefinition .LogicalCollectionName , recordId ),
@@ -262,19 +236,9 @@ func (client *DataRecordClient) DeleteDataRecord(ctx context.Context, recordId s
262
236
for _ , nestedItem := range nestedMapList {
263
237
nestedMap := nestedItem .(map [string ]interface {})
264
238
265
- entityLogicalName := nestedMap ["entity_logical_name" ]
239
+ entityLogicalName := nestedMap ["entity_logical_name" ].( string )
266
240
267
- entityDefinitionApiUrl := & url.URL {
268
- Scheme : "https" ,
269
- Host : strings .TrimPrefix (environmentUrl , "https://" ),
270
- Path : fmt .Sprintf ("/api/data/v9.2/EntityDefinitions(LogicalName='%s')" , entityLogicalName ),
271
- Fragment : "$select=PrimaryIdAttribute,LogicalCollectionName" ,
272
- }
273
- columnEntityDefinition := EntityDefinitionsDto {}
274
- _ , err = client .Api .Execute (ctx , "GET" , entityDefinitionApiUrl .String (), nil , nil , []int {http .StatusOK }, & columnEntityDefinition )
275
- if err != nil {
276
- return err
277
- }
241
+ columnEntityDefinition := getEntityDefinition (ctx , client , environmentUrl , entityLogicalName )
278
242
279
243
apiUrl = & url.URL {
280
244
Scheme : "https" ,
0 commit comments