mirror of
https://github.com/doublecmd/doublecmd.git
synced 2026-06-21 09:58:13 +00:00
UPD: MacCloud step-41: refactor http and util
This commit is contained in:
parent
63b392d46c
commit
04418aad3c
5 changed files with 44 additions and 43 deletions
|
|
@ -76,6 +76,7 @@
|
|||
<Unit>
|
||||
<Filename Value="drivers/dropbox/udropboxclient.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="uDropBoxClient"/>
|
||||
</Unit>
|
||||
<Unit>
|
||||
<Filename Value="drivers/root/ucloudrootdriver.pas"/>
|
||||
|
|
|
|||
|
|
@ -723,7 +723,7 @@ var
|
|||
http: TMiniHttpClient;
|
||||
httpResult: TMiniHttpResult;
|
||||
dropBoxResult: TDropBoxResult;
|
||||
body: String;
|
||||
body: NSString;
|
||||
begin
|
||||
try
|
||||
body:= TJsonUtil.dumps( ['path', _path] );
|
||||
|
|
@ -755,7 +755,7 @@ var
|
|||
http: TMiniHttpClient;
|
||||
httpResult: TMiniHttpResult;
|
||||
dropBoxResult: TDropBoxResult;
|
||||
body: String;
|
||||
body: NSString;
|
||||
begin
|
||||
try
|
||||
body:= TJsonUtil.dumps( ['cursor', _cursor] );
|
||||
|
|
@ -863,14 +863,14 @@ end;
|
|||
procedure TDropBoxDownloadSession.download;
|
||||
var
|
||||
http: TMiniHttpClient;
|
||||
argJsonString: String;
|
||||
argJsonString: NSString;
|
||||
dropBoxResult: TDropBoxResult;
|
||||
begin
|
||||
try
|
||||
argJsonString:= TJsonUtil.dumps( ['path', _serverPath], True );
|
||||
http:= TMiniHttpClient.Create;
|
||||
_authSession.setAuthHeader( http );
|
||||
http.addHeader( DropBoxConst.HEADER.ARG, argJsonString );
|
||||
http.addHeader( NSSTR(DropBoxConst.HEADER.ARG), argJsonString );
|
||||
|
||||
dropBoxResult:= TDropBoxResult.Create;
|
||||
dropBoxResult.httpResult:= http.download( DropBoxConst.URI.DOWNLOAD, _localPath, _callback );
|
||||
|
|
@ -898,14 +898,14 @@ end;
|
|||
procedure TDropBoxUploadSession.uploadSmall;
|
||||
var
|
||||
http: TMiniHttpClient;
|
||||
argJsonString: String;
|
||||
argJsonString: NSString;
|
||||
dropBoxResult: TDropBoxResult;
|
||||
begin
|
||||
try
|
||||
argJsonString:= TJsonUtil.dumps( ['path',_serverPath, 'mode','overwrite'], True );
|
||||
http:= TMiniHttpClient.Create;
|
||||
_authSession.setAuthHeader( http );
|
||||
http.addHeader( DropBoxConst.HEADER.ARG, argJsonString );
|
||||
http.addHeader( NSSTR(DropBoxConst.HEADER.ARG), argJsonString );
|
||||
|
||||
dropBoxResult:= TDropBoxResult.Create;
|
||||
dropBoxResult.httpResult:= http.upload( DropBoxConst.URI.UPLOAD_SMALL, _localPath, _callback );
|
||||
|
|
@ -952,7 +952,7 @@ var
|
|||
end;
|
||||
|
||||
procedure uploadAppend( range: NSRange );
|
||||
function getArgJsonString: String;
|
||||
function getArgJsonString: NSString;
|
||||
var
|
||||
jsonCursor: NSMutableDictionary;
|
||||
begin
|
||||
|
|
@ -969,7 +969,7 @@ var
|
|||
try
|
||||
http:= TMiniHttpClient.Create;
|
||||
_authSession.setAuthHeader( http );
|
||||
http.addHeader( DropBoxConst.HEADER.ARG, getArgJsonString );
|
||||
http.addHeader( NSSTR(DropBoxConst.HEADER.ARG), getArgJsonString );
|
||||
|
||||
dropBoxResult:= TDropBoxResult.Create;
|
||||
dropBoxResult.httpResult:= http.uploadRange(
|
||||
|
|
@ -987,7 +987,7 @@ var
|
|||
end;
|
||||
|
||||
procedure uploadFinish;
|
||||
function getArgJsonString: String;
|
||||
function getArgJsonString: NSString;
|
||||
var
|
||||
jsonCursor: NSMutableDictionary;
|
||||
jsonCommit: NSMutableDictionary;
|
||||
|
|
@ -1012,7 +1012,7 @@ var
|
|||
try
|
||||
http:= TMiniHttpClient.Create;
|
||||
_authSession.setAuthHeader( http );
|
||||
http.addHeader( DropBoxConst.HEADER.ARG, getArgJsonString );
|
||||
http.addHeader( NSSTR(DropBoxConst.HEADER.ARG), getArgJsonString );
|
||||
http.setContentType( HttpConst.ContentType.OctetStream );
|
||||
|
||||
dropBoxResult:= TDropBoxResult.Create;
|
||||
|
|
@ -1063,7 +1063,7 @@ procedure TDropBoxCreateFolderSession.createFolder;
|
|||
var
|
||||
http: TMiniHttpClient;
|
||||
dropBoxResult: TDropBoxResult;
|
||||
body: String;
|
||||
body: NSString;
|
||||
begin
|
||||
try
|
||||
body:= TJsonUtil.dumps( ['path', _path] );
|
||||
|
|
@ -1095,7 +1095,7 @@ end;
|
|||
procedure TDropBoxDeleteSession.delete;
|
||||
var
|
||||
http: TMiniHttpClient;
|
||||
body: String;
|
||||
body: NSString;
|
||||
dropBoxResult: TDropBoxResult;
|
||||
begin
|
||||
try
|
||||
|
|
@ -1131,7 +1131,7 @@ var
|
|||
uri: String;
|
||||
http: TMiniHttpClient;
|
||||
dropBoxResult: TDropBoxResult;
|
||||
body: String;
|
||||
body: NSString;
|
||||
begin
|
||||
try
|
||||
http:= TMiniHttpClient.Create;
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ type
|
|||
public
|
||||
procedure addHeader( const name: NSString; const value: NSString ); overload;
|
||||
procedure addHeader( const name: String; const value: String ); overload;
|
||||
procedure setBody( const body: String );
|
||||
procedure setBody( const body: NSString ); overload;
|
||||
procedure setContentType( const contentType: NSString );
|
||||
procedure setContentLength( const length: Integer );
|
||||
protected
|
||||
|
|
@ -458,13 +458,12 @@ begin
|
|||
self.addHeader( NSSTR(name), StringToNSString(value) );
|
||||
end;
|
||||
|
||||
procedure TMiniHttpClient.setBody(const body: String);
|
||||
procedure TMiniHttpClient.setBody(const body: NSString);
|
||||
var
|
||||
bodyData: NSData;
|
||||
begin
|
||||
bodyData:= StringToNSString(body).dataUsingEncoding( NSUTF8StringEncoding );
|
||||
bodyData:= body.dataUsingEncoding( NSUTF8StringEncoding );
|
||||
_request.setHTTPBody( bodyData );
|
||||
_request.setHTTPMethod( HttpConst.Method.POST );
|
||||
self.setContentLength( bodyData.length );
|
||||
end;
|
||||
|
||||
|
|
@ -506,26 +505,13 @@ function TMiniHttpClient.doPost(
|
|||
var
|
||||
url: NSURL;
|
||||
delegate: TMiniHttpConnectionDataDelegate;
|
||||
|
||||
function getBody: String;
|
||||
var
|
||||
components: NSURLComponents;
|
||||
cocoaItems: NSArray;
|
||||
begin
|
||||
components:= NSURLComponents.new;
|
||||
cocoaItems:= THttpClientUtil.toQueryItems( lclItems );
|
||||
components.setQueryItems( cocoaItems );
|
||||
Result:= components.query.UTF8String;
|
||||
components.release;
|
||||
end;
|
||||
|
||||
begin
|
||||
try
|
||||
url:= NSURL.URLWithString( StringToNSString(urlPart) );
|
||||
_request.setURL( url );
|
||||
_request.setHTTPMethod( HttpConst.Method.POST );
|
||||
if lclItems <> nil then begin
|
||||
self.setBody( getBody );
|
||||
self.setBody( THttpClientUtil.toNSString(lclItems) );
|
||||
self.setContentType( HttpConst.ContentType.UrlEncoded );
|
||||
end;
|
||||
|
||||
|
|
|
|||
|
|
@ -199,7 +199,7 @@ procedure TCloudConfigManager.saveToSecurity;
|
|||
end;
|
||||
|
||||
var
|
||||
jsonString: String;
|
||||
jsonString: NSString;
|
||||
jsonConnectionsSecurity: NSArray;
|
||||
begin
|
||||
jsonConnectionsSecurity:= saveConnectionsSecurity;
|
||||
|
|
@ -303,7 +303,7 @@ procedure TCloudConfigManager.saveToCommon(const path: String);
|
|||
end;
|
||||
|
||||
var
|
||||
jsonString: String;
|
||||
jsonString: NSString;
|
||||
jsonDrivers: NSArray;
|
||||
jsonConnectionsCommon: NSArray;
|
||||
begin
|
||||
|
|
@ -315,7 +315,7 @@ begin
|
|||
False,
|
||||
NSJSONWritingWithoutEscapingSlashes or NSJSONWritingPrettyPrinted );
|
||||
|
||||
StringToNSString(jsonString).writeToFile_atomically_encoding_error(
|
||||
jsonString.writeToFile_atomically_encoding_error(
|
||||
StringToNSString(path),
|
||||
False,
|
||||
NSUTF8StringEncoding,
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ type
|
|||
THttpClientUtil = class
|
||||
public
|
||||
class function toQueryItems( const lclItems: TQueryItemsDictonary ): NSArray;
|
||||
class function toNSString( const lclItems: TQueryItemsDictonary ): NSString;
|
||||
class procedure openInSafari( const urlPart: String; lclItems: TQueryItemsDictonary );
|
||||
class function queryValue( components: NSURLComponents; const name: String ): String;
|
||||
end;
|
||||
|
|
@ -76,7 +77,7 @@ type
|
|||
class function dumps(
|
||||
const Elements: Array of Const;
|
||||
const ensureAscii: Boolean = False;
|
||||
const options: Integer = NSJSONWritingWithoutEscapingSlashes ): String;
|
||||
const options: Integer = NSJSONWritingWithoutEscapingSlashes ): NSString;
|
||||
class function parse( const jsonString: String ): NSDictionary; overload;
|
||||
class function parse( const jsonString: NSString ): NSDictionary; overload;
|
||||
class procedure setString( const json: NSMutableDictionary; const key: String; const value: String );
|
||||
|
|
@ -97,7 +98,7 @@ type
|
|||
|
||||
TSecUtil = class
|
||||
public
|
||||
class procedure saveValue( const service: String; const account: String; const value: String ); overload;
|
||||
class procedure saveValue( const service: String; const account: String; const value: NSString ); overload;
|
||||
class function getValue( const service: String; const account: String ): String;
|
||||
end;
|
||||
|
||||
|
|
@ -211,7 +212,7 @@ end;
|
|||
class function TJsonUtil.dumps(
|
||||
const Elements: array of const;
|
||||
const ensureAscii: Boolean = False;
|
||||
const options: Integer = NSJSONWritingWithoutEscapingSlashes ): String;
|
||||
const options: Integer = NSJSONWritingWithoutEscapingSlashes ): NSString;
|
||||
var
|
||||
i: integer;
|
||||
jsonData: NSData;
|
||||
|
|
@ -248,6 +249,7 @@ var
|
|||
i: Integer;
|
||||
begin
|
||||
newString:= NSMutableString.new;
|
||||
newString.autorelease;
|
||||
for i:=0 to oldString.length-1 do begin
|
||||
c:= oldString.characterAtIndex( i );
|
||||
if c > $7F then
|
||||
|
|
@ -256,11 +258,10 @@ var
|
|||
newString.appendFormat( NSSTR('%C'), c );
|
||||
end;
|
||||
Result:= newString;
|
||||
newString.autorelease;
|
||||
end;
|
||||
|
||||
begin
|
||||
Result:= EmptyStr;
|
||||
Result:= nil;
|
||||
|
||||
if ((High(Elements)-Low(Elements)) mod 2)=0 then
|
||||
Exit;
|
||||
|
|
@ -278,12 +279,13 @@ begin
|
|||
error:= nil;
|
||||
jsonData:= NSJSONSerialization.dataWithJSONObject_options_error( json, options, @error );
|
||||
if error <> nil then
|
||||
Result:= EmptyStr
|
||||
Result:= nil
|
||||
else begin
|
||||
jsonString:= NSString.alloc.initWithData_encoding( jsonData, NSUTF8StringEncoding );
|
||||
jsonString.autorelease;
|
||||
if ensureAscii then
|
||||
jsonString:= escapeUnicode( jsonString );
|
||||
Result:= jsonString.UTF8String;
|
||||
Result:= jsonString;
|
||||
end;
|
||||
|
||||
json.release;
|
||||
|
|
@ -382,7 +384,7 @@ end;
|
|||
class procedure TSecUtil.saveValue(
|
||||
const service: String;
|
||||
const account: String;
|
||||
const value: String );
|
||||
const value: NSString );
|
||||
var
|
||||
appID: NSString;
|
||||
data: NSData;
|
||||
|
|
@ -390,7 +392,7 @@ var
|
|||
status: OSStatus;
|
||||
begin
|
||||
appID:= NSBundle.mainBundle.bundleIdentifier;
|
||||
data:= StringToNSString(value).dataUsingEncoding(NSUTF8StringEncoding);
|
||||
data:= value.dataUsingEncoding(NSUTF8StringEncoding);
|
||||
attributes:= NSMutableDictionary.new;
|
||||
attributes.setObject_forKey( kSecClassGenericPassword , kSecClass );
|
||||
attributes.setObject_forKey( appID, kSecAttrLabel );
|
||||
|
|
@ -544,6 +546,18 @@ begin
|
|||
Result:= cocoaItems;
|
||||
end;
|
||||
|
||||
class function THttpClientUtil.toNSString(const lclItems: TQueryItemsDictonary ): NSString;
|
||||
var
|
||||
components: NSURLComponents;
|
||||
cocoaItems: NSArray;
|
||||
begin
|
||||
components:= NSURLComponents.new;
|
||||
cocoaItems:= THttpClientUtil.toQueryItems( lclItems );
|
||||
components.setQueryItems( cocoaItems );
|
||||
Result:= components.query;
|
||||
components.release;
|
||||
end;
|
||||
|
||||
class procedure THttpClientUtil.openInSafari(
|
||||
const urlPart: String; lclItems: TQueryItemsDictonary );
|
||||
var
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue