Code Samples
As an example, let's use the API to ping a locally installed MusicPimp server and validate our credentials. We make a request and expect to get an HTTP response with status code 200 OK.
Here's how it can be done in Scala, using either Apache HttpClient or the Web Services library in Play Framework:
This file contains hidden or 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
package com.mle.samples | |
import org.scalatest.FunSuite | |
import com.ning.http.client.Realm.AuthScheme | |
import play.api.libs.ws.WS | |
import play.api.http.{HeaderNames, MimeTypes} | |
import scala.concurrent.Await | |
import scala.concurrent.duration._ | |
import org.apache.http.impl.client.{HttpClientBuilder, DefaultHttpClient} | |
import org.apache.http.client.methods.{HttpPost, HttpGet} | |
import org.apache.http.auth.UsernamePasswordCredentials | |
import org.apache.http.impl.auth.BasicScheme | |
import org.apache.http.protocol.BasicHttpContext | |
/** | |
* MusicPimp API samples. | |
*/ | |
class PimpSamples extends FunSuite { | |
test("ping MusicPimp with Apache HttpClient 4.3") { | |
val client = HttpClientBuilder.create().build() | |
val request = new HttpGet("http://localhost:8456/pingauth") | |
val creds = new UsernamePasswordCredentials("admin", "test") | |
request addHeader new BasicScheme().authenticate(creds, request, new BasicHttpContext()) | |
request.addHeader("Accept", "application/json") | |
val response = client execute request | |
val statusCode = response.getStatusLine.getStatusCode | |
assert(statusCode === 200) | |
} | |
test("ping MusicPimp with Apache HttpClient 4.2") { | |
val client = new DefaultHttpClient() | |
val request = new HttpGet("http://localhost:8456/pingauth") | |
request.addHeader("Accept", "application/json") | |
val creds = new UsernamePasswordCredentials("admin", "test") | |
request addHeader BasicScheme.authenticate(creds, "US-ASCII", false) | |
val response = client execute request | |
val statusCode = response.getStatusLine.getStatusCode | |
assert(statusCode === 200) | |
} | |
// uses the Web Services library in Play Framework | |
test("ping MusicPimp with Play WS client") { | |
val responseFuture = WS.url("http://localhost:8456/pingauth") | |
.withHeaders(HeaderNames.ACCEPT -> MimeTypes.JSON) | |
.withAuth("admin", "test", AuthScheme.BASIC) | |
.get() | |
val response = Await.result(responseFuture, 5 seconds) | |
assert(response.status === 200) | |
} | |
} |
Here's the same in C#, using either Microsoft HTTP Client Libraries (HttpClient) or RestSharp:
This file contains hidden or 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 Microsoft.VisualStudio.TestTools.UnitTesting; | |
using RestSharp; | |
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace tests { | |
[TestClass] | |
public class PimpSample { | |
[TestMethod] | |
public async Task PingPimpWithHttpClient() { | |
var client = new HttpClient(); | |
var headers = client.DefaultRequestHeaders; | |
var credBytes = Encoding.UTF8.GetBytes(String.Format("{0}:{1}", "admin", "test")); | |
var encodedCredentials = Convert.ToBase64String(credBytes); | |
headers.Authorization = new AuthenticationHeaderValue("Basic", encodedCredentials); | |
headers.Accept.ParseAdd("application/json"); | |
client.BaseAddress = new Uri("http://localhost:8456"); | |
var response = await client.GetAsync("/pingauth"); | |
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | |
} | |
[TestMethod] | |
public void PingPimpWithRestSharp() { | |
var client = new RestClient("http://localhost:8456"); | |
client.Authenticator = new HttpBasicAuthenticator("admin", "test"); | |
var request = new RestRequest("/pingauth", Method.GET); | |
var response = client.Execute(request); | |
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); | |
} | |
} | |
} |