Detailed Notes
The proliferation of websites and APIs in modern development makes it a common requirement to be able to call them from with code. This video focuses on API development and creating a Python application that utilizes the API.
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 if you are continuing along uh sort of going in order then last episode we talked about using Postman to work with an API and so we had that up we would do stuff like for example we would post and we'd give it a let's give it an address like this one we're going to take that and we're going to turn around and we're going to give it that address and when we post it it's going to send us back a shortened URL it's in our logic here in shorty that basically all it does is just each time it's going to give us another ID counter we have done this separately but right now we're doing it just in a very simple thing so if I come in to get if I get it with a zero it gives me one that was there a minute ago uh if I give it with the one then it gives me whoops why did you give me the same thing interesting sorry take this address and let's just put it here for now and let's take another one uh we'll go back to there and we'll do our calendly one because that's a big nasty one so those are two we're going to work with so let's work with those real quick let's get this fixed and figure out why it's not working so if I come in and I run and I post with so if I add the first one well let's add this one and there we go it tells me what that is so now if I post a second one should give me both yep so if we look here uh oh for some reason it was grabbing older one where is he grabbing that from oh I wonder if that's params instead of body that's our problem that's not what we want so we don't want it to be here we want it to be here uh is that right this is the post okay let's try this nope uh form data there we go okay so that's what we want that's why it wasn't working so now if I send that so short two should get me the calendly one and if I go back over here this is why we got to keep track of what we're doing and uh here we go and short three should give us this one so we're posting that and so if we do short zero I think that was I don't remember what that one was that was that quick start one is going to be the quick start thing there you go two is going to be the only and then three gives us the RB site okay so what we want to do now is we're going to build a little application to grab to basically do the call so that we can actually see that so we're going to call this thing let's make this collar at py and then we're going to need to do a couple things to get ourselves set up to make that call out to out to our API now the easiest way to do this is take a look at the get as I showed you before and if we come over here I'm looking at cheat is if we do python with requests watch what's going to happen if I come into here so I'm going to import requests which I think is already there let's see so if I do Python 3 caller boom so notice that I'm getting the same thing here that I got here so if you want a shortcut that's the best way to go but let's examine this a little bit so we're going to actually turn this into something a little more useful and so requester we create request is going to be uh let's do well let's make it pretty uh how cool do we want to do this it's a good question we can either store it or we can do something with it so let's go ahead for now and we're going to actually do a we actually put this together a little bit so what we're going to do is we're going to do uh we're going to call Base URL and it's going to be equal to that guy and it's going to get us all the way up to let's do here we're going to actually break this down a little bit and then we're going to have the um let's say get short let's do the short end point it's just going to be shorten now this is going to be for the get this is going to be a little different we do the the put and we'll show that in a minute so what we want to do here is we're going to do let's see the URL equals input which URL push to use and then we're going to have a little function that we're going to call get resolve get short uh no get real get real URL let's do get full URL and what it's going to do is it's going to take a URL push these up here and then we're gonna do this and then we're going to take this stuff notice we don't need to worry about a payload or headers this would be if you were doing something like let me do this real quick uh if you're doing something like having form date or something which we're going to show in a minute when we do the post so we're gonna come in here we're going to say which URL do you wish to use and then it's going to take the URL and this is going to be pretty simple um response.txt and which is assuming it's we should probably are going to want to wrap some stuff around it but we'll check that in a minute uh so print let's see get for a little URL so now if we do this and we say what is URL you wish to use and so we could use short three and it's going to blow up because it doesn't like what we said it so let's go take a look at this because this is going to be where we're going to start running into some issues so let's go look at it so let's see connection refused oh wait uh whoops oh that's the problem is because here uh let's see so URL is actually going to be so the uh let's call that API URL oops so it's going to be equal to base URL Plus a short endpoint whoops if I can write that oh there we go that's it short end point and so that gives me that plus short and then I'm going to need to do plus Uh u r l equals and then I'm going to add the URL so now if I run it um see this now if I run it with the three boom it gives me that if I run it with a if I run it and I do things same thing and I give it a two then notice I get a different let's do it again so if I do that and if I call it with a 3 notice I either get the https or I get this one so I get something a little bit different from each of these now what I want to do here is now I want to take this out because in an API call what you want to do is you want to have something that's a little more valuable essentially in an API you don't want to just send strings back like this so what we really want to do here is we're going to come in and we aren't sure well we're going to we'll hold this off until our next episode we're going to talk about cleaning these up a little bit what you've got here is that is our where'd that go collar so this is going to be to get for the get full URL but now what we want to do is we're going to do something similar that is going to be um Escape get shortened URL and now for this we're still going to cheat because this is so easy to do so I come over here for the Post which there we go we're going to take that and it's going to look about the same but what you're going to see here return response that text so you can see the same kind of thing except because it's the the URL is going to be the same combo so we can do it like this we don't need that we already have requests up at the top so we don't need that again our payload now is going to be and so this is that form we're setting the URL equal to the URL that they send in so this is where we have a payload we have for running files we're not seeing those in we're not sending anything that's a header of any sort come in make this the API URL but because it's a post we're going to do that just to be clean we could send some of these things and work with them that's beyond what we're going to talk about at this point and they're going to return the response text so now if I come in and say let's see we're going to do that we're going to print it and then we're going to come out and say which well do you wish to shorten and it's going to do get shortened URL uh we'll do your the URL again so now first one we do we'll just do let's get number four and see what that was so we get number four then that's what it was but if we take this long thing here and say that's what we want to shorten then we're going to get short five so now to see do we get that back if we call short five oh which is this we can just take this and uh oh and then it kicked it back here this is what you sent short five this is the actual URL okay so we have fairly easily and this is the the breakdown of what we do is she people with requests all you have to do if you note is you're going to on requests make a request tell it to type and we'll talk another time about various types are out there you send it to whatever its address is in this case it's our API address send some headers if you need to payload which you might need to do like for example in a form and then it's going to send you a response back based on whatever is returned by that by that API call so this is really simple and as I mentioned before and I've shown you if you want to use Postman that is going to be the fastest way to get you just like flying through setting up both sides and testing your API which what we want to do with our shortener here is to just get this thing working because then we can put it out on our server and have our own little API our own little urls shortener that we're going to need so we're going to come back next episode or we're going to clean these things up a little bit and start tying them back into the database but for now 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 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.development or 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 thank you
Transcript Segments
foreign
[Music]
well hello and welcome back if you are
continuing along uh sort of going in
order then last episode we talked about
using Postman to work with an API and so
we had that up we would do stuff like
for example we would post and we'd give
it a let's give it an address
like this one
we're going to take that and we're going
to turn around and we're going to give
it that address
and when we post it
it's going to send us back a shortened
URL it's in our logic here in shorty
that basically all it does is just each
time it's going to give us another ID
counter
we have done this separately but right
now we're doing it just in a very simple
thing so if I come in to get if I get it
with a zero it gives me one that was
there a minute ago uh if I give it with
the one
then it gives me
whoops
why did you give me the same thing
interesting
sorry take this address and let's just
put it here for now and let's take
another one uh we'll go back to there
and we'll do
our calendly one because that's a big
nasty one
so those are two we're going to work
with so let's work with those real quick
let's get this fixed and figure out why
it's not working so if I come in and I
run
and I post with
so if I add the first one well let's add
this one
and
there we go it tells me what that is so
now if I post a second one
should give me both yep so if we look
here
uh
oh for some reason it was grabbing
older one where is he grabbing that from
oh
I wonder if
that's params instead of body
that's our problem
that's not what we want
so we don't want it to be here we want
it to be here
uh
is that right
this is the post okay
let's try this
nope
uh form data there we go okay so that's
what we want
that's why it wasn't working so now if I
send that
so short two should get me the calendly
one and
if I go back over here
this is why we got to keep track of what
we're doing
and uh here we go
and short three
should give us this one so we're posting
that and so if we do short zero I think
that was I don't remember what that one
was
that was that quick start one is going
to be the quick start thing
there you go two is going to be the
only and then three gives us the RB site
okay
so what we want to do now is we're going
to build a little application to grab
to basically do the call so that we can
actually see that so we're going to call
this thing
let's make this collar
at py
and then we're going to need to do a
couple things to get ourselves set up to
make that call out to out to our API
now the easiest way to do this is take a
look at the get
as I showed you before
and if we come over here
I'm looking at cheat
is if we do python with requests
watch what's going to happen if I come
into here
so I'm going to import requests which I
think is already there let's see
so if I do Python 3
caller
boom
so notice that I'm getting the same
thing here
that I got here so if you want a
shortcut that's the best way to go but
let's examine this a little bit so we're
going to actually turn this into
something a little more useful
and so requester
we create request
is going to be uh
let's do well let's make it pretty uh
how cool do we want to do this it's a
good question
we can either store it or we can do
something with it so let's go ahead for
now and we're going to actually do
a
we actually put this together a little
bit so what we're going to do is we're
going to do uh we're going to call Base
URL
and it's going to be equal to that guy
and it's going to get us all the way up
to
let's do here we're going to actually
break this down a little bit
and then we're going to have the um
let's say get
short
let's do the
short end point
it's just going to be
shorten
now this is going to be for the get this
is going to be a little different we do
the the put and we'll show that in a
minute
so what we want to do here is we're
going to do let's see
the URL equals
input
which URL
push to use
and then we're going to have a little
function that we're going to call
get resolve get short uh no get
real
get real URL let's do get full URL
and what it's going to do is it's going
to take
a URL
push these up here
and then we're gonna do this and then
we're going to take this stuff
notice we don't need to worry about a
payload or headers this would be if you
were doing something like let me do this
real quick uh if you're doing something
like having form date or something which
we're going to show in a minute when we
do the post
so we're gonna come in here we're going
to say which URL do you wish to use
and then it's going to take the URL
and this is going to be pretty simple
um
response.txt
and
which is assuming it's we should
probably are going to want to wrap some
stuff around it but we'll check that in
a minute uh so print let's see get for a
little URL
so now if we do this
and we say
what is URL you wish to use and so we
could use
short three
and it's going to blow up because it
doesn't like what we said it so let's go
take a look at this because this is
going to be
where we're going to start running into
some issues so let's go look at it so
let's see
connection refused
oh wait
uh
whoops
oh that's the problem is because here uh
let's see so URL
is actually going to be so the uh
let's call that API URL
oops so it's going to be equal to
base URL
Plus
a short endpoint
whoops
if I can write that oh there we go
that's it short end point
and so that gives me that plus short and
then I'm going to need to do plus
Uh u r l equals
and then I'm going to add the URL
so now if I run it
um
see this now if I run it
with the three boom it gives me
that if I run it with a
if I run it and I do things same thing
and I give it a two
then notice I get a different
let's do it again so if I do that and if
I call it with a 3 notice I either get
the https or I get this one
so I get something a little bit
different from each of these now what I
want to do here is now I want to take
this out because in an API call what you
want to do
is you want to have something that's a
little more
valuable essentially in an API you don't
want to just send strings back like this
so what we really want to do here
is we're going to come in and we aren't
sure well we're going to we'll hold this
off until our next episode we're going
to talk about cleaning these up a little
bit
what you've got here is that is our
where'd that go collar so this is going
to be to get for the get full URL but
now what we want to do is we're going to
do something similar that is going to be
um
Escape get shortened
URL
and now for this we're still going to
cheat because this is so easy to do so I
come over here
for the Post
which there we go we're going to take
that
and it's going to look about the same
but what you're going to see here
return response that text
so you can see the same kind of thing
except because it's the the URL is going
to be the same combo
so we can do it like this
we don't need that we already have
requests up at the top so we don't need
that again
our payload now is going to be and so
this is that form we're setting the URL
equal to
the URL that they send in
so this is where we have a payload we
have for running files we're not seeing
those in we're not sending anything
that's a header of any sort
come in make this the API URL but
because it's a post we're going to do
that just to be clean
we could send some of these things and
work with them that's beyond what we're
going to talk about at this point and
they're going to return the response
text
so now if I come in and say
let's see
we're going to do that we're going to
print it and then we're going to come
out and say which well do you wish to
shorten
and it's going to do get
shortened URL
uh we'll do your the URL again
so now
first one we do we'll just do let's get
number four and see what that was
so we get number four
then that's what it was but if we take
this long thing
here and say that's what we want to
shorten
then we're going to get short five so
now to see do we get that back
if we call
short five
oh which is this we can just take this
and
uh oh and then it kicked it back here
this is what you sent short five this is
the actual URL okay
so
we have fairly easily and this is the
the breakdown of what we do is she
people with requests all you have to do
if you note is you're going to on
requests make a request tell it to type
and we'll talk another time about
various types are out there you send it
to whatever its address is in this case
it's our API address send some headers
if you need to payload which you might
need to do like for example in a form
and then it's going to send you a
response back based on whatever is
returned by that by that API call
so this is really simple and as I
mentioned before and I've shown you if
you want to use Postman that is going to
be the fastest way to get you just like
flying through setting up both sides and
testing your API which what we want to
do with our shortener here is to just
get this thing working because then we
can put it out on our server and have
our own little API our own little urls
shortener that we're going to need so
we're going to come back next episode or
we're going to clean these things up a
little bit and start tying them back
into the database but for now 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 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.development or 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
thank you