Skip to content

Instantly share code, notes, and snippets.

@johnmmoss
johnmmoss / gist:ff97daa1afcee5644631
Created June 16, 2015 06:59
Example Http API Client Request
using (var client = new HttpClient())
{
var serializedBody = JsonConvert.SerializeObject(customerRequest);
var url = string.Format("{0}/{1}", ConfigurationManager.AppSettings["API_URL"], "customer");
var response = await client.PostAsync(url, new StringContent(serializedBody, Encoding.UTF8, "application/json"));
var content = await response.Content.ReadAsStringAsync();
@johnmmoss
johnmmoss / gist:a633b30955d2916a5783
Last active August 29, 2015 14:27
Mocking Web API Controller Context
var controller = new TestController();
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/test");
var route = config.Routes.MapHttpRoute("default", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "test" } });
controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
@johnmmoss
johnmmoss / Invoke-RestMethodExample.ps1
Created September 17, 2015 10:08
Examples using Invoke-RestMethod
# Get a page over http
Invoke-RestMethod -URI https://www.google.com -Method "GET"
# Post text content over http
Invoke-RestMethod -URI http://mvcsampleapp.apphb.com/Feedback -Body "Email=afsd&FirstName=sdfasdf&Surname=fasdfsd&Text=fasdfsdafsd" -ContentType "text/html" -Method POST
# Get a page over http specifying the IE proxey server
$Webproxy = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
Invoke-RestMethod -URI https://www.google.com -Method "GET" -Proxy $Webproxy -UseDefaultCredentials
@johnmmoss
johnmmoss / Invoke-RestMethodExample2.ps1
Created September 17, 2015 10:18
Example helper method round using Invoke-RestMethod
# Read out proxy settings
$Webproxy = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
# Wrapper method for Invokeing a rest method
function Invoke-Request {
Param( [string]$Method="GET", [string]$Body, [string]$Url )
if($Method -eq "GET") {
return Invoke-RestMethod -Proxy $Webproxy -UseDefaultCredentials -URI $Url -Method "GET"
} else {
@johnmmoss
johnmmoss / nuget.xml
Created October 8, 2015 11:10
Nuget.targets csproj xml
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
@johnmmoss
johnmmoss / typeahead-example.js
Created October 17, 2015 14:53
Typeahead and Bloodhound engine example
var bloodhound = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote:
{
url: baseUrl + '?q=%QUERY',
wildcard: '%QUERY',
}
});
@johnmmoss
johnmmoss / Shrinkfile1.sql
Created December 9, 2015 09:38
Shrinking a database file
USE AdeventureWorksDb
-- Set the datbase into simple mode which will truncate the log and close any connections
ALTER DATABASE AdeventureWorksDb
SET RECOVERY Simple;
GO
-- Shrink the file
DBCC SHRINKFILE('AdeventureWorksDb_Log')
@johnmmoss
johnmmoss / rhino-mock-setup.cs
Last active January 12, 2016 08:59
Rhino Mock Setup
private IConnectionManager _connectionManager;
private ISubscriptionManager _subscriptionManager;
[SetUp]
public void SetUp()
{
_connectionManager = MockRepository.GenerateStub<IConnectionManager>();
_subscriptionManager = MockRepository.GenerateStub<ISubscriptionManager>();
_connectionManager.Stub(x => x.Connections)
@johnmmoss
johnmmoss / JavaScript 101.js
Last active July 1, 2016 15:14
Introduction to JavaScript
'use strict'
// An empty JavaScript object
var myEmpty = {};
// Create an object using object literals
var monkey = { name: "John", age: 23, color: "blue" };
console.log(monkey.name) // John
@johnmmoss
johnmmoss / ECMA6_Class_Keyword.js
Last active July 1, 2016 15:14
Using ECMA 6 Class keyword
'use strict'
class Monkey {
constructor(name, color) {
this.color = color;
this.name = name;
}
}
var bert = new Monkey("albert", "red");