Skip to content

Commit

Permalink
Cleanup of namespace imports and more code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pheede committed Mar 24, 2013
1 parent d9bfda0 commit 906012b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
9 changes: 4 additions & 5 deletions ArcGISRESTAdmin/AGSClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public async Task Authenticate()
var data = new[] { new KeyValuePair<string, string>("username", encryptedUsername),
new KeyValuePair<string, string>("password", encryptedPassword),
new KeyValuePair<string, string>("client", encryptedClient),
new KeyValuePair<string, string>("encrypted", "true"),
new KeyValuePair<string, string>("f", "json") };
new KeyValuePair<string, string>("encrypted", "true") };
var content = new FormUrlEncodedContent(data);

var tokenInfo = await PostAsync(tokenEndpoint, content, addToken: false);
Expand Down Expand Up @@ -193,7 +192,8 @@ public async Task<string> PublishServiceDefinition(System.IO.FileInfo fi)
// Publish Service Definition GP tool takes one required parameter in_sdp_id which specifies the ID of the uploaded .sd
// simply omitting other optional parameters
var parms = new[] { new KeyValuePair<string, string>("in_sdp_id", uploadResponse.item.itemID) };


// Publish Service Definition is a system published GP tool hosted outside the main ArcGIS for Server Admin REST API
Uri publishEndpoint = new Uri(ServerUrl, "/arcgis/rest/services/System/PublishingTools/GPServer/Publish Service Definition/submitJob");

var publishResponse = await GetStringAsync(publishEndpoint, parms);
Expand Down Expand Up @@ -224,9 +224,8 @@ public async Task<UploadResponse> UploadItem(System.IO.FileInfo fi, string descr
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
content.Add(fileContent);

// other parameters
// add description if provided
if (!string.IsNullOrEmpty(description)) content.Add(new StringContent(description), "description");
content.Add(new StringContent("json"), "f");

UploadResponse response = await PostAsync<UploadResponse>(uploadEndpoint, content);

Expand Down
3 changes: 0 additions & 3 deletions ArcGISRESTAdmin/Classes/JsonConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace ArcGISRESTAdmin.Classes
Expand Down
4 changes: 0 additions & 4 deletions ArcGISRESTAdmin/Classes/LogMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ArcGISRESTAdmin.Classes
Expand Down
4 changes: 0 additions & 4 deletions ArcGISRESTAdmin/Classes/LogQueryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ArcGISRESTAdmin.Classes
Expand Down
6 changes: 0 additions & 6 deletions ArcGISRESTAdmin/Classes/ServicesResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArcGISRESTAdmin.Classes
{
public class ServicesResponse
Expand Down
54 changes: 35 additions & 19 deletions ArcGISRESTAdmin/EncodingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArcGISRESTAdmin
{
/// <summary>
/// A set of helper methods for converting to/from various encoding formats used by the ArcGIS for Server Admin REST API.
/// </summary>
public static class EncodingHelper
{
private static readonly DateTime UnixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

/// <summary>
/// Converts a hex-encoded string to the corresponding byte array.
/// </summary>
/// <param name="hex">Hex-encoded string</param>
/// <returns>Byte representation of the hex-encoded input</returns>
public static byte[] HexToBytes(string hex)
{
int length = hex.Length;
Expand All @@ -28,47 +32,59 @@ public static byte[] HexToBytes(string hex)

byte[] bytes = new byte[length / 2];
for (int i = 0; i < length; i += 2)
{
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}

return bytes;
}

/// <summary>
/// Hex-encodes a byte array.
/// </summary>
/// <param name="bytes">Byte array to encode</param>
/// <returns>Hex-encoded string</returns>
public static string BytesToHex(byte[] bytes)
{
StringBuilder stringBuilder = new StringBuilder(bytes.Length * 2);
StringBuilder sb = new StringBuilder(bytes.Length * 2);

for (int i = 0; i < bytes.Length; i++)
{
byte b = bytes[i];
stringBuilder.AppendFormat("{0:x2}", b);
sb.AppendFormat("{0:x2}", bytes[i]);
}
return stringBuilder.ToString();

return sb.ToString();
}

/// <summary>
/// Return the given DateTime as the count of milliseconds since the Unix epoch (1970-01-01).
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static long GetUnixTimestampMillis(DateTime dateTime)
{
dateTime = dateTime.ToUniversalTime();
double totalMs = (dateTime - UnixEpoch).TotalMilliseconds;
return (long)totalMs;
}

/// <summary>
/// Return the current UTC date and time as the count of milliseconds since the Unix epoch (1970-01-01).
/// </summary>
/// <returns></returns>
public static long GetCurrentUnixTimestampMillis()
{
return (long)(DateTime.UtcNow - UnixEpoch).TotalMilliseconds;
}

/// <summary>
/// Return a DateTime corresponding to the input Unix timestamp (in milliseconds).
/// </summary>
/// <param name="millis"></param>
/// <returns></returns>
public static DateTime DateTimeFromUnixTimestampMillis(long millis)
{
return UnixEpoch.AddMilliseconds(millis);
}

public static long GetCurrentUnixTimestampSeconds()
{
return (long)(DateTime.UtcNow - UnixEpoch).TotalSeconds;
}

public static DateTime DateTimeFromUnixTimestampSeconds(long seconds)
{
return UnixEpoch.AddSeconds(seconds);
}
}
}

0 comments on commit 906012b

Please sign in to comment.