SearchRequest.kt 2.55 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
23

import android.content.Context
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
24
25
26
27
28
29
30
import foundation.e.apps.application.model.Application
import foundation.e.apps.applicationmanager.ApplicationManager
import foundation.e.apps.application.model.data.BasicData
import foundation.e.apps.utils.Error
import foundation.e.apps.utils.ApplicationParser
import foundation.e.apps.utils.Common
import foundation.e.apps.utils.Constants
31
import java.lang.Exception
Nihar Thakkar's avatar
Nihar Thakkar committed
32
import java.net.URLEncoder
jo's avatar
jo committed
33
34
35
36
37
38
39

class SearchRequest(private val keyword: String, private val page: Int, private val resultsPerPage: Int) {

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

40
41
    fun request(callback: (Error?, SearchResult?) -> Unit) {
        try {
jo's avatar
jo committed
42
43
            val url = Constants.BASE_URL + "apps?action=search&keyword=${URLEncoder.encode(keyword, "utf-8")}&page=$page&nres=$resultsPerPage"
            val urlConnection = Common.createConnection(url)
44
45
46
47
            val result = reader.readValue<SearchResult>(urlConnection.inputStream)
            urlConnection.disconnect()
            callback.invoke(null, result)
        } catch (e: Exception) {
jo's avatar
jo committed
48
            callback.invoke(Error.findError(e), null)
49
        }
jo's avatar
jo committed
50
51
52
    }

    class SearchResult @JsonCreator
53
54
    constructor(@JsonProperty("success") success: Boolean,
                @param:JsonProperty("pages") val pages: Int,
jo's avatar
jo committed
55
56
57
                @param:JsonProperty("numberOfResults") val resultsNumber: Int,
                @param:JsonProperty("apps") val appResults: Array<BasicData>) {

58
59
        fun getApplications(applicationManager: ApplicationManager, context: Context): List<Application> {
            return ApplicationParser.parseToApps(applicationManager, context, appResults)
jo's avatar
jo committed
60
61
62
        }
    }
}