-2

How do I convert the following curl request into Scala . I tried the following and it does not work .(getting 401 status code).

Any inputs:Any inputs:Any inputs:Any inputs:

curl -X GET 'https://api.newrelic.com/v2/applications.json' -H 'X-Api Key:9338232b0a1caa4e' -i -G -d 'filter[name]=my.app.hostname'

import com.ning.http.client.AsyncHttpClientConfig
import play.api.libs.ws.ning.NingWSClient
import scala.concurrent.ExecutionContext.Implicits.global


object callToNewRelic {
  val httpConfigBuilder = new AsyncHttpClientConfig.Builder()
  val httpConfig = httpConfigBuilder.build()
  val client = new NingWSClient(httpConfig)

  def filterByHost(APIkey:String,hostName: String):String={
    val content=client.url("https://api.newrelic.com/v2/applications.json")
      .withHeaders("X-Api-Key" -> APIkey)
      .withQueryString("filter[name]" -> hostName)
      .get()

    content onSuccess{
      case x if x.status ==200 =>
        x.json.toString()
      case y =>
        "Falied with Status Code "+y.status
        //println("Falied with Status Code "+y.status)
    }
    client.close()
    content.failed.toString
  }


  def main(args: Array[String]) {

    println("Filter by host is : \n"+ filterByHost("9338232b0943df4b690aa4e", "c85-ap2.qam.q.com"))
    //getNewRelicContentFromUrl()
  }
}

I'm getting the following error :

 [main] DEBUG c.n.h.c.p.n.NettyAsyncHttpProvider - Number of application's worker threads is 16
Exception in thread "main" java.lang.NoSuchMethodError: com.ning.http.client.RequestBuilder.addQueryParam(Ljava/lang/String;Ljava/lang/String;)Lcom/ning/http/client/RequestBuilder;
    at play.api.libs.ws.ning.NingWSRequest$$anonfun$buildRequest$3$$anonfun$apply$4.apply(NingWS.scala:230)
    at play.api.libs.ws.ning.NingWSRequest$$anonfun$buildRequest$3$$anonfun$apply$4.apply(NingWS.scala:229)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at play.api.libs.ws.ning.NingWSRequest$$anonfun$buildRequest$3.apply(NingWS.scala:229)
    at play.api.libs.ws.ning.NingWSRequest$$anonfun$buildRequest$3.apply(NingWS.scala:228)
    at scala.collection.TraversableLike$WithFilter$$anonfun$foreach$1.apply(TraversableLike.scala:772)
    at scala.collection.immutable.Map$Map1.foreach(Map.scala:109)
    at scala.collection.TraversableLike$WithFilter.foreach(TraversableLike.scala:771)
    at play.api.libs.ws.ning.NingWSRequest.buildRequest(NingWS.scala:228)
    at play.api.libs.ws.ning.NingWSRequest.execute(NingWS.scala:128)
    at play.api.libs.ws.WSRequest$class.get(WS.scala:408)
    at play.api.libs.ws.ning.NingWSRequest.get(NingWS.scala:81)
    at callToNewRelic$.filterByHost(callToNewRelic.scala:52)
    at callToNewRelic$.main(callToNewRelic.scala:68)
    at callToNewRelic.main(callToNewRelic.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Alferd Nobel
  • 3,185
  • 2
  • 30
  • 35

2 Answers2

1

I didnt compile it, but give it a try. It uses https://www.playframework.com/documentation/2.3.x/ScalaWS

import com.ning.http.client.AsyncHttpClientConfig
import play.api.libs.ws.ning.NingWSClient
import scala.concurrent.ExecutionContext.Implicits.global

val httpConfigBuilder = new AsyncHttpClientConfig.Builder()
val httpConfig = httpConfigBuilder.build()
val client = new NingWSClient(httpConfig)

client
  .url(s"https://api.newrelic.com/v2/applications.json")
  .withHeaders("X-Api Key" ->"9338232b0a1c0690aa4e")
  .withQueryString("filter[name]" -> "my.app.hostname")
  .get() map { res =>
    client.close()
    res.status
  }
Sergey Yarotskiy
  • 4,536
  • 2
  • 19
  • 27
  • @ Sergey Yarotskiy, thanks for responding, It still does not work, I included the code with play framework, When I tried to debug it I see `httpProvider = Method threw 'java.lang.NullPointerException' exception. Cannot evaluate com.ning.http.client.providers.netty.NettyAsyncHttpProvider.toString()` appreciate your inputs ? – Alferd Nobel Mar 13 '16 at 08:24
  • You do not need Play framework. Just import "com.typesafe.play" %% "play-ws" % 2.4.6, "com.typesafe.play" %% "play-json" % 2.4.6 Problem can be with the version of the library. Check version that being included. Check documentation for this version. API could change. This code works in my project. – Sergey Yarotskiy Mar 13 '16 at 15:28
  • thanks this worked, I did remove couple of references to to scala 2.9.1 and I used scala 2.10.6 – Alferd Nobel Mar 14 '16 at 21:09
  • I'm getting the following error "Exception in thread "main" java.lang.NoSuchMethodError:" , any idea how to fix this? – Alferd Nobel Mar 24 '16 at 04:58
  • I got the error resolved(java.lang.NoSuchMethodError: com.ning.http.client.) by adding the maven dependency for async-http-client(com.ning) – Alferd Nobel Mar 24 '16 at 07:03
-1

I fixed the issue by adding the following dependencies in maven, I got the error resolved(java.lang.NoSuchMethodError: com.ning.http.client.) by adding the maven dependency for async-http-client(com.ning)

<dependency>
   <groupId>com.ning</groupId>
   <artifactId>async-http-client</artifactId>
   <version>1.9.31</version>
</dependency>
<dependency>
  <groupId>com.typesafe.play</groupId>
  <artifactId>play_2.10</artifactId>
  <version>2.4.6</version>
</dependency>
<dependency>
    <groupId>com.typesafe.play</groupId>
    <artifactId>play-ws_2.10</artifactId>
    <version>2.4.6</version>
</dependency>
<dependency>
    <groupId>com.typesafe.play</groupId>
    <artifactId>play-json_2.10</artifactId>
    <version>2.4.6</version>
</dependency>

Also used Scala SDK 2.10.6 and removed the references to 2.9 scala compiler in Libraries ( IntelliJ )

This is the the modified code :

import com.ning.http.client.AsyncHttpClientConfig
import play.api.libs.ws.ning.NingWSClient
import scala.concurrent.ExecutionContext.Implicits.global

object getContent {

  def getNewRelicContentFromUrl(): Unit = {
    val httpConfigBuilder = new AsyncHttpClientConfig.Builder()
    val httpConfig = httpConfigBuilder.build()
    val client = new NingWSClient(httpConfig)
    val content =client
      .url("https://api.newrelic.com/v2/applications.json")
      .withHeaders("X-Api-Key" -> "9338232b0a1c010dc7943df4b690aa4e")
      //.withQueryString("filter[name]" -> "my.app.hostname")
     .get() /* map { res =>
      client.close()
      res.status
    }*/
    content onSuccess{

        case x if x.status ==200 =>
          //x.statusText
          println(x.json);
        case y =>
          println("Falied with Status Code "+y.status)
    }
    //println(response.toString);
  }

  def main(args: Array[String]) {

    getNewRelicContentFromUrl()
  }
}
Alferd Nobel
  • 3,185
  • 2
  • 30
  • 35