TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jun 2015 |
Maintained by Tribus Hannes.
This framework aims to simplify the construction of a SOAP request as well as the parsing/mapping of the response into domain objects.
The parsing is based on the GDataXMLNode class that was initially developed by Google. A handy XML parsing class that will come up with nice and strucured results. Actually you'll see that because the result is already parsed you can use the result directly without mapping it into objects.
The mapping features is based on the KeyValueObjectMapping done by dchohfi. Basically an adaption to deal with the result classes of SoapKit.
These examples are mainly taken from the example projects which should make it easy to follow
For this example we take a freely available webservice (WSDL) construct the SKRequest
and send it.
- (void)loadItemsofType:(NSString *)type withinLast:(NSUInteger)days
onCompletion:(void (^)(NSArray *lectures, NSError *error))completion {
SKRequest *request = [[SKRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.foxcentral.net/foxcentral.asmx"] operation:@"GetNewsItems" andNamespaceURL:[NSURL URLWithString:@"http://www.west-wind.com/foxcentral"]];
[request addInputs:@[[SKData dataWithName:@"Days" andIntValue:days],
[SKData dataWithName:@"Provider" andIntValue:0],
[SKData dataWithName:@"Type" andStringValue:type]]
];
SKService *soapService = [[SKService alloc] init];
[soapService performRequest:request onSuccess:^(SKService *soapService, SKData *data) {
NSLog(@"Name: %@",data.name);
NSLog(@"Value: %@",data.stringValue);
} onFailure:^(SKService *soapService, NSError *error) {
NSLog(@"Error: %@",[error localizedDescription]);
}];
}
When you run it by yourself you'll see that the SKData *data
in the result contains just a long string which itself is in XML format and could be parsed.
Again we'll use a freely available webservice (WSDL) which is returning a simple but structured XML that we can use directly.
- (void)loadItemsofIndustryType:(NSString *)type
onCompletion:(void (^)(NSArray *lectures, NSError *error))completion {
SKRequest *request = [[SKRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.webservicex.net/GenericNAICS.asmx"] operation:@"GetNAICSByIndustry" andNamespaceURL:[NSURL URLWithString:@"http://www.webservicex.net/"]];
[request addInput:[SKData dataWithName:@"IndustryName" andStringValue:type]];
SKService *soapService = [[SKService alloc] init];
[soapService performRequest:request onSuccess:^(SKService *soapService, SKData *data) {
SKDataObjectMapping *mapping = [SKDataObjectMapping mapperForClass:[NAICSItem class]];
NSArray *result = [mapping parseArray:[[data childByName:@"NAICSData"] childByName:@"NAICSData"].children];
NSLog(@"Got %lu results",(unsigned long)[result count]);
} onFailure:^(SKService *soapService, NSError *error) {
NSLog(@"Error: %@",[error localizedDescription]);
}];
}
Now the NSArray *result
contains the results as an array of NAICSItem
s. That's in this case a simple domain object class defined as
@interface NAICSItem : NSObject
@property (nonatomic,strong)NSString *NAICSCode;
@property (nonatomic,strong)NSString *Title;
@property (nonatomic,strong)NSString *Country;
@property (nonatomic,strong)NSString *IndustryDescription;
@end
It might be that for some reasons you'll not be able to use the field names from the XML in the domain objects. Well fortunately this problem was already solved in the original mapping project and so it can be used here too
- (void)loadItemsofIndustryType:(NSString *)type
onCompletion:(void (^)(NSArray *lectures, NSError *error))completion {
SKRequest *request = [[SKRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.webservicex.net/GenericNAICS.asmx"] operation:@"GetNAICSByIndustry" andNamespaceURL:[NSURL URLWithString:@"http://www.webservicex.net/"]];
[request addInput:[SKData dataWithName:@"IndustryName" andStringValue:type]];
SKService *soapService = [[SKService alloc] init];
[soapService performRequest:request onSuccess:^(SKService *soapService, SKData *data) {
SKParserConfiguration *configuration = [SKParserConfiguration configuration];
[configuration addObjectMapping:[SKObjectMapping mapKeyPath:@"Title" toAttribute:@"NAICSTitle" onClass:[NAICSItem class]]];
SKDataObjectMapping *mapping = [SKDataObjectMapping mapperForClass:[NAICSItem class]andConfiguration:configuration];
NSArray *result = [mapping parseArray:[[data childByName:@"NAICSData"] childByName:@"NAICSData"].children];
NSLog(@"Got %lu results",(unsigned long)[result count]);
for (id ritem in result) {
if ([ritem isKindOfClass:[NAICSItem class]]) {
[self.textView setText:[NSString stringWithFormat:@"%@ TITLE:%@\n DESCRIPTION:%@\n",self.textView.text, ((NAICSItem *)ritem).NAICSTitle, ((NAICSItem *)ritem).IndustryDescription]];
}
}
} onFailure:^(SKService *soapService, NSError *error) {
NSLog(@"Error: %@",[error localizedDescription]);
}];
}
This means of course that the field in the domain class needs to be changed from Title
to NAICSTitle
...are really welcome. The featureset that is implemented right now is the minimum that I need for my projects and will grow with my personal needs. That's why I decided open source it, to be able to cover more use cases in a shorter amount of time.
Source code of this project is available under the standard MIT license. Please see the license file.