Detailed Notes
Building an API is more than just simple calls. Those are often a GET request and there are many others available. This video looks into them and adding those into an API we created with Python and Flask.
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 are cruising along in our little shortener application doing a series of questions and answers basically to get you through some key things so basically we're going to do here is we're going to talk a little bit about we've got ourselves so we've got an API that we're calling from python we are now going to talk a little bit about get and post and deletes and request types A little bit really more uh the getting the posts and just sort of how these things work and what you should be doing to actually craft like a decent API call because what we've got right here just kicks out if you see it when we do calls it just kicks out some nice big texty thing which is really not generally useful I mean you can see it here is it's just here's your text what we really want is something that is more workable because our raw notice is the same as our pretty and we're jasonifying stuff when we don't really have a need to so what you normally want to do is you're going to have something in your message that you're going to send back so we're going to change up our API this time and what we're going to do is we're going to actually start working on this a bit so instead of uh let's see do I want to call that I'm trying to think if I want to go ahead let's go ahead and we'll come back to this another time because I know I was going to hook the whole thing up but uh I could probably do that pretty quick okay so let's see um yeah I can probably do this so let's try this so here's what we're going to do so we've got a little database set up we got a couple calls so we're going to go to shorty and over here we're going to import or actually how does it look uh it is uh from DB create whoops import my database I think that's what I want to do whoop my database there we go so now what we're going to do here is we don't have to worry about our counter anymore but we do want to do is we're going to actually let's do in a knit and take all of that like this and so let's see we're gonna set up our database we don't need to create one there's a database name we're going to pull oop import uh let's do this we're going to change this around a little bit so we're going to come in here and we're going to do this take that I'm going to come over here so first thing we want to do is we're going to set up we're doing a little bit of changes here is we're going to come in and we're going to set up our database we're going to get here we're gonna get his name I think that's all we need here we don't need to create a table uh we don't need rows oh we do want to steal one of those calls insert record links okay so those are the ones we're going to need so we need searches and then we've got a connection that we don't need to worry about right now let's do this so we're going to use its Constructor which means we create this shortener it's going to go through these things we don't need it to go through these so let's do that and now what we want to do is so navigate is going to be just going to do a retrieve link based on the number so here he's going to do uh let's do my DB uh let's do this and I'm going to do my DB connect which is a self.idb Connect and then this is going to be self.idb dot retrieve link based on the idx that and then we're gonna do we're gonna do message well we're gonna do this we'll do that afterwards so we're gonna do uh let's see what's that we're gonna return the message but the message is going to be and it's going to be status is status and data is whoops pan data is result the result there we go status we're going to do this we're going to try that we're going to do this sort of simplify but and I will walk through this in just a second except if I can spell it right and then here status equals error their status SQL status there we go real life results you probably don't like that a variable annotation cannot be combined with Tuple unpacking okay I'm not sure what that means we're gonna find out in a second oh it probably needs to be this maybe that's what it likes okay let's try that so retiring light retrieval ain't uh yeah okay so that gives us our Link in the database let's see is it going to complain uh probably yeah invalid status so we need this to be oh because I did that wrong there we go uh and so let me go ahead and get this one done while I'm at it we're gonna do basically the same thing so here when we retrieve New York URL um our return value so we need to insert a record link easy because that's going to be Health Dot and then return value let's say that well let's just do this it's going to look about the same let's take that we're going to look about the same we don't need to close or return the message what we're going to try is this and he's instead going to be uh oh here it's looking for a code that's right so before we're going to give it a code so let's go look back here at dbcreates when it does or did we do that create database create table list tables list rows insert record links here we go so we were going to give it a number and let's do let's do this we're going to actually get a little bit funky here so we're gonna get uh let's just do this uh we're gonna we're gonna cheat for now uh no we're not okay I was going to but let's we'll come back to this so let's do uh get next number and this one's gonna be really simple because all we're going to do is we're gonna let's see here here and we're going to do select Max ID plus one from links and then this and let's see result oops so here we're going to do we can do because before we did a fetch all we saw this here we're going to do effects one so we don't need to commit so we're going to do row equals fetch one and then let's see we're going to do ID equals oh let's do yeah we'll do uh we'll call it result so it equals zero here the result equals negative one we're gonna print that we're going to return the result and this is going to be result because if we fetch one and it's going to be zero because we want the first row uh probably is going to get overwritten so we can get away with it but we're gonna let that go anyways okay so that's gonna be getting X number so when we go to shorty uh here we're gonna set our counter so we're gonna do uh we're just gonna call it style equals self Dot mydb dot get next number and then here the return value is just simply going to be the base Plus that but then we need to save it uh which I think we're gonna be okay so that's our return value and so here we need to take oh this actually simplifies because now this is going to be the return value which is the um the URL the short URL and then the next thing is going to be the next vowel because that's what our code is and then a one because oh because that was our user so I have to worry about it and then we have a result okay boom so now we have Rewritten these things to be something a little more useful should run okay so let's go look at these real quick so if we go and do a post of this oh and shorty status equals error oh here we go I forgot to put that there oh which we also forgot uh uh here no here we want this to be oh shoot shorty doesn't really need to do this because he's we really want him to be on the API okay my mistake uh but let's do that anyways so what we're going to do now is we're going to pass those through so now uh this is the so retrieve new new URL is the result and then here result and then here we're just going to dump the results so now normally we're going to do this somewhere else but we'll do these here because that'll just allow us to get that done so now if we run this and we come here and run our post first then okay data is three oops we did not want that to be three we wanted our AP um when we add a link uh let's see insert record link which means over at Shorty we didn't want it to be the return value oh yeah self.base plus next vowel oh data is oops my bad that needs to be return value because yes that's going to be successful insert record links doesn't give anything oh because that's the new and we're going to call we'll do this um uh let's call that new ID and that'll be result otherwise result is zero let's do that for fun okay so that is shorty let's get him here if we do that let's get rid of that so now if we run it uh so that was three this is probably gonna be something else okay so four so now if we take that and see that's like the pretty version Raw it's actually about the same but now we're getting something that we can actually work with we can do something with this on the other side of it so shorty uh get and so if I do four then he's going to send that back and so now in my app my caller I can actually do something with this so now my response I'm going to get something and I'm going to get my response dot text and let's go look at the same thing uh so let's see so what you already wish to use let's use this big one and so now uh [Music] in that literal for INT so what do we do here oh Oops I did that reverse order so I want this to be this guy wait right yeah okay and so now I can see that I've got data and let's see which short URL do you want to shorten if I take this big one I've got that but now I can take the same thing and let's go change it now so that when I do caller let's see what he wants to do instead because he's going to do response.text and he's going to be so the shortened URL is actually uh uh the data so let's see how that works let's see if we do that now we do that for short uh is it not printing out what I wanted it to oh yes that's the problem not worry about shortening one and let's see which I already wish to use oh so this is because of the way it's set up so data is what was Data data was two I think data is one okay so it's this just because of what we're doing so if we do it this way and we do it this way let's see what that looks like so now if we do it it gives us an empty oh we want zero let's try that whoop oh let's do this super spots data uh response object is not subscriptable so we did not do what we wanted to with this so let's take a look here so we got that that's our payload should be that um our API is going to send back that so our call is that uh let's see what did we miss here what was it complaining about it said it didn't like response object is not subscriptable so let's go look at response real quick uh let's do that oh we need to do uh where is it response.data let's go see let's go look at him again so that's response is 200. so if we go look here I wonder if that is the wrong brackets that I wanted to use huh I think so okay my mistake I was doing a dump to a string I need to do a jersonify so now I see something makes a little better sense and then uh Where'd I go over here so now if I do oops I don't want shorty I want the collar uh so if he does response uh well let's look at that first if I look at that uh if I shorten [Music] okay there's my response how to response dot data I think make sure I get this right um whoops uh it doesn't have that so if I do let's try a response well let's do next to that oh dang it keep hitting the wrong key uh okay so there's the data and if I do response text data is that going to give me I think that's going to give me what I want ah there we go uh nope it must be an integer okay so it's not getting that back out one second okay we have to actually do this is where we want um let's see our cell equal Json dot loads uh oh am I in the right place that's not really where let's sit this way I want it here in the API because I already have it uh so I'm gonna jsonify the result oh and then the caller is going to need that so he's going to need an import Json and then he's gonna need from the response he's going to do there's no equals Json dot okay so that loads the response dot text and then I can do the result now I can do the result I'm going to call that data look at that and I think we're going to see here there we go and so now we're actually pulling this so we're putting a complex thing together that's we can check for like so here we can say um if oops I don't want to put it there yet so I want to come back here and we could say if results uh what did we call that we called it here status usually we're going to do this from the API we did it's a little different but if we take result status equals let's say not equal to success then return status there we go otherwise you're going to return the result so now we're going to get something like this so if we go here and we give it something bogus then it should oh it's going to blow up uh because we didn't put a try here wait uh let's do this let's just move him up uh who's that that is shorty so oops we're going to restart this start our API now if we do the call and give it something useless let's see oh results very good result equals negative one we got to set that up too so now let's try that one more time and let's just give it something that's bogus so now it's going to tell us an error so there you go so now we've actually added a little bit error checking to our API which is something you definitely want to do final thing is I did want to mention there's a difference between you have gits and posts and deletes and updates and things like that typically as we're seeing here is use a get to actually get a value from it's sort of like a list kind of thing post is usually going to be an update which is what we're doing here so when we're inserting our record we're going to do retrieve new URL otherwise if it's a get which really should be uh I'll have to get then well we're going to do that what we could do is we could say whoop I'm gonna do this is we could say oh if it's delete then result equals um let's just say so it is deleted and then we can come back down here and we can do else of equals uh at the not import ant so now if we come in and we can do let's run this and we're going to show you what some of these are so if I could run this and I come over here to my let's change my get but instead of a get these are all these little guys we can do so if I'm going to come in so if I said delete method not allowed oh because I haven't turned it on here so this actually is already going to get it to me so if I hit delete but uh let me also do oh let's just pick another one let's do oh let's do a patch which is usually going to be an update so that would be a different one so let's do that so let's do those three or those four so now if we run it if we do a get we're good if we do a delete it's going to say assume it's deleted if we do a patch it's going to get through but it's going to say it's not implemented and if we do like say a purge then it's going to blow up but it's going to say it's not allowed so you've got a quite a few things that are used here and it's one of those that's probably best to sort of see what the you know the standards are particularly for your your organization as far as development standards and such but you will see these fairly often used at least you'll see get post put patch and delete I see a lot the others that sort of varies but it's good to know they're out there and to know that that's the difference is that you can use the same API endpoint and you can actually have several different functions working off of that as we're doing so that gives you a a good dive into your gits or posts and things like that we have gotten a lot of functionality into our API and we'll come back next time and we'll continue on so thanks a lot we've got our post get delete request types got those things done and uh get some more done next time so go out there and have yourself a great day and we will talk to you next time thank you 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 enroll 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.develop anywhere always having a little more of a educational format and a way for you to track your progress as you move forward becoming a better developer
Transcript Segments
foreign
[Music]
well hello and welcome back we are
cruising along in our little shortener
application doing a series of questions
and answers basically to get you through
some key things so basically we're going
to do here is we're going to talk a
little bit about we've got ourselves so
we've got an API that we're calling from
python
we are now going to talk a little bit
about get and post and deletes and
request types A little bit really more
uh the getting the posts and just sort
of how these things work and what you
should be doing to actually craft like a
decent API call because what we've got
right here
just kicks out if you see it when we do
calls it just kicks out some nice big
texty thing which is really not
generally useful I mean you can see it
here is it's just here's your text what
we really want is something that is more
workable because our raw notice is the
same as our pretty
and we're jasonifying stuff
when we don't really have a need to so
what you normally want to do
is you're going to have something in
your message that you're going to send
back so we're going to change up our API
this time
and what we're going to do
is we're going to actually start working
on this a bit so instead of
uh let's see do I want to call that
I'm trying to think if I want to go
ahead let's go ahead
and we'll come back to this another time
because I know I was going to hook the
whole thing up but
uh I could probably do that pretty quick
okay
so let's see
um
yeah I can probably do this so let's try
this
so here's what we're going to do
so we've got a little database
set up
we got a couple calls so we're going to
go to shorty
and over here
we're going to import
or actually how does it look uh it is uh
from DB create
whoops
import
my database I think that's what I want
to do
whoop
my database there we go
so now what we're going to do here is we
don't have to worry about our counter
anymore
but we do want to do is
we're going to actually let's do in a
knit
and take all of that
like this
and so let's see we're gonna set up our
database we don't need to create one
there's a database name we're going to
pull oop import
uh
let's do
this we're going to change this around a
little bit
so we're going to come in here
and we're going to do this
take that I'm going to come over here
so first thing we want to do is we're
going to set up we're doing a little bit
of changes here is we're going to come
in and we're going to set up our
database
we're going to get here
we're gonna get his name I think that's
all we need here
we don't need to create a table uh we
don't need rows
oh we do want to steal one of those
calls insert record links okay so those
are the ones we're going to need so we
need
searches and then we've got a connection
that we don't need to worry about right
now
let's do this
so we're going to use its Constructor
which means we create this shortener
it's going to go through these things we
don't need it to go through these
so let's do that
and now what we want to do is so
navigate
is going to be
just going to do a retrieve link
based on the number
so here
he's going to do
uh let's do my DB
uh let's do this
and I'm going to do my DB connect which
is a
self.idb Connect
and then this is going to be
self.idb dot retrieve link based on the
idx
that
and then we're gonna do we're gonna do
message
well we're gonna do this we'll do that
afterwards so we're gonna do uh let's
see
what's that
we're gonna return
the message but the message is going to
be
and it's going to be status
is status
and
data
is
whoops
pan data
is result
the result there we go status
we're going to do this we're going to
try that
we're going to do this sort of simplify
but and I will walk through this in just
a second except if I can spell it right
and then here
status equals error
their status
SQL status there we go real life results
you probably don't like that
a variable annotation cannot be combined
with Tuple unpacking okay I'm not sure
what that means we're gonna find out in
a second
oh
it probably needs to be this maybe
that's what it likes
okay let's try that so retiring light
retrieval ain't uh yeah okay
so that gives us our Link in the
database
let's see is it going to complain uh
probably
yeah invalid status so we need this to
be oh
because I did that wrong
there we go
uh and so let me go ahead and get this
one done while I'm at it
we're gonna do basically the same thing
so here when we retrieve New York URL
um
our return value so we need to insert a
record link
easy because that's going to be
Health Dot
and then return value
let's say that well let's just do this
it's going to look about the same
let's take that we're going to look
about the same we don't need to close
or return the message
what we're going to try is this
and he's instead going to be
uh
oh here it's looking for a code that's
right so before we're going to give it a
code
so let's go look back here at
dbcreates when it does
or did we do that
create database create table list tables
list rows insert record links here we go
so we were going to give it a number
and let's do
let's do this
we're going to actually get a little bit
funky here so
we're gonna get uh
let's just do this uh we're gonna we're
gonna cheat for now
uh no we're not okay I was going to but
let's we'll come back to this so let's
do uh get next number
and this one's gonna be really simple
because all we're going to do is we're
gonna let's see here here
and we're going to do select Max ID
plus one
from links
and then
this
and let's see result oops
so here we're going to do we can do
because before we did a fetch all we saw
this
here we're going to do effects one so we
don't need to commit
so we're going to do row equals fetch
one
and then
let's see we're going to do ID equals oh
let's do yeah we'll do uh we'll call it
result
so it equals zero here
the result equals negative one
we're gonna print that we're going to
return the result
and this is going to be result
because if we fetch one and it's going
to be zero because we want the first row
uh probably is going to get overwritten
so we can get away with it but we're
gonna let that go anyways okay so that's
gonna be getting X number so when we go
to shorty uh here we're gonna set our
counter so we're gonna do uh we're just
gonna call it
style equals
self Dot mydb
dot get next number
and then here
the return value is just simply going to
be the base
Plus
that
but then we need to save it
uh which I think we're gonna be okay
so that's our return value and so here
we need to take oh this actually
simplifies because now this is going to
be the return value which is the um
the URL the short URL and then the next
thing is going to be the next vowel
because that's what our code is
and then a one because oh because that
was our user so I have to worry about it
and then we have a result okay boom so
now we have Rewritten these things to be
something a little more useful
should run okay so let's go look at
these real quick so if we go
and do a post
of this
oh and shorty
status equals error oh here we go I
forgot to put that there oh which we
also forgot
uh uh here no
here
we want this to be
oh shoot
shorty doesn't really need to do this
because he's
we really want him to be on the API okay
my mistake uh but let's do that anyways
so what we're going to do now is we're
going to pass those through so now
uh this is the so retrieve new new URL
is the result
and then here result
and then here we're just going to dump
the results so now normally we're going
to do this somewhere else but we'll do
these here because that'll just allow us
to get that done
so now if we run this
and we come here and run our post first
then okay data is three oops we did not
want that to be three we wanted our AP
um
when we add a link
uh let's see
insert record link which means over at
Shorty we didn't want it to be the
return value oh yeah
self.base
plus next vowel
oh data is oops my bad
that needs to be return value
because
yes that's going to be successful
insert record links doesn't give
anything oh because that's the new
and we're going to call we'll do this
um
uh let's call that new ID
and that'll be result
otherwise result is zero
let's do that for fun
okay so that is shorty let's get him
here
if we do that
let's get rid of that so now if we run
it
uh so that was three this is probably
gonna be something else okay so four so
now if we take that
and see that's like the pretty version
Raw it's actually about the same
but now we're getting something that we
can actually work with we can do
something with this on the other side of
it
so shorty uh get
and so if I do
four
then he's going to send that back and so
now in my
app
my caller I can actually do something
with this so now my response I'm going
to get something and I'm going to get my
response dot text
and let's go look at the same thing uh
so let's see
so what you already wish to use let's
use this big one
and so now
uh
[Music]
in that literal for INT so what do we do
here
oh
Oops I did that reverse order
so I want this to be this guy
wait right yeah
okay
and so now I can see that I've got data
and
let's see which short URL do you want to
shorten if I take this big one
I've got that
but now I can take the same thing
and let's go change it now so that when
I do
caller
let's see what he wants to do instead
because he's going to do response.text
and he's going to be so the shortened
URL is actually
uh uh the data
so let's see how that works
let's see if we do that now
we do that for short
uh is it not printing out what I wanted
it to
oh yes
that's the problem
not worry about shortening one
and let's see which I already wish to
use
oh so this is because of the way it's
set up so data is what was Data data was
two I think
data is one okay
so it's this just because of what we're
doing so if we do it this way
and we do it this way let's see what
that looks like
so now if we do it
it gives us an empty oh we want
zero let's try that
whoop
oh
let's do this
super spots
data
uh response object is not subscriptable
so we did not do what we wanted to with
this
so let's take a look here so we got that
that's our payload should be that
um
our API is going to send back
that so our call
is that
uh let's see what did we miss here
what was it complaining about it said it
didn't like
response object is not subscriptable so
let's go look at response real quick uh
let's do that
oh
we need to do
uh
where is it
response.data let's go see
let's go look at him again
so that's response is 200.
so if we go look here
I wonder if that is the wrong
brackets that I wanted to use
huh I think so
okay my mistake I was doing a dump to a
string I need to do a jersonify so now I
see something makes a little better
sense and then
uh Where'd I go over here
so now if I do oops I don't want shorty
I want the collar
uh so if he does response
uh well let's look at that first if I
look at that
uh if I shorten
[Music]
okay there's my response
how to response dot data I think
make sure I get this right
um
whoops
uh it doesn't have that so if I do
let's try a response well let's do
next
to that
oh dang it keep hitting the wrong key
uh okay so there's the data and if I do
response text
data
is that going to give me I think that's
going to give me what I want
ah
there we go
uh nope it must be an integer okay
so it's not getting that back out one
second
okay we have to actually do this is
where we want
um
let's see our cell equal Json dot loads
uh oh am I in the right place
that's not really where let's sit this
way I want it here in the API because I
already have it
uh so I'm gonna jsonify the result
oh and then the caller is going to need
that so he's going to need an import
Json
and then he's gonna need from the
response
he's going to do there's no equals
Json dot okay so that loads
the response dot text
and then I can do the result
now I can do the result I'm going to
call that data
look at that
and I think we're going to see here
there we go and so now we're actually
pulling this so we're putting a complex
thing together that's we can check for
like so here we can say
um
if oops I don't want to put it there yet
so I want to come back here and we could
say if
results
uh what did we call that
we called it
here
status
usually we're going to do this from the
API we did it's a little different but
if we take result status
equals
let's say not equal to
success
then
return
status
there we go otherwise you're going to
return the result
so now we're going to get something like
this so if we go here
and we give it something bogus
then it should oh it's going to blow up
uh because we didn't put a try here wait
uh
let's do this
let's just move him up
uh who's that that is shorty so oops
we're going to restart this
start our API now if we do the call
and give it something useless
let's see
oh results
very good result equals negative one we
got to set that up too
so now
let's try that one more time
and let's just give it something that's
bogus
so now it's going to tell us an error so
there you go so now we've actually added
a little bit error checking to our API
which is something you definitely want
to do
final thing is I did want to mention
there's a difference between you have
gits and posts and deletes and updates
and things like that typically as we're
seeing here is use a get to actually get
a value
from it's sort of like a list kind of
thing post is usually going to be an
update which is what we're doing here so
when we're inserting our record we're
going to do retrieve new URL
otherwise if it's a get
which really should be
uh I'll have
to get
then well we're going to do that
what we could do is we could say whoop
I'm gonna do this is we could say oh if
it's delete
then result equals
um let's just say
so it is deleted
and then we can come back down here and
we can do else
of equals uh
at the not import
ant
so now if we come in and we can do let's
run this
and we're going to show you what some of
these are so if I could run this and I
come over here to my let's change my get
but instead of a get
these are all these little guys we can
do so if I'm going to come in so if I
said delete
method not allowed
oh because I haven't turned it on here
so this actually is already going to get
it to me so if I hit delete
but uh let me also do
oh let's just pick another one
let's do
oh
let's do a patch which is usually going
to be an update
so that would be a different one so
let's do that so let's do those three or
those four
so now if we run it
if we do a get
we're good
if we do a delete it's going to say
assume it's deleted if we do a patch
it's going to get through but it's going
to say it's not implemented
and if we do like say a purge
then it's going to blow up but it's
going to say it's not allowed
so you've got a quite a few things that
are used here and it's one of those
that's probably best to sort of see what
the you know the standards are
particularly for your your organization
as far as development standards and such
but you will see these fairly often used
at least you'll see get post put
patch and delete I see a lot
the others that sort of varies but
it's good to know they're out there and
to know that that's the difference is
that you can use the same API endpoint
and you can actually have several
different functions working off of that
as we're doing so that gives you a a
good dive into your gits or posts and
things like that we have gotten a lot of
functionality into our API and we'll
come back next time and we'll continue
on so thanks a lot we've got our post
get delete request types got those
things done and uh get some more done
next time so go out there and have
yourself a great day and we will talk to
you next time
thank you
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 enroll 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.develop anywhere always having a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
developer