Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
96ed1cbd
Commit
96ed1cbd
authored
Sep 17, 2017
by
Filipe de Lima Brito
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add RestApiHelper.kt
parent
ca4a86e5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
209 additions
and
0 deletions
+209
-0
RestApiHelper.kt
...c/main/java/chat/rocket/android/api/rest/RestApiHelper.kt
+209
-0
No files found.
app/src/main/java/chat/rocket/android/api/rest/RestApiHelper.kt
0 → 100644
View file @
96ed1cbd
package
chat.rocket.android.api.rest
import
chat.rocket.core.models.Room
import
okhttp3.HttpUrl
import
okhttp3.Request
/**
* Helper class for dealing with Rest API calls.
*
* @see <a href="https://rocket.chat/docs/developer-guides/rest-api">https://rocket.chat/docs/developer-guides/rest-api</a>.
*/
object
RestApiHelper
{
/**
* Returns an OkHttp3 request for pinned messages.
*
* @param roomId The ID of the room.
* @param roomType The type of the room.
* @param hostname The server hostname.
* @param token The token.
* @param userId The user Id.
* @return An OkHttp3 request.
*/
fun
getRequestForPinnedMessages
(
roomId
:
String
,
roomType
:
String
,
hostname
:
String
,
token
:
String
,
userId
:
String
):
Request
{
val
parsedHttpUrl
=
HttpUrl
.
parse
(
getEndpointUrlForMessages
(
roomType
,
hostname
))
?.
newBuilder
()
?.
addQueryParameter
(
"roomId"
,
roomId
)
?.
addQueryParameter
(
"query"
,
"{\"pinned\":true}"
)
?.
build
()
return
Request
.
Builder
()
.
url
(
parsedHttpUrl
)
.
get
()
.
addHeader
(
"X-Auth-Token"
,
token
)
.
addHeader
(
"X-User-Id"
,
userId
)
.
build
()
}
/**
* Returns an OkHttp3 request for favorite messages.
*
* @param roomId The ID of the room.
* @param roomType The type of the room.
* @param hostname The server hostname.
* @param token The token.
* @param userId The user Id.
* @return An OkHttp3 request.
*/
fun
getRequestForFavoriteMessages
(
roomId
:
String
,
roomType
:
String
,
hostname
:
String
,
token
:
String
,
userId
:
String
):
Request
{
val
parsedHttpUrl
=
HttpUrl
.
parse
(
getEndpointUrlForMessages
(
roomType
,
hostname
))
?.
newBuilder
()
?.
addQueryParameter
(
"roomId"
,
roomId
)
?.
addQueryParameter
(
"query"
,
"{\"starred._id\":{\"\$in\":[\"$userId\"] } }"
)
?.
build
()
return
Request
.
Builder
()
.
url
(
parsedHttpUrl
)
.
get
()
.
addHeader
(
"X-Auth-Token"
,
token
)
.
addHeader
(
"X-User-Id"
,
userId
)
.
build
()
}
/**
* Returns an OkHttp3 request for file list.
*
* @param roomId The ID of the room.
* @param roomType The type of the room.
* @param hostname The server hostname.
* @param token The token.
* @param userId The user Id.
* @return An OkHttp3 request.
*/
fun
getRequestForFileList
(
roomId
:
String
,
roomType
:
String
,
hostname
:
String
,
token
:
String
,
userId
:
String
):
Request
{
val
parsedHttpUrl
=
HttpUrl
.
parse
(
getEndpointUrlForFileList
(
roomType
,
hostname
))
?.
newBuilder
()
?.
addQueryParameter
(
"roomId"
,
roomId
)
?.
build
()
return
Request
.
Builder
()
.
url
(
parsedHttpUrl
)
.
get
()
.
addHeader
(
"X-Auth-Token"
,
token
)
.
addHeader
(
"X-User-Id"
,
userId
)
.
build
()
}
/**
* Returns an OkHttp3 request for member list.
*
* @param roomId The ID of the room.
* @param roomType The type of the room.
* @param hostname The server hostname.
* @param token The token.
* @param userId The user Id.
* @return An OkHttp3 request.
*/
fun
getRequestForMemberList
(
roomId
:
String
,
roomType
:
String
,
hostname
:
String
,
token
:
String
,
userId
:
String
):
Request
{
val
parsedHttpUrl
=
HttpUrl
.
parse
(
getEndpointUrlForMemberList
(
roomType
,
hostname
))
?.
newBuilder
()
?.
addQueryParameter
(
"roomId"
,
roomId
)
?.
build
()
return
Request
.
Builder
()
.
url
(
parsedHttpUrl
)
.
get
()
.
addHeader
(
"X-Auth-Token"
,
token
)
.
addHeader
(
"X-User-Id"
,
userId
)
.
build
()
}
/**
* Returns a Rest API endpoint URL for favorite or pinned messages accordingly with the room type and the server hostname.
*
* @param roomType The type of the room.
* @param hostname The server hostname.
* @return A Rest API URL endpoint.
*/
fun
getEndpointUrlForMessages
(
roomType
:
String
,
hostname
:
String
):
String
=
"https://"
+
hostname
.
replace
(
"http://"
,
""
).
replace
(
"https://"
,
""
)
+
getRestApiUrlForMessages
(
roomType
)
/**
* Returns a Rest API endpoint URL for member list accordingly with the room type and the server hostname.
*
* @param roomType The type of the room.
* @param hostname The server hostname.
* @return A Rest API URL endpoint.
*/
fun
getEndpointUrlForMemberList
(
roomType
:
String
,
hostname
:
String
):
String
=
"https://"
+
hostname
.
replace
(
"http://"
,
""
).
replace
(
"https://"
,
""
)
+
getRestApiUrlForMemberList
(
roomType
)
/**
* Returns a Rest API endpoint URL for file list accordingly with the room type and the server hostname.
*
* @param roomType The type of the room.
* @param hostname The server hostname.
* @return A Rest API URL endpoint.
*/
fun
getEndpointUrlForFileList
(
roomType
:
String
,
hostname
:
String
):
String
=
"https://"
+
hostname
.
replace
(
"http://"
,
""
).
replace
(
"https://"
,
""
)
+
getRestApiUrlForFileList
(
roomType
)
/**
* Returns the correspondent Rest API URL accordingly with the room type to get its favorite or pinned messages.
*
* @param roomType The type of the room.
* @return A Rest API URL or null if the room type does not match.
*/
fun
getRestApiUrlForMessages
(
roomType
:
String
):
String
?
{
var
restApiUrl
:
String
?
=
null
when
(
roomType
)
{
Room
.
TYPE_CHANNEL
->
restApiUrl
=
"/api/v1/channels.messages"
Room
.
TYPE_PRIVATE
->
restApiUrl
=
"/api/v1/groups.messages"
Room
.
TYPE_DIRECT_MESSAGE
->
restApiUrl
=
"/api/v1/dm.messages"
}
return
restApiUrl
}
/**
* Returns the correspondent Rest API URL accordingly with the room type to get its file list.
*
* @param roomType The type of the room.
* @return A Rest API URL or null if the room type does not match.
*/
fun
getRestApiUrlForFileList
(
roomType
:
String
):
String
?
{
var
restApiUrl
:
String
?
=
null
when
(
roomType
)
{
Room
.
TYPE_CHANNEL
->
restApiUrl
=
"/api/v1/channels.files"
Room
.
TYPE_PRIVATE
->
restApiUrl
=
"/api/v1/groups.files"
Room
.
TYPE_DIRECT_MESSAGE
->
restApiUrl
=
"/api/v1/dm.files"
}
return
restApiUrl
}
/**
* Returns the correspondent Rest API URL accordingly with the room type to get its members list.
*
* @param roomType The type of the room.
* @return A Rest API URL or null if the room type does not match.
*/
private
fun
getRestApiUrlForMemberList
(
roomType
:
String
):
String
?
{
var
restApiUrl
:
String
?
=
null
when
(
roomType
)
{
Room
.
TYPE_CHANNEL
->
restApiUrl
=
"/api/v1/channels.members"
Room
.
TYPE_PRIVATE
->
restApiUrl
=
"/api/v1/groups.members"
Room
.
TYPE_DIRECT_MESSAGE
->
restApiUrl
=
"/api/v1/dm.members"
}
return
restApiUrl
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment