AppDetailRequest.kt 1.8 KB
Newer Older
Nihar Thakkar's avatar
Nihar Thakkar committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
    Copyright (C) 2019  e Foundation

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

18
package foundation.e.apps.api
jo's avatar
jo committed
19
20
21
22

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
23
24
25
26
import foundation.e.apps.application.model.data.FullData
import foundation.e.apps.utils.Common
import foundation.e.apps.utils.Constants
import foundation.e.apps.utils.Error
jo's avatar
jo committed
27
28
29
30
31
32
33

class AppDetailRequest(private val id: String) {

    companion object {
        private val reader = ObjectMapper().readerFor(Result::class.java)
    }

34
35
    fun request(callback: (Error?, FullData?) -> Unit) {
        try {
jo's avatar
jo committed
36
37
            val url = Constants.BASE_URL + "apps?action=app_detail&id=$id"
            val urlConnection = Common.createConnection(url)
38
39
40
41
            val result = reader.readValue<Result>(urlConnection.inputStream)
            urlConnection.disconnect()
            callback.invoke(null, result.app)
        } catch (e: Exception) {
jo's avatar
jo committed
42
            callback.invoke(Error.findError(e), null)
43
        }
jo's avatar
jo committed
44
45
46
47
48
49
    }

    class Result @JsonCreator
    constructor(@JsonProperty("app") val app: FullData,
                @JsonProperty("success") private val success: Boolean)
}