Created
March 13, 2013 11:28
-
-
Save JohannesRudolph/5151254 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Formatting; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Xunit; | |
using System.IO.Compression; | |
using System.Web.Http; | |
using System.Net; | |
namespace IronWorkerDemo.Scenarios | |
{ | |
public class IronWorkerConfiguration | |
{ | |
public string ProjectId | |
{ | |
get { return "YOUR_PROJECT_ID"; ; } | |
} | |
public string OAuthToken | |
{ | |
get { return "YOUR_TOKEN"; ; } | |
} | |
} | |
public class IronWorkerFacade : IDisposable | |
{ | |
private readonly HttpClient _client; | |
private readonly IronWorkerConfiguration _config; | |
public IronWorkerFacade( IronWorkerConfiguration config ) | |
{ | |
if (config == null) | |
throw new ArgumentNullException( "config" ); | |
_config = config; | |
_client = new HttpClient(); | |
} | |
public Task<HttpResponseMessage> DeployWorker( FileInfo file ) | |
{ | |
var request = BuildRequest( _config, file ); | |
return _client.SendAsync( request ); | |
} | |
private static HttpRequestMessage BuildRequest( IronWorkerConfiguration config, FileInfo file ) | |
{ | |
string urlFormat = "https://worker-aws-us-east-1.iron.io/2/projects/{0}/codes?oauth={1}"; | |
string url = String.Format( urlFormat, config.ProjectId, config.OAuthToken ); | |
HttpRequestMessage req = new HttpRequestMessage( HttpMethod.Post, url ); | |
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue( "OAuth", config.OAuthToken ); | |
var content = new MultipartFormDataContent(); | |
content.Add( BuildJsonContent( file ), "data" ); | |
content.Add( BuildZipContent( file ), "file", "hourly.zip" ); | |
req.Content = content; | |
return req; | |
} | |
private static ObjectContent<CreateWorkerRequest> BuildJsonContent( FileInfo file ) | |
{ | |
var body = new CreateWorkerRequest() | |
{ | |
Name = "hourly", | |
File_Name = file.Name, | |
Runtime = "binary" | |
}; | |
return new ObjectContent<CreateWorkerRequest>( body, new JsonMediaTypeFormatter() ); | |
} | |
private static ByteArrayContent BuildZipContent( FileInfo file ) | |
{ | |
var zip = CreateWorkerRequest.PackageScript( file ); | |
ByteArrayContent zipContent = new ByteArrayContent( zip ); | |
zipContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue( "application/zip" ); | |
return zipContent; | |
} | |
public void Dispose() | |
{ | |
_client.Dispose(); | |
} | |
private class CreateWorkerRequest | |
{ | |
public static byte[] PackageScript( FileInfo scriptFile ) | |
{ | |
var target = new MemoryStream(); | |
using (ZipArchive za = new ZipArchive( stream: target, mode: ZipArchiveMode.Create, leaveOpen: true, entryNameEncoding: Encoding.ASCII )) | |
{ | |
var entry = za.CreateEntry( scriptFile.Name, CompressionLevel.Optimal ); | |
using (var entryStream = entry.Open()) | |
{ | |
using (var input = scriptFile.OpenRead()) | |
{ | |
input.CopyTo( entryStream ); | |
} | |
} | |
} | |
return target.ToArray(); | |
} | |
[Newtonsoft.Json.JsonProperty( "name" )] | |
public string Name { get; set; } | |
[Newtonsoft.Json.JsonProperty( "file_name" )] | |
public string File_Name { get; set; } | |
[Newtonsoft.Json.JsonProperty( "runtime" )] | |
public string Runtime { get; set; } | |
} | |
} | |
public class IronFacadeFacts | |
{ | |
[Fact] | |
public void DeployWorker() | |
{ | |
IronWorkerConfiguration config = new IronWorkerConfiguration(); | |
using (var sut = new IronWorkerFacade( config )) | |
{ | |
FileInfo script = new FileInfo( @"scriptfile here" ); | |
using (var response = sut.DeployWorker( script ).Result) | |
{ | |
Assert.Equal( HttpStatusCode.OK, response.StatusCode ); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment