Skip to content

Commit

Permalink
Merge pull request #14 from lincode/master
Browse files Browse the repository at this point in the history
增加 PUT
  • Loading branch information
lincode committed Feb 7, 2013
2 parents b40d0f5 + ea17bad commit cdb55ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.h
Expand Up @@ -52,6 +52,11 @@
callback:(DOUReqBlock)block
uploadProgressDelegate:(id<ASIProgressDelegate>)progressDelegate;


- (DOUHttpRequest *)put:(DOUQuery *)query
postBody:(NSString *)body
callback:(DOUReqBlock)block;

- (DOUHttpRequest *)delete:(DOUQuery *)query callback:(DOUReqBlock)block;

#endif
Expand Down
35 changes: 35 additions & 0 deletions DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.m
Expand Up @@ -387,6 +387,41 @@ - (DOUHttpRequest *)post:(DOUQuery *)query
}


- (DOUHttpRequest *)put:(DOUQuery *)query postBody:(NSString *)body callback:(DOUReqBlock)block {
query.apiBaseUrlString = self.apiBaseUrlString;
__block DOUHttpRequest * req = [DOUHttpRequest requestWithQuery:query completionBlock:^{
if (block != NULL) {
block(req);
}
}];

[req setRequestMethod:@"PUT"];
[req addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded; charset=UTF-8"];

if (body && [body length] > 0) {

NSError *error = nil;
GDataXMLElement *element = [[[GDataXMLElement alloc] initWithXMLString:body error:&error] autorelease];
if (!error && element) {
// if body is XML, Content-Type must be application/atom+xml
[req addRequestHeader:@"Content-Type" value:@"application/atom+xml"];
}

NSData *objectData = [body dataUsingEncoding:NSUTF8StringEncoding];
NSString *length = [NSString stringWithFormat:@"%d", [objectData length]];
[req appendPostData:objectData];
[req addRequestHeader:@"Content-Length" value:length];
}
else {
[req addRequestHeader:@"Content-Length" value:@"0"];
}

[req setResponseEncoding:NSUTF8StringEncoding];
[self addRequest:req];
return req;
}


- (DOUHttpRequest *)delete:(DOUQuery *)query callback:(DOUReqBlock)block {

query.apiBaseUrlString = self.apiBaseUrlString;
Expand Down

0 comments on commit cdb55ef

Please sign in to comment.