📺 Develpreneur YouTube Episode

Video + transcript

DELETE API Example

2023-08-29 •Youtube

Detailed Notes

Building an API is more than just simple calls. We provide an example of Python Flask and a DELETE request as part of our series of tutorials around a URL shortener.

You can find out more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list and you will periodically receive coupons for courses as well as notifications of the latest releases.

Transcript Text
foreign
[Music]
well hello and welcome back we have
moved our way through our apis and one
of the things I want to do is handle a
couple of the sort of round out a couple
of these particularly the delete call so
we prior to this we had just something
just kicked out a little you know result
text so what we're going to do is we
actually want to go in and when we get a
delete request
then we want to actually delete that
record so if we go into our test what
we're going to end up doing I'm going to
do almost the exact same thing
and so here where it's doing a get
we're going to do
combo here because we're going to do is
we're going to do a post
and this is going to be
payload where URL equals uh we're just
going to call it my test
url.com slash blah
and that's going to be our payload
what it is like pretty it up a little
bit here
oh like that
I cannot be the side of my time oh
URL equals that
believe is how we want it to go
let's start with here let's actually go
out here and so if we run
let's go ahead and add our little
terminal
if we go in here
and we run our
API call
so we're going to run this so now we're
running our
API
and so we can come in and look here this
is on 4001 let's see this is the get so
let's say do we have
uh method not alone
uh let's see
that is a oops it says Purge we don't
want Purge we want to get
okay so we get a status here
and if we look here uh so what we want
is oh
so this is going to be so we have to
tweak stuff a little bit because there's
some space here so we're going to do our
base
is actually
what did I call that localhost
and
and do that
it's a local host
this
is not gonna get anything but if I post
let's go look at this if I'm going to
post something
uh URL we're going to send that
uh 5001.
so let's create a record
Okay so we've got this record that it
sent
and then what we want to do on a get is
take that same one
which I think we can do here
just leave that there
uh
do I need I probably don't need that
I can do it like this
so he's going to get back my ID okay so
I'm good here so let's do a second one
and let's do
um
bad URL
so it brings back so that's gonna be
number seven if we do number seven
there we go so now what we want to do
is we're going to do it's very similar
to the get
step four
this one's going to be a delete
and so we want to do here
is and we're going to see what we're
going to take from the delete so the
delete
where'd it go uh leads over here on our
API
and we're coming and we're going to get
our URL and so we can do is we're going
to say here this is actually going to be
uh we're going to get URL so navigate
url url let's just do it's going to be a
request Arc here so instead of this
being a query program
um oh no that's right
so it doesn't need to be a body it's
just going to be a query parameter
and we're going to do it here we're
going to delete now if we do it right
now it's going to tell us that there's
an error because it's going to blow up
it doesn't like that
because oh because I started to code
this so now we're going to go to shorty
delete
so instead of navigate uh instead of is
that what we wanted
that's new URL so this is the one we
want so we'll do it sort of like this
so now what we're going to do is we're
going to do a delete
and where you don't need to do that
we do want to get the index
and then what we're going to do is we're
going to come up here where did we do
that oh so we're just going to do
this basically the same
because this one actually does a
oh let's just do this
let's go in here and let's do a delete
link
and we either get an error or we get a
result
and this is going to be in uh my DB
which is
this guy
so here instead of is that where it's at
uh get next number retrieve length there
we go
so this is going to be delete link
and we're going to come in and this will
be actually pretty straightforward
because we're going to do
we're going to do a delete from
links
where code equals that
he's going to do an execute
he is going to need to do
a um
soft uh oh he does we probably do want
to do a commit
and then
let's just do uh
so equals
after deleted
uh new ID we don't need that we don't
need to return new ID because we're not
using it at all cursor close
uh
uh oh yeah I think we can do that and
we're going to return results so let's
see what happens
do that and then we go foreground
and kill it so now we're going to run it
again
okay and actually we'll just leave it
there we don't have to background it
here
so now what we're going to do is let's
see let's just do that we'll go right in
and we're going to delete it and let's
see what happens
gives us record deleted success so if we
come back over to our uh let's save this
first
is going to be a shorty delete
and
put it there
there we go so we've got short delete if
we go to shorty get
now it's not going to find it because we
have deleted the record so we have added
the ability to do a delete now without
doing a whole lot of extra work and this
again is where you go into where the API
is it's one of the nice things about it
is when you use the the request methods
you can basically have like here we have
really just one endpoint and from there
we're going to be able to post so we can
insert something we're going to do a get
and we're going to do a delete we don't
really have when you look at some of the
others we don't have some of these
others that don't make as much sense
so now we've got a very simple way to
come in and provide for a deletion of it
so that's a really uh I know it's a
little bit of a quick and dirty one
but I did want to take a look at that
show you how to implement a delete
within flask and we're going to come
back and sort of wrap this thing up
start looking at how we can deploy this
some of the little tweaks and things
like that we need to do part of the
configuration because we're going to use
this on a or actually deploy this to
something that uses an Apache server if
you use something else you know as a
front end then it'll vary a little bit
but at least we'll give you those
instructions
that being said hope you go out there
and have yourself a great day and we
will talk to you next time
hello this is Rob with developmentor
also known as building better developers
wanted to announce that we have
school.developmentor.com feel free to
check it out if you like any of this
information any of the content that
we've sent and you would like to see
more you can come out you can roll for
free we have free courses we've got
places for you to get better at just
learning a technology our how to's you
can work on your business skills we can
help you with becoming a better
developer as encoding and things like
that a lot of the stuff you've seen on
YouTube we also have out at
school.developmentor we just have it a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
developer
foreign
Transcript Segments
0.42

foreign

18.89

[Music]

27.18

well hello and welcome back we have

29.939

moved our way through our apis and one

32.64

of the things I want to do is handle a

34.2

couple of the sort of round out a couple

36.42

of these particularly the delete call so

41.1

we prior to this we had just something

43.68

just kicked out a little you know result

45.48

text so what we're going to do is we

47.34

actually want to go in and when we get a

50.64

delete request

52.86

then we want to actually delete that

55.44

record so if we go into our test what

58.68

we're going to end up doing I'm going to

60.42

do almost the exact same thing

62.52

and so here where it's doing a get

65.76

we're going to do

68.88

combo here because we're going to do is

70.38

we're going to do a post

73.14

and this is going to be

76.74

payload where URL equals uh we're just

80.7

going to call it my test

83.06

url.com slash blah

88.56

and that's going to be our payload

92.759

what it is like pretty it up a little

94.32

bit here

96.72

oh like that

102.78

I cannot be the side of my time oh

108.9

URL equals that

112.14

believe is how we want it to go

115.2

let's start with here let's actually go

117.299

out here and so if we run

121.32

let's go ahead and add our little

123

terminal

125.52

if we go in here

127.5

and we run our

132.06

API call

134.34

so we're going to run this so now we're

136.08

running our

138.36

API

140.7

and so we can come in and look here this

143.04

is on 4001 let's see this is the get so

145.44

let's say do we have

151.2

uh method not alone

155.84

uh let's see

159.36

that is a oops it says Purge we don't

162.12

want Purge we want to get

165.48

okay so we get a status here

167.58

and if we look here uh so what we want

177.48

is oh

179.7

so this is going to be so we have to

181.44

tweak stuff a little bit because there's

182.76

some space here so we're going to do our

184.2

base

185.7

is actually

188.34

what did I call that localhost

194.099

and

195.599

and do that

198.9

it's a local host

202.98

this

205.379

is not gonna get anything but if I post

208.8

let's go look at this if I'm going to

210.12

post something

212.7

uh URL we're going to send that

215.64

uh 5001.

218.459

so let's create a record

220.68

Okay so we've got this record that it

223.26

sent

225.54

and then what we want to do on a get is

228.78

take that same one

232.019

which I think we can do here

236.58

just leave that there

239.519

uh

241.739

do I need I probably don't need that

244.86

I can do it like this

248.459

so he's going to get back my ID okay so

250.5

I'm good here so let's do a second one

252.659

and let's do

254.58

um

255.78

bad URL

259.079

so it brings back so that's gonna be

260.94

number seven if we do number seven

268.62

there we go so now what we want to do

272.94

is we're going to do it's very similar

274.259

to the get

276.419

step four

278.16

this one's going to be a delete

282.419

and so we want to do here

286.979

is and we're going to see what we're

288.6

going to take from the delete so the

290.22

delete

291.9

where'd it go uh leads over here on our

294.84

API

297.96

and we're coming and we're going to get

299.58

our URL and so we can do is we're going

302.759

to say here this is actually going to be

306.78

uh we're going to get URL so navigate

309

url url let's just do it's going to be a

311.88

request Arc here so instead of this

313.62

being a query program

315.54

um oh no that's right

317.04

so it doesn't need to be a body it's

319.08

just going to be a query parameter

321.419

and we're going to do it here we're

323.039

going to delete now if we do it right

325.08

now it's going to tell us that there's

327.479

an error because it's going to blow up

329.039

it doesn't like that

332.1

because oh because I started to code

334.44

this so now we're going to go to shorty

337.56

delete

340.199

so instead of navigate uh instead of is

344.4

that what we wanted

346.86

that's new URL so this is the one we

348.72

want so we'll do it sort of like this

352.68

so now what we're going to do is we're

354.24

going to do a delete

358.1

and where you don't need to do that

362.18

we do want to get the index

366.3

and then what we're going to do is we're

367.979

going to come up here where did we do

370.02

that oh so we're just going to do

374.639

this basically the same

379.38

because this one actually does a

382.44

oh let's just do this

384.84

let's go in here and let's do a delete

386.16

link

389.22

and we either get an error or we get a

392.639

result

395.22

and this is going to be in uh my DB

399.44

which is

401.819

this guy

405

so here instead of is that where it's at

410.34

uh get next number retrieve length there

413.16

we go

415.259

so this is going to be delete link

424.8

and we're going to come in and this will

426.479

be actually pretty straightforward

427.62

because we're going to do

431.52

we're going to do a delete from

434.759

links

437.699

where code equals that

440.16

he's going to do an execute

443.039

he is going to need to do

446

a um

448.919

soft uh oh he does we probably do want

452.28

to do a commit

454.919

and then

459.539

let's just do uh

461.88

so equals

465.479

after deleted

468.9

uh new ID we don't need that we don't

471.479

need to return new ID because we're not

472.86

using it at all cursor close

475.68

uh

478.68

uh oh yeah I think we can do that and

481.8

we're going to return results so let's

482.94

see what happens

485.46

do that and then we go foreground

488.28

and kill it so now we're going to run it

490.74

again

492.599

okay and actually we'll just leave it

494.58

there we don't have to background it

495.84

here

496.919

so now what we're going to do is let's

498.24

see let's just do that we'll go right in

500.58

and we're going to delete it and let's

501.66

see what happens

503.28

gives us record deleted success so if we

506.46

come back over to our uh let's save this

509.58

first

511.379

is going to be a shorty delete

516.719

and

521.339

put it there

524.7

there we go so we've got short delete if

526.86

we go to shorty get

529.68

now it's not going to find it because we

532.5

have deleted the record so we have added

534.48

the ability to do a delete now without

538.14

doing a whole lot of extra work and this

540.779

again is where you go into where the API

543.42

is it's one of the nice things about it

545.279

is when you use the the request methods

548.04

you can basically have like here we have

549.959

really just one endpoint and from there

552.42

we're going to be able to post so we can

553.86

insert something we're going to do a get

555.3

and we're going to do a delete we don't

557.04

really have when you look at some of the

558.3

others we don't have some of these

559.44

others that don't make as much sense

561.6

so now we've got a very simple way to

564.24

come in and provide for a deletion of it

568.019

so that's a really uh I know it's a

570

little bit of a quick and dirty one

572.279

but I did want to take a look at that

575.16

show you how to implement a delete

577.08

within flask and we're going to come

580.5

back and sort of wrap this thing up

581.76

start looking at how we can deploy this

584.1

some of the little tweaks and things

586.56

like that we need to do part of the

587.76

configuration because we're going to use

589.14

this on a or actually deploy this to

591.66

something that uses an Apache server if

593.94

you use something else you know as a

595.56

front end then it'll vary a little bit

597.3

but at least we'll give you those

598.5

instructions

599.76

that being said hope you go out there

601.74

and have yourself a great day and we

603.6

will talk to you next time

606.42

hello this is Rob with developmentor

608.76

also known as building better developers

610.8

wanted to announce that we have

613.22

school.developmentor.com feel free to

615.48

check it out if you like any of this

617.7

information any of the content that

619.68

we've sent and you would like to see

620.88

more you can come out you can roll for

622.98

free we have free courses we've got

625.26

places for you to get better at just

627.899

learning a technology our how to's you

630.54

can work on your business skills we can

632.64

help you with becoming a better

634.019

developer as encoding and things like

636.48

that a lot of the stuff you've seen on

638.16

YouTube we also have out at

640.519

school.developmentor we just have it a

642.6

little more of a educational format and

645.12

a way for you to track your progress as

647.579

you move forward becoming a better

649.8

developer

651.24

foreign