|
1 | 1 | package net.pearx.kpastebin
|
2 | 2 |
|
3 |
| -import io.ktor.client.HttpClient |
4 |
| -import io.ktor.client.features.defaultRequest |
5 | 3 | import io.ktor.client.request.post
|
6 | 4 | import io.ktor.http.*
|
7 | 5 | import net.pearx.kpastebin.internal.*
|
8 |
| -import net.pearx.kpastebin.internal.checkPastebinResponse |
9 | 6 | import net.pearx.kpastebin.model.ExpireDate
|
10 | 7 | import net.pearx.kpastebin.model.PasteDetails
|
11 | 8 | import net.pearx.kpastebin.model.Privacy
|
12 | 9 | import net.pearx.kpastebin.model.UserDetails
|
13 | 10 |
|
14 |
| -public class PastebinClient(private val devKey: String) { |
15 |
| - private suspend fun sendRequest(url: String, parameters: Parameters): String { |
| 11 | +/** |
| 12 | + * Pastebin API client with specified unique developer API key [devKey]. |
| 13 | + * You can get your key on [official Pastebin website](https://pastebin.com/doc_api#1). |
| 14 | + * |
| 15 | + * @param userKey user key used for requests. Use null for guest user. |
| 16 | + */ |
| 17 | +public class PastebinClient( |
| 18 | + private val devKey: String, |
| 19 | + /** |
| 20 | + * User key used for requests. Use null for guest user. It also can be set using [login] method. |
| 21 | + */ |
| 22 | + public var userKey: String? = null |
| 23 | +) { |
| 24 | + private suspend fun sendRequest(url: String, userKeyRequired: Boolean, parameters: Parameters): String { |
| 25 | + val userKey = userKey |
| 26 | + if (userKeyRequired && userKey == null) |
| 27 | + throw InvalidUserKeyException("The 'userKey' property is null. It can be initialized by setting it directly or using the 'login' function.") |
16 | 28 | val out = Http.post<String> {
|
17 | 29 | contentType(ContentType.Application.FormUrlEncoded)
|
18 | 30 | this.url.takeFrom(url)
|
19 | 31 | body = Parameters.build {
|
20 | 32 | append("api_dev_key", devKey)
|
| 33 | + if (userKey != null) |
| 34 | + append("api_user_key", userKey) |
21 | 35 | appendAll(parameters)
|
22 | 36 | }.formUrlEncode()
|
23 | 37 | }
|
24 | 38 | checkPastebinResponse(out)
|
25 | 39 | return out
|
26 | 40 | }
|
27 | 41 |
|
28 |
| - private suspend inline fun sendRequest(url: String, parametersBuilder: ParametersBuilder.() -> Unit) = sendRequest(url, Parameters.build(parametersBuilder)) |
| 42 | + private suspend inline fun sendRequest(url: String, userKeyRequired: Boolean, parametersBuilder: ParametersBuilder.() -> Unit) = sendRequest(url, userKeyRequired, Parameters.build(parametersBuilder)) |
29 | 43 |
|
| 44 | + /** |
| 45 | + * Publishes a new paste with specified [text]. |
| 46 | + * If [userKey] is null, the paste will be published anonymously. |
| 47 | + * |
| 48 | + * @param name paste title. |
| 49 | + * @param format paste syntax highlighting format. Please see [Pastebin documentation](https://pastebin.com/doc_api#5) for the list of available options. |
| 50 | + * @param privacy paste privacy status. |
| 51 | + * @param expireDate paste expiration duration. |
| 52 | + * |
| 53 | + * @throws InvalidUserKeyException when [userKey] is invalid or expired. |
| 54 | + * @throws PastePerDayLimitException when paste limit for this IP per day is exceeded. |
| 55 | + * @throws FreePasteLimitException when paste limit for current user is exceeded. |
| 56 | + * @throws PasteSizeException when [text] exceeds the size limit. |
| 57 | + * @throws InvalidPasteFormatException when specified [format] is invalid. |
| 58 | + */ |
30 | 59 | public suspend fun createPaste(
|
31 | 60 | text: String,
|
32 |
| - userKey: String? = null, |
33 |
| - name: String? = null, |
34 |
| - format: String? = null, |
35 |
| - privacy: Privacy? = null, |
36 |
| - expireDate: ExpireDate? = null |
| 61 | + name: String = "", |
| 62 | + format: String = "text", |
| 63 | + privacy: Privacy = Privacy.PUBLIC, |
| 64 | + expireDate: ExpireDate = ExpireDate.NEVER |
37 | 65 | ): String {
|
38 |
| - return sendRequest(API_URL_POST) { |
| 66 | + return sendRequest(API_URL_POST, false) { |
39 | 67 | append("api_option", "paste")
|
40 | 68 | append("api_paste_code", text)
|
41 |
| - if (userKey != null) |
42 |
| - append("api_user_key", userKey) |
43 |
| - if (name != null) |
44 |
| - append("api_paste_name", name) |
45 |
| - if (format != null) |
46 |
| - append("api_paste_format", format) |
47 |
| - if (privacy != null) |
48 |
| - append("api_privacy", privacy.ordinal.toString()) |
49 |
| - if (expireDate != null) |
50 |
| - append("api_paste_expire_date", expireDate.code) |
| 69 | + append("api_paste_name", name) |
| 70 | + append("api_paste_format", format) |
| 71 | + append("api_privacy", privacy.ordinal.toString()) |
| 72 | + append("api_paste_expire_date", expireDate.code) |
51 | 73 | }
|
52 | 74 | }
|
53 | 75 |
|
54 |
| - public suspend fun login(username: String, password: String): String { |
55 |
| - return sendRequest(API_URL_LOGIN) { |
| 76 | + /** |
| 77 | + * Log ins into Pastebin account using provided [username] and [password] combination and sets the [userKey] property. |
| 78 | + * |
| 79 | + * @throws InvalidLoginException when specified [username]/[password] combination is invalid. |
| 80 | + * @throws AccountNotActiveException when the account is not active. |
| 81 | + */ |
| 82 | + public suspend fun login(username: String, password: String) { |
| 83 | + userKey = sendRequest(API_URL_LOGIN, false) { |
56 | 84 | append("api_user_name", username)
|
57 | 85 | append("api_user_password", password)
|
58 | 86 | }
|
59 | 87 | }
|
60 | 88 |
|
61 |
| - public suspend fun listPastes(userKey: String, resultsLimit: Int? = null): List<PasteDetails> { |
62 |
| - val out = sendRequest(API_URL_POST) { |
63 |
| - append("api_user_key", userKey) |
| 89 | + /** |
| 90 | + * Lists pastes created by this user. |
| 91 | + * |
| 92 | + * @param resultsLimit maximum number of results returned. It should be in range of 1 to 1000. |
| 93 | + * |
| 94 | + * @throws InvalidUserKeyException when [userKey] is null, expired or invalid. |
| 95 | + */ |
| 96 | + public suspend fun listPastes(resultsLimit: Int = 50): List<PasteDetails> { |
| 97 | + require(resultsLimit in 1..1000) { "resultsLimit should be in range of 1 to 1000" } |
| 98 | + val out = sendRequest(API_URL_POST, true) { |
64 | 99 | append("api_option", "list")
|
65 |
| - if (resultsLimit != null) |
66 |
| - append("api_results_limit", resultsLimit.toString()) |
| 100 | + append("api_results_limit", resultsLimit.toString()) |
67 | 101 | }
|
68 | 102 | if (out == "No pastes found.")
|
69 | 103 | return listOf()
|
70 | 104 | return PasteDetails.parseList(out)
|
71 | 105 | }
|
72 | 106 |
|
73 |
| - public suspend fun deletePaste(userKey: String, pasteKey: String) { |
74 |
| - val out = sendRequest(API_URL_POST) { |
| 107 | + /** |
| 108 | + * Deletes a paste by its [pasteKey] created by this user. |
| 109 | + * |
| 110 | + * @throws InvalidUserKeyException when [userKey] is null, expired or invalid. |
| 111 | + * @throws PasteNotFoundException when no paste with such [pasteKey] is found. |
| 112 | + */ |
| 113 | + public suspend fun deletePaste(pasteKey: String) { |
| 114 | + val out = sendRequest(API_URL_POST, true) { |
75 | 115 | append("api_option", "delete")
|
76 |
| - append("api_user_key", userKey) |
77 | 116 | append("api_paste_key", pasteKey)
|
78 | 117 | }
|
79 | 118 | if (out != "Paste Removed")
|
80 | 119 | throw PastebinException(out)
|
81 | 120 | }
|
82 | 121 |
|
83 |
| - public suspend fun getUserDetails(userKey: String): UserDetails { |
| 122 | + /** |
| 123 | + * Gets detailed information about this user. |
| 124 | + * |
| 125 | + * @throws InvalidUserKeyException when [userKey] is null, expired or invalid. |
| 126 | + */ |
| 127 | + public suspend fun getUserDetails(): UserDetails { |
84 | 128 | return UserDetails.parse(
|
85 |
| - sendRequest(API_URL_POST) { |
| 129 | + sendRequest(API_URL_POST, true) { |
86 | 130 | append("api_option", "userdetails")
|
87 |
| - append("api_user_key", userKey) |
88 | 131 | }
|
89 | 132 | )
|
90 | 133 | }
|
91 | 134 |
|
92 |
| - public suspend fun getPaste(userKey: String, pasteKey: String): String { |
93 |
| - return sendRequest(API_URL_RAW) { |
| 135 | + /** |
| 136 | + * Gets text of a paste by its [pasteKey]. |
| 137 | + * |
| 138 | + * @see net.pearx.kpastebin.getPaste |
| 139 | + * |
| 140 | + * @throws InvalidUserKeyException when [userKey] is null, expired or invalid. |
| 141 | + * @throws PasteNotFoundException when no paste with such [pasteKey] is found. |
| 142 | + */ |
| 143 | + public suspend fun getPaste(pasteKey: String): String { |
| 144 | + return sendRequest(API_URL_RAW, true) { |
94 | 145 | append("api_option", "show_paste")
|
95 |
| - append("api_user_key", userKey) |
96 | 146 | append("api_paste_key", pasteKey)
|
97 | 147 | }
|
98 | 148 | }
|
|
0 commit comments