Created
May 23, 2014 15:02
-
-
Save dodikk/e6d75eca9bf36296a927 to your computer and use it in GitHub Desktop.
REST API flow with Xamarin async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetPublicKeyTasks : IRestApiCallTasks<string, Stream, PublicKeyX509Certificate> | |
{ | |
public async Task<string> BuildRequestUrlForRequestAsync( string instanceUrl ) | |
{ | |
return await Task.Factory.StartNew ( () => instanceUrl + "/-/item/v1/-/actions/getpublickey" ); | |
} | |
public async Task<Stream> SendRequestForUrlAsync( string requestUrl ) | |
{ | |
return await this.httpClient.GetStreamAsync(requestUrl); | |
} | |
// disposes httpData | |
public async Task<PublicKeyX509Certificate> ParseResponseDataAsync(Stream httpData) | |
{ | |
using (Stream publicKeyStream = httpData) | |
{ | |
Func<PublicKeyX509Certificate> syncParsePublicKey = () => | |
{ | |
return new PublicKeyXmlParser().Parse(publicKeyStream); | |
}; | |
PublicKeyX509Certificate result = await Task.Factory.StartNew(syncParsePublicKey); | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IRestApiCallTasks<TRequest, THttpResult, TResult> | |
{ | |
Task<string> BuildRequestUrlForRequestAsync( TRequest request ); | |
Task<THttpResult> SendRequestForUrlAsync( string requestUrl ); | |
Task<TResult> ParseResponseDataAsync(THttpResult httpData); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RestApiCallFlow | |
{ | |
public static async Task<TResult> LoadRequestFromNetworkFlow<TRequest, THttpResult, TResult>(TRequest request, IRestApiCallTasks<TRequest, THttpResult, TResult> stages) | |
{ | |
string requestUrl = await stages.BuildRequestUrlForRequestAsync (request ); | |
THttpResult serverResponse = await stages.SendRequestForUrlAsync (requestUrl ); | |
TResult parsedData = await stages.ParseResponseDataAsync (serverResponse); | |
return parsedData; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<PublicKeyX509Certificate> GetPublicKey() | |
{ | |
GetPublicKeyTasks taskFlow = new GetPublicKeyTasks (this.httpClient); | |
PublicKeyX509Certificate result = await RestApiCallFlow.LoadRequestFromNetworkFlow(this.sessionConfig.InstanceUrl, taskFlow); | |
this.publicCertifiacte = result; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment