All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.kondaurovdev.http_client.PageParser.scala Maven / Gradle / Ivy

The newest version!
package com.github.kondaurovdev.http_client

import com.github.kondaurovdev.play_json.JsonHelper
import com.github.kondaurovdev.snippets.helper.TryHelper
import play.api.libs.json.{JsError, JsString, JsValue, Json}

import scalaj.http.{HttpRequest, HttpResponse}

object PageParser {

  def getPage(req: HttpRequest, maxTries: Int = 1, tries: Int = 0): Either[JsValue, HttpResponse[String]] = {

    TryHelper.tryBlock { () =>
      req.execute[String]()
    }.fold(
      err => {
        if (tries + 1 >= maxTries) {
          Left(JsString(s"Can't get page: $err. Tries amount: $tries"))
        } else {
          getPage(req, maxTries, tries + 1)
        }
      },
      response => Right(response)
    )

  }

  def getJsonPage(req: HttpRequest, maxTries: Int = 1): Either[JsValue, HttpResponse[JsValue]] = {

    getPage(req, maxTries).right.flatMap(response => {
      JsonHelper.parse(response.body).asEither.left.map(err => {
        Json.obj("Can't parse json output" -> JsError.toJson(err))
      }).right.flatMap(json => {
        if (response.code != 200) {
          Left(Json.obj(
            "error" -> "response code isn't 200",
            "response" -> json,
            "request" -> req.toString
          ))
        } else {
          Right(HttpResponse(json, response.code, response.headers))
        }
      })

    })

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy