Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Efrei-MS-TP1
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
Sofiane Lasri
Efrei-MS-TP1
Commits
4235f137
Commit
4235f137
authored
1 year ago
by
Sofiane Lasri
Browse files
Options
Downloads
Patches
Plain Diff
TP1 terminé
parents
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
main.py
+53
-0
53 additions, 0 deletions
main.py
with
55 additions
and
0 deletions
.gitignore
0 → 100644
+
2
−
0
View file @
4235f137
/.idea
\ No newline at end of file
This diff is collapsed.
Click to expand it.
main.py
0 → 100644
+
53
−
0
View file @
4235f137
import
requests
def
get_posts
():
posts
=
requests
.
get
(
'
https://jsonplaceholder.typicode.com/posts
'
)
return
posts
.
json
()[:
5
]
def
get_users
():
users
=
requests
.
get
(
'
https://jsonplaceholder.typicode.com/users
'
)
return
users
.
json
()[:
5
]
def
create_post
(
title
,
body
):
posts
=
requests
.
get
(
'
https://jsonplaceholder.typicode.com/posts
'
)
last_id
=
posts
.
json
()[
-
1
][
'
id
'
]
new_post
=
{
'
userId
'
:
1
,
'
id
'
:
last_id
+
1
,
'
title
'
:
title
,
'
body
'
:
body
,
}
requests
.
post
(
'
https://jsonplaceholder.typicode.com/posts
'
,
json
=
new_post
)
return
last_id
+
1
def
main
():
print
(
"
Souhaitez-vous:
"
)
print
(
"
1. Lister les 5 premiers posts
"
)
print
(
"
2. Lister les 5 premiers utilisateurs
"
)
print
(
"
3. Créer un post
"
)
choice
=
input
(
"
Votre choix:
"
)
if
choice
==
"
1
"
:
print
(
"
Liste des 5 premiers posts:
"
)
posts
=
get_posts
()
for
post
in
posts
:
print
(
"
Nom:
"
+
post
[
'
title
'
])
print
(
"
Contenu:
"
+
post
[
'
body
'
])
print
(
""
)
elif
choice
==
"
2
"
:
users
=
get_users
()
usersString
=
""
for
user
in
users
:
usersString
+=
user
[
'
name
'
]
+
"
,
"
print
(
"
Liste des 5 premiers utilisateurs:
"
)
print
(
usersString
[:
-
2
])
elif
choice
==
"
3
"
:
title
=
input
(
"
Titre:
"
)
body
=
input
(
"
Contenu:
"
)
new_post_id
=
create_post
(
title
,
body
)
print
(
"
Le post a été créé avec l
'
id
"
+
str
(
new_post_id
))
else
:
print
(
"
Choix invalide
"
)
if
__name__
==
"
__main__
"
:
main
()
\ No newline at end of file
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