-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmagento2.cfc
More file actions
201 lines (173 loc) · 7.7 KB
/
magento2.cfc
File metadata and controls
201 lines (173 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
component {
public struct function init( string apiKey = '', struct config = { } ) {
var configObj = new lib.config( apiKey, config );
var basePath = getDirectoryFromPath( getMetadata( this ).path ).replace( '\', '/', 'all' );
variables.objectMetadata = loadMetadata( basePath );
variables.httpService = new lib.httpService();
variables.parsers = {
arguments: new lib.parsers.arguments( configObj ),
headers: new lib.parsers.headers( configObj ),
response: new lib.parsers.response( configObj, objectMetadata )
};
for ( var resourcePath in listResources( basePath ) ) {
var resourceParts = resourcePath.listFirst( '.' ).listToArray( '/' );
var parent = this;
for ( var i = 1; i < resourceParts.len(); i++ ) {
if ( !structKeyExists( parent, resourceParts[ i ] ) ) {
parent[ resourceParts[ i ] ] = { };
}
parent = parent[ resourceParts[ i ] ];
}
parent[ resourceParts[ i ] ] = new lib.apiResource( this, configObj, resourceParts.toList( '.' ) );
}
// this[ 'webhooks' ] = new lib.webhooks( parsers.response );
return this;
}
public any function call(
required string resourceName,
required string methodName,
required any argCollection,
required struct methodMetadata,
struct argOverrides = { }
) {
var argumentsType = determineArgumentsType( argCollection, methodMetadata );
var sources = getSources( argCollection, methodMetadata, argumentsType );
var ignoredArgs = [ ];
// Check endpoint
if (!Len(methodMetadata.endpoint)) {
throw(
'No endpoint has been provided.'
);
}
// collect positional args and add to params
if ( argumentsType == 'positional' ) {
var argIndex = 1;
for ( var argName in methodMetadata.positionalArgs ) {
if ( arrayLen( argCollection ) < argIndex ) {
throw(
'`#resourceName#.#methodName#()` missing positional argument `#argName#` at index [#argIndex#]'
);
} else if ( !isSimpleValue( argCollection[ argIndex ] ) ) {
throw(
'`#resourceName#.#methodName#()` positional argument `#argName#` at index [#argIndex#] is not a simple value'
);
} else {
sources.params[ argName ] = argCollection[ argIndex ];
}
argIndex++;
}
} else if ( argumentsType == 'nested' ) {
for ( var argName in methodMetadata.positionalArgs ) {
if ( !structKeyExists( argCollection, argName ) ) {
throw( '`#resourceName#.#methodName#()` missing required argument `#argName#`' );
}
sources.params[ argName ] = argCollection[ argName ];
}
}
sources.params.append( argOverrides, true );
// path
var path = 'https://' & replaceNoCase(methodMetadata.endpoint, 'https://', '') & methodMetadata.path;
for ( var argName in methodMetadata.pathArgs ) {
path = path.replace( '{#argName#}', sources.params[ argName ] );
ignoredArgs.append( argName );
}
// headers
var headerData = parsers.headers.parse( sources.headers, methodMetadata );
ignoredArgs.append( headerData.headerArgNames, true );
// params - Magento API likes payloads to be in JSON, so we're not flattening the parsed params and passing a struct to the makeRequest funciton
var params = '';
if(ListFindNoCase('PUT,POST', methodMetadata.httpmethod) && !methodMetadata.multipart) {
params = parsers.arguments.parse( sources.params, methodMetadata.arguments, ignoredArgs, false );
}
else {
params = parsers.arguments.parse( sources.params, methodMetadata.arguments, ignoredArgs );
}
var requestStart = getTickCount();
var rawResponse = httpService.makeRequest(
methodMetadata.httpmethod,
path,
headerData.headers,
params,
methodMetadata.multipart
);
var response = { };
try {
response[ 'duration' ] = getTickCount() - requestStart;
response[ 'requestId' ] = structKeyExists(rawResponse.responseheader, 'X-Request-Id') ? rawResponse.responseheader[ 'X-Request-Id' ] : '';
response[ 'headers' ] = rawResponse.responseheader;
response[ 'status' ] = listFirst( rawResponse.statuscode, ' ' );
response[ 'content' ] = deserializeJSON( rawResponse.filecontent );
parsers.response.parse( response.content );
}
catch( any e ) {
response[ 'content' ] = rawResponse.fileContent;
}
return response;
}
public string function determineArgumentsType( required any argCollection, required struct methodMetadata ) {
if ( arrayLen( argCollection ) == 0 || structKeyExists( argCollection, '1' ) ) {
return 'positional';
}
var nestedNamedKeys = [ 'params', 'headers' ];
for ( var key in structKeyArray( argCollection ) ) {
if ( !( methodMetadata.positionalArgs.findNoCase( key ) || nestedNamedKeys.findNoCase( key ) ) ) {
return 'flat';
}
}
return 'nested';
}
private any function getSources(
required any argCollection,
required struct methodMetadata,
required string argumentsType
) {
var sourceKeys = [
{
name: 'params',
offset: 1
},
{
name: 'headers',
offset: 2
}
];
var sources = { };
for ( var source in sourceKeys ) {
if ( argumentsType == 'positional' ) {
var sourceIndex = arrayLen( methodMetadata.positionalArgs ) + source.offset;
sources[ source.name ] = arrayLen( argCollection ) >= sourceIndex ? argCollection[ sourceIndex ] : { };
} else if ( argumentsType == 'nested' ) {
sources[ source.name ] = structKeyExists( argCollection, source.name ) ? argCollection[ source.name ] : { };
} else {
sources[ source.name ] = argCollection;
}
}
return sources;
}
private struct function loadMetadata( required string basePath ) {
var metadataPath = basePath & 'metadata/';
var metadata = { };
var jsonFiles = directoryList( metadataPath, true, 'path', '*.json' );
for ( var path in jsonFiles ) {
var metaName = path
.replace( '\', '/', 'all' )
.replace( metadataPath, '' )
.listFirst( '.' )
.replace( '/', '.', 'all' );
metadata[ metaName ] = deserializeJSON( fileRead( path ) );
}
return metadata;
}
private array function listResources( required string basePath ) {
var resourcePath = basePath & 'lib/resources/';
var paths = directoryList( resourcePath, true, 'path', '*.cfc' );
paths = paths.map( function( path ) {
return path.replace( '\', '/', 'all' ).replace( resourcePath, '' );
} )
// Sort to add components located in subfolders after their parents, this way they are added inside the parent component
.sort(function(a, b){
return (ListLen(a, '/') < ListLen(b, '/')) ? -1 : 1;
});
return paths;
}
}