Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
REST Webservices with Pagination
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SOFT Core
SOFT 161 and 162
REST Webservices with Pagination
Commits
6dc33d9f
Commit
6dc33d9f
authored
4 years ago
by
Brady James Garvin
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit.
parents
Branches
main
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+5
-0
5 additions, 0 deletions
.gitignore
main.py
+30
-0
30 additions, 0 deletions
main.py
openmrs.py
+46
-0
46 additions, 0 deletions
openmrs.py
rest.kv
+8
-0
8 additions, 0 deletions
rest.kv
with
89 additions
and
0 deletions
.gitignore
0 → 100644
+
5
−
0
View file @
6dc33d9f
*~
*.pyc
*.pyo
.idea
This diff is collapsed.
Click to expand it.
main.py
0 → 100644
+
30
−
0
View file @
6dc33d9f
from
kivy.app
import
App
from
kivy.uix.label
import
Label
from
kivy.logger
import
Logger
from
json
import
dumps
from
openmrs
import
RESTConnection
class
RestApp
(
App
):
def
load_locations
(
self
):
self
.
root
.
ids
.
results
.
clear_widgets
()
openmrs_connection
=
RESTConnection
(
'
localhost
'
,
8080
,
'
admin
'
,
'
Admin123
'
)
openmrs_connection
.
send_composite_request
(
'
location
'
,
None
,
None
,
self
.
on_locations_loaded
,
self
.
on_locations_not_loaded
,
self
.
on_locations_not_loaded
)
def
on_locations_loaded
(
self
,
_
,
response
):
print
(
dumps
(
response
,
indent
=
4
,
sort_keys
=
True
))
results_layout
=
self
.
root
.
ids
.
results
for
result
in
response
[
'
results
'
]:
results_layout
.
add_widget
(
Label
(
text
=
result
[
'
display
'
]))
def
on_locations_not_loaded
(
self
,
_
,
error
):
self
.
root
.
ids
.
results
.
add_widget
(
Label
(
text
=
'
[Failed to load locations]
'
))
Logger
.
error
(
f
'
{
self
.
__class__
.
__name__
}
:
{
error
}
'
)
if
__name__
==
'
__main__
'
:
app
=
RestApp
()
app
.
run
()
This diff is collapsed.
Click to expand it.
openmrs.py
0 → 100644
+
46
−
0
View file @
6dc33d9f
import
base64
import
json
from
urllib.parse
import
quote
from
kivy.network.urlrequest
import
UrlRequest
class
RESTConnection
:
def
__init__
(
self
,
authority
,
port
,
username
,
password
):
self
.
authority
=
authority
self
.
port
=
port
credentials
=
f
'
{
username
}
:
{
password
}
'
credentials
=
base64
.
standard_b64encode
(
credentials
.
encode
(
'
UTF8
'
)).
decode
(
'
UTF8
'
)
self
.
headers
=
{
'
Authorization
'
:
f
'
Basic
{
credentials
}
'
,
'
Content-type
'
:
'
application/json
'
,
}
def
construct_url
(
self
,
resource
,
get_parameters
=
None
):
get_parameters
=
'
&
'
.
join
(
f
'
{
quote
(
str
(
key
))
}
=
{
quote
(
str
(
value
))
}
'
for
key
,
value
in
get_parameters
.
items
())
\
if
get_parameters
is
not
None
else
''
return
f
'
http://
{
self
.
authority
}
:
{
self
.
port
}
/openmrs/ws/rest/v1/
{
resource
}
?
{
get_parameters
}
'
def
send_request_by_url
(
self
,
url
,
post_parameters
,
on_success
,
on_failure
,
on_error
):
UrlRequest
(
url
,
req_headers
=
self
.
headers
,
req_body
=
json
.
dumps
(
post_parameters
)
if
post_parameters
is
not
None
else
None
,
on_success
=
on_success
,
on_failure
=
on_failure
,
on_error
=
on_error
)
def
send_request
(
self
,
resource
,
get_parameters
,
post_parameters
,
on_success
,
on_failure
,
on_error
):
url
=
self
.
construct_url
(
resource
,
get_parameters
)
self
.
send_request_by_url
(
url
,
post_parameters
,
on_success
,
on_failure
,
on_error
)
def
send_composite_request
(
self
,
resource
,
get_parameters
,
post_parameters
,
on_success
,
on_failure
,
on_error
):
results
=
[]
def
on_response
(
request
,
response
):
nonlocal
results
results
+=
response
[
'
results
'
]
for
link
in
response
.
get
(
'
links
'
,
[]):
if
link
[
'
rel
'
]
==
'
next
'
:
self
.
send_request_by_url
(
link
[
'
uri
'
],
post_parameters
,
on_response
,
on_failure
,
on_error
)
break
else
:
on_success
(
request
,
{
'
results
'
:
results
})
self
.
send_request_by_url
(
self
.
construct_url
(
resource
,
get_parameters
),
post_parameters
,
on_response
,
on_failure
,
on_error
)
This diff is collapsed.
Click to expand it.
rest.kv
0 → 100644
+
8
−
0
View file @
6dc33d9f
BoxLayout:
orientation: 'vertical'
BoxLayout:
id: results
orientation: 'vertical'
Button:
text: 'Load Locations'
on_press: app.load_locations()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment